Beispiel #1
0
 private bool IsRetryEligible(HandleUnsuccessfulResponseArgs args)
 {
     return(this.HandleUnsuccessfulResponseFunc != null &&
            this.HandleUnsuccessfulResponseFunc(args.Response) &&
            args.SupportsRetry &&
            args.CurrentFailedTry <= this.BackOff.MaxNumOfRetries);
 }
Beispiel #2
0
                public Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
                {
                    TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

                    tcs.SetResult(args.Response.StatusCode.Equals(HttpStatusCode.ServiceUnavailable));
                    return(tcs.Task);
                }
            public Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
            {
                TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

                tcs.SetResult(true);
                return(tcs.Task);
            }
Beispiel #4
0
            public override async Task <bool> HandleResponseAsync(
                HandleUnsuccessfulResponseArgs args)
            {
                // if the func returns true try to handle this current failed try
                if (this.IsRetryEligible(args))
                {
                    var delay = this.GetDelayFromResponse(args.Response);

                    // Retry-After header can specify very long delay intervals (e.g. 24 hours). If
                    // we cannot wait that long, we should not perform any retries at all. In
                    // general it is not correct to retry earlier than what the server has
                    // recommended to us.
                    if (delay > this.MaxTimeSpan)
                    {
                        return(false);
                    }
                    else if (delay > TimeSpan.Zero)
                    {
                        await this.Wait(delay, args.CancellationToken).ConfigureAwait(false);

                        return(true);
                    }
                }

                return(await base.HandleResponseAsync(args).ConfigureAwait(false));
            }
        /// <summary>
        /// Handles an abnormal response when sending a HTTP request.
        /// A simple rule must be followed, if you modify the request object in a way that the abnormal response can
        /// be resolved, you must return <c>true</c>.
        /// </summary>
        public async Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
        {
            if (args.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                return(await RefreshTokenAsync(args.CancellationToken).ConfigureAwait(false));
            }

            return(false);
        }
Beispiel #6
0
        public async Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
        {
            // TODO(peleyal): check WWW-Authenticate header.
            if (args.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                return(!Object.Equals(Token.AccessToken, AccessMethod.GetAccessToken(args.Request)) ||
                       await RequestAccessTokenAsync(args.CancellationToken).ConfigureAwait(false));
            }

            return(false);
        }
Beispiel #7
0
            public async Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
            {
                if (args.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    await _client.GetCurrentToken();

                    return(true);
                }

                return(false);
            }
 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
 {
     var statusCode = (int)args.Response.StatusCode;
     // handle the error if and only if all the following conditions occur:
     // - there is going to be an actual retry
     // - the message request is for media upload with the current Uri (remember that the message handler
     //   can be invoked from other threads \ messages, so we should call server error callback only if the
     //   request is in the current context).
     // - we got a 5xx server error.
     if (args.SupportsRetry && args.Request.RequestUri.Equals(Owner.UploadUri) && statusCode / 100 == 5)
     {
         return OnServerError(args.Request);
     }
     return false;
 }
        /// <summary>
        /// Decorates unsuccessful responses, returns true if the response gets modified.
        /// See IHttpUnsuccessfulResponseHandler for more information.
        /// </summary>
        public async Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
        {
            // If the response was unauthorized, request a new access token so that the original
            // request can be retried.
            // TODO(peleyal): check WWW-Authenticate header.
            if (args.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                bool tokensEqual = false;
                if (Token != null)
                {
                    tokensEqual = Object.Equals(
                        Token.AccessToken, AccessMethod.GetAccessToken(args.Request));
                }
                return(!tokensEqual ||
                       await RequestAccessTokenAsync(args.CancellationToken).ConfigureAwait(false));
            }

            return(false);
        }
        public async Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
        {
            var retry = args.SupportsRetry &&
                        await IsRetriableResponse(args.Response).ConfigureAwait(false);

            if (!retry)
            {
                return(false);
            }
            // The first failure will have args.CurrentFailedTry set to 1,
            // whereas we want the first delay to be 1 second. We use Math.Min on the power
            // rather than on the result to obtain a max retry of 32 seconds without risking
            // calling Math.Pow with a huge number.
            int    power   = Math.Min(args.CurrentFailedTry - 1, 5);
            double seconds = Math.Pow(2.0, power);
            var    delay   = TimeSpan.FromSeconds(seconds);
            await Task.Delay(delay, args.CancellationToken).ConfigureAwait(false);

            return(true);
        }
Beispiel #11
0
            /// <summary>
            /// Handler for back-off, if retry is not possible it returns <c>false</c>. Otherwise it blocks for some time (miliseconds)
            /// and returns <c>true</c>, so call is retried.
            /// </summary>
            /// <param name="args">The arguments object to handler call.</param>
            /// <returns>If request could be retried.</returns>
            public async Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
            {
                if (!args.SupportsRetry || _backoff.MaxNumOfRetries < args.CurrentFailedTry)
                {
                    return(false);
                }

                if (IsTransientError(args.Response.StatusCode, _service.DeserializeError(args.Response).Result))
                {
                    var delay = _backoff.GetNextBackOff(args.CurrentFailedTry);
                    if (delay > _maxTimeSpan || delay < TimeSpan.Zero)
                    {
                        return(false);
                    }

                    await Task.Delay(delay, args.CancellationToken);

                    Logger.Log("Back-Off waited " + delay.TotalMilliseconds + "ms before next retry...", EventType.Debug);

                    return(true);
                }

                return(false);
            }
Beispiel #12
0
 public Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args) => Task.FromResult(++Count == 0);
Beispiel #13
0
 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
 {
     return(true);
 }
 /// <inheritdoc />
 public Task <bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
 {
     logger.LogWarning($"{args.Request.RequestUri} returned status code {args.Response.StatusCode}", args);
     return(Task.FromResult(false));
 }
 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
 {
     HandleCalls++;
     // Mock a refresh token process here... (second apply will attach SecondToken authorization)
     return(true);
 }
Beispiel #16
0
 /// <summary>
 /// Override handle response to refresh the token when Unauthorized status code is received.
 /// </summary>
 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
 {
     return(args.Response.StatusCode == HttpStatusCode.Unauthorized && tokenProvider.RefreshToken(State, null));
 }
 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
 {
     return(args.Response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable);
 }
Beispiel #18
0
 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
 {
     ++Calls;
     return(args.Response.StatusCode.Equals(HttpStatusCode.ServiceUnavailable));
 }