public async Task <HttpResponseMessage> SendAsync(HttpClient client, Uri address, CancellationToken cancellationToken)
        {
            var backoff = new ExponentialBackoff(_maximumRetries, _delay, _maximumDelay);

            while (true)
            {
                HttpResponseMessage httpResponse = null;

                try
                {
                    httpResponse = await client.GetAsync(address, cancellationToken);

                    httpResponse.EnsureSuccessStatusCode();

                    return(httpResponse);
                }
                catch (Exception e)
                {
                    httpResponse?.Dispose();
                    if (IsTransientError(e, httpResponse))
                    {
                        await backoff.Delay();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Runs the function async with exponential backoff
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public async Task RunAsync(Func <Task> func)
        {
            ExponentialBackoff backoff = new ExponentialBackoff(this.Config);

            bool shouldContinue = true;

            while (shouldContinue)
            {
                try
                {
                    await func();

                    shouldContinue = false;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception raised is: {ex.GetType().ToString()} – Message: {ex.Message}");

                    if (this.Config.ExceptionHandlingLogic(ex))
                    {
                        await backoff.Delay();
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Runs the function async with exponential backoff
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public async Task <T> RunAsync <T>(Func <Task <T> > func)
        {
            ExponentialBackoff backoff = new ExponentialBackoff(this.Config);

            while (true)
            {
                try
                {
                    return(await func());
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception raised is: {ex.GetType().ToString()} – Message: {ex.Message}");

                    if (this.Config.ExceptionHandlingLogic != null && this.Config.ExceptionHandlingLogic(ex))
                    {
                        await backoff.Delay();
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
        }
 public async Task RunAsync(Func<Task> func, CancellationToken cancellationToken = default(CancellationToken))
 {
     ExponentialBackoff backoff = new ExponentialBackoff(this.maxRetries, this.delayMilliseconds, this.maxDelayMilliseconds);
     retry:
     try
     {
         await func();
     }
     catch (Exception ex) when (ex is TimeoutException || ex is FabricTransientException)
     {
         await backoff.Delay(cancellationToken);
         goto retry;
     }
 }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            backoff = new ExponentialBackoff(
                maxRetryAttempts,
                DelayMilliseconds,
                MaxDelayMilliseconds);

            while (true)
            {
                var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

                if ((int)response.StatusCode < 500)
                {
                    return(response);
                }

                await backoff.Delay().ConfigureAwait(false);
            }
        }
示例#6
0
        public async Task Reconnect()
        {
            reconnecting = true;
            ExponentialBackoff backoff = new ExponentialBackoff();

            while (reconnecting)
            {
                try
                {
                    await backoff.Delay();

                    await xmppCon.ConnectAsync(cts.Token);

                    reconnecting = false;
                }
                catch (Exception)
                {
                }
            }
        }
示例#7
0
    public async Task RunAsync(Func <Task> func)
    {
        ExponentialBackoff backoff = new ExponentialBackoff(this.maxRetries,
                                                            this.delayMilliseconds,
                                                            this.maxDelayMilliseconds);

retry:
        try
        {
            await func();
        }
        catch (Exception ex) when(ex is TimeoutException ||
                                  ex is System.Net.Http.HttpRequestException)
        {
            Debug.WriteLine("Exception raised is: " +
                            ex.GetType().ToString() +
                            " –Message: " + ex.Message +
                            " -- Inner Message: " +
                            ex.InnerException.Message);
            await backoff.Delay();

            goto retry;
        }
    }