Example #1
0
        private async ValueTask <Response> WaitForCompletionAsync(bool async, Operation operation, TimeSpan?suggestedInterval, CancellationToken cancellationToken)
        {
            while (true)
            {
                Response response = async ? await operation.UpdateStatusAsync(cancellationToken).ConfigureAwait(false) : operation.UpdateStatus(cancellationToken);

                if (operation.HasCompleted)
                {
                    return(operation.GetRawResponse());
                }

                await Delay(async, _delayStrategy.GetNextDelay(response, suggestedInterval), cancellationToken).ConfigureAwait(false);
            }
        }
        public virtual async ValueTask <Response> WaitForCompletionResponseAsync(UpdateStatusAsync updateStatusAsync, HasCompleted hasCompleted, GetRawResponse getRawResponse, TimeSpan?suggestedInterval, CancellationToken cancellationToken)
        {
            while (true)
            {
                Response response = await updateStatusAsync(cancellationToken).ConfigureAwait(false);

                if (hasCompleted())
                {
                    return(getRawResponse());
                }

                await Task.Delay(_delayStrategy.GetNextDelay(response, suggestedInterval), cancellationToken).ConfigureAwait(false);
            }
        }
Example #3
0
        public override TimeSpan GetNextDelay(Response response, TimeSpan?suggestedInterval)
        {
            TimeSpan delay = TimeSpan.Zero;

            if (response.Headers.TryGetValue(RetryAfterMsHeaderName, out string?retryAfterValue) ||
                response.Headers.TryGetValue(XRetryAfterMsHeaderName, out retryAfterValue))
            {
                if (int.TryParse(retryAfterValue, out int serverDelayInMilliseconds))
                {
                    delay = TimeSpan.FromMilliseconds(serverDelayInMilliseconds);
                }
            }
            else if (response.Headers.TryGetValue(RetryAfterHeaderName, out retryAfterValue))
            {
                if (int.TryParse(retryAfterValue, out int serverDelayInSeconds))
                {
                    delay = TimeSpan.FromSeconds(serverDelayInSeconds);
                }
            }

            return(Max(delay, _fallbackStrategy.GetNextDelay(response, suggestedInterval)));
        }