Beispiel #1
0
        private async Task <short> GetVersionAsync(ApiKey apiKey, CancellationToken cancellationToken)
        {
            var configuredSupport = _configuration.VersionSupport as DynamicVersionSupport;

            if (configuredSupport == null)
            {
                return(_configuration.VersionSupport.GetVersion(apiKey).GetValueOrDefault());
            }

            var versionSupport = _versionSupport;

            if (versionSupport != null)
            {
                return(versionSupport.GetVersion(apiKey).GetValueOrDefault());
            }

            return(await _versionSupportSemaphore.LockAsync(
                       async() => {
                try {
                    return await _configuration.ConnectionRetry.TryAsync(
                        async(retryAttempt, elapsed) => {
                        var response = await SendAsync(new ApiVersionsRequest(), cancellationToken, new RequestContext(version: 0)).ConfigureAwait(false);
                        if (response.error_code.IsRetryable())
                        {
                            return RetryAttempt <short> .Retry;
                        }
                        if (!response.error_code.IsSuccess())
                        {
                            return RetryAttempt <short> .Abort;
                        }

                        var supportedVersions = response.api_versions.ToImmutableDictionary(
                            _ => _.api_key,
                            _ => configuredSupport.UseMaxSupported ? _.max_version : _.min_version);
                        _versionSupport = new VersionSupport(supportedVersions);
                        return new RetryAttempt <short>(_versionSupport.GetVersion(apiKey).GetValueOrDefault());
                    },
                        (exception, retryAttempt, retryDelay) => {
                        _log.Debug(() => LogEvent.Create(exception, $"Failed getting version info on retry {retryAttempt}: Will retry in {retryDelay}"));
                        exception.PrepareForRethrow();
                    },
                        () => _versionSupport = _configuration.VersionSupport,     // fall back to default support in this case
                        cancellationToken);
                } catch (Exception ex) {
                    _log.Error(LogEvent.Create(ex));
                    _versionSupport = _configuration.VersionSupport;     // fall back to default support in this case
                    return (short)0;
                }
            },
                       cancellationToken
                       ).ConfigureAwait(false));
        }
Beispiel #2
0
        private async Task <short> GetVersionAsync(ApiKeyRequestType requestType, CancellationToken cancellationToken)
        {
            if (!_configuration.VersionSupport.IsDynamic)
            {
                return(_configuration.VersionSupport.GetVersion(requestType).GetValueOrDefault());
            }

            using (await _versionSupportLock.ReaderLockAsync(cancellationToken).ConfigureAwait(false)) {
                if (_versionSupport != null)
                {
                    return(_versionSupport.GetVersion(requestType).GetValueOrDefault());
                }
            }
            using (await _versionSupportLock.WriterLockAsync(cancellationToken).ConfigureAwait(false)) {
                return(await _configuration.ConnectionRetry.AttemptAsync(
                           async (attempt, timer) => {
                    var response = await SendAsync(new ApiVersionsRequest(), cancellationToken, new RequestContext(version: 0)).ConfigureAwait(false);
                    if (response.ErrorCode.IsRetryable())
                    {
                        return RetryAttempt <short> .Retry;
                    }
                    if (!response.ErrorCode.IsSuccess())
                    {
                        return RetryAttempt <short> .Abort;
                    }

                    var supportedVersions = response.SupportedVersions.ToImmutableDictionary(
                        _ => _.ApiKey,
                        _ => _.MaxVersion);
                    _versionSupport = new VersionSupport(supportedVersions);
                    return new RetryAttempt <short>(_versionSupport.GetVersion(requestType).GetValueOrDefault());
                },
                           (attempt, timer) => _log.Debug(() => LogEvent.Create($"Retrying {nameof(GetVersionAsync)} attempt {attempt}")),
                           attempt => _versionSupport = _configuration.VersionSupport,
                           exception => _log.Error(LogEvent.Create(exception)),
                           cancellationToken));
            }
        }