Http.Invoker Invoker()
 {
     return
         (ExponentialBackoff(_backoff, _dispose.Token)
          (
              CatchExceptions(e => _options.Events.HttpError(e), _dispose.Token)
              (
                  Capture(_options.Events.HttpSuccess)
                      (CheckResponseValid(Send(_client, _dispose.Token))))));
 }
示例#2
0
        public void Test_Calculate()
        {
            var testData = new Dictionary <uint, int>
            {
                { 0, 1 },
                { 1, 3 },
                { 2, 7 },
                { 3, 15 },
                { 4, 31 },
                { 5, 63 },
                { 6, 127 },
                { 7, 255 },
                { 8, 500 },
                { 9, 500 }
            };

            var mockOp  = new Mock <IOperation>();
            var backoff = ExponentialBackoff.Create(100, 1, 500);

            foreach (var data in testData)
            {
                var delay = backoff.CalculateBackoff(mockOp.Object);
                Assert.Equal(data.Key, mockOp.Object.Attempts);
                Assert.Equal(TimeSpan.FromMilliseconds(data.Value), delay);
            }
        }
示例#3
0
        public void Retry_SetRetryPolicyVerifyInternals_Success()
        {
            var innerHandlerMock = Substitute.For <IDelegatingHandler>();
            var contextMock      = Substitute.For <IPipelineContext>();
            var sut = new RetryDelegatingHandler(contextMock);

            sut.ContinuationFactory = c => innerHandlerMock;

            var exponentialBackoff = new ExponentialBackoff(10, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(10));

            sut.SetRetryPolicy(exponentialBackoff);

            Assert.AreEqual(typeof(RetryDelegatingHandler.IotHubTransientErrorIgnoreStrategy), sut.internalRetryPolicy.ErrorDetectionStrategy.GetType());
            Assert.AreEqual(typeof(RetryDelegatingHandler.IotHubRuntimeOperationRetryStrategy), sut.internalRetryPolicy.RetryStrategy.GetType());
            var iotHubRuntimeOperationRetryStrategy = (RetryDelegatingHandler.IotHubRuntimeOperationRetryStrategy)sut.internalRetryPolicy.RetryStrategy;

            Assert.AreEqual(typeof(TransientFaultHandling.RetryStrategyWrapper), iotHubRuntimeOperationRetryStrategy.retryStrategy.GetType());
            Assert.AreSame(exponentialBackoff, ((TransientFaultHandling.RetryStrategyWrapper)iotHubRuntimeOperationRetryStrategy.retryStrategy).retryPolicy);

            var noretry = new NoRetry();

            sut.SetRetryPolicy(noretry);

            Assert.AreEqual(typeof(RetryDelegatingHandler.IotHubTransientErrorIgnoreStrategy), sut.internalRetryPolicy.ErrorDetectionStrategy.GetType());
            Assert.AreEqual(typeof(RetryDelegatingHandler.IotHubRuntimeOperationRetryStrategy), sut.internalRetryPolicy.RetryStrategy.GetType());
            iotHubRuntimeOperationRetryStrategy = (RetryDelegatingHandler.IotHubRuntimeOperationRetryStrategy)sut.internalRetryPolicy.RetryStrategy;
            Assert.AreEqual(typeof(TransientFaultHandling.RetryStrategyWrapper), iotHubRuntimeOperationRetryStrategy.retryStrategy.GetType());
            Assert.AreSame(noretry, ((TransientFaultHandling.RetryStrategyWrapper)iotHubRuntimeOperationRetryStrategy.retryStrategy).retryPolicy);
        }
        private static RetryManager GetDefaultRetryManager()
        {
            const int retryCount = 4;
            const int minBackoffDelayMilliseconds = 2000;
            const int maxBackoffDelayMilliseconds = 8000;
            const int deltaBackoffMilliseconds    = 2000;

            var exponentialBackoffStrategy =
                new ExponentialBackoff(
                    "exponentialBackoffStrategy",
                    retryCount,
                    TimeSpan.FromMilliseconds(minBackoffDelayMilliseconds),
                    TimeSpan.FromMilliseconds(maxBackoffDelayMilliseconds),
                    TimeSpan.FromMilliseconds(deltaBackoffMilliseconds)
                    );

            var manager = new RetryManager(
                new List <RetryStrategy>
            {
                exponentialBackoffStrategy
            },
                exponentialBackoffStrategy.Name
                );

            return(manager);
        }
        public BaseClass(string connectionStringName)
        {
            ConnectionString = CloudConfigurationManager.GetSetting(connectionStringName);
            MessagingFactory = MessagingFactory.CreateFromConnectionString(ConnectionString);
            NamespaceManager = NamespaceManager.CreateFromConnectionString(ConnectionString);
            if (_hasBeenInitialized)
            {
                return;
            }

            lock (LockThisClass)
            {
                if (_hasBeenInitialized)
                {
                    return;
                }
                var strategy = new ExponentialBackoff("ServiceBusStrategy",
                                                      RetryStrategy.DefaultClientRetryCount,
                                                      RetryStrategy.DefaultMinBackoff,
                                                      RetryStrategy.DefaultMaxBackoff,
                                                      RetryStrategy.DefaultMinBackoff);
                _retryPolicy        = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(strategy);
                _hasBeenInitialized = true;
            }
        }
示例#6
0
        public RetryDelegatingHandler(IPipelineContext context)
            : base(context)
        {
            RetryStrategy retryStrategy = new ExponentialBackoff(RetryCount, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(100));

            this.internalRetryPolicy = new RetryPolicy(new IotHubTransientErrorIgnoreStrategy(), new IotHubRuntimeOperationRetryStrategy(retryStrategy));
        }
        private static SteamClientApiClient GetSteamClientApiClient(IContext c)
        {
            var settings        = c.Kernel.Get <IDailyLeaderboardsSettings>();
            var telemetryClient = c.Kernel.Get <TelemetryClient>();
            var log             = c.Kernel.Get <ILog>();

            var userName = settings.SteamUserName;
            var password = settings.SteamPassword.Decrypt();
            var timeout  = settings.SteamClientTimeout;

            var policy = Policy
                         .Handle <Exception>(SteamClientApiClient.IsTransient)
                         .WaitAndRetryAsync(
                3,
                ExponentialBackoff.GetSleepDurationProvider(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(2)),
                (ex, duration) =>
            {
                telemetryClient.TrackException(ex);
                if (log.IsDebugEnabled)
                {
                    log.Debug($"Retrying in {duration}...", ex);
                }
            });

            return(new SteamClientApiClient(userName, password, policy, telemetryClient)
            {
                Timeout = timeout
            });
        }
示例#8
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;
                    }
                }
            }
        }
示例#9
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;
                    }
                }
            }
        }
        protected async Task SendPendingMessages()
        {
            ChadderMessage msg = null;

            try
            {
                var backoff = new ExponentialBackoff(1000, 5000);
                while (PendingMessages.Count > 0)
                {
                    if (PendingMessages.TryPeek(out msg) && msg.Status != ChadderMessage.MESSAGE_STATUS.SENT)
                    {
                        var conversation = db.GetConversation(msg.ConversationId);
                        if (await SendMessageToServer(msg, conversation) == ChadderError.OK)
                        {
                            while (PendingMessages.TryDequeue(out msg))
                            {
                                ;
                            }
                            backoff.Reset();
                        }
                        else
                        {
                            await backoff.Failed();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Insight.Track("SendPendingMessages safe try-catch");
                Insight.Report(ex);
            }
            SendPendingMessagesTask = null;
        }
        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;
                    }
                }
            }
        }
示例#12
0
        public async Task <IBoxResponse <T> > ExecuteAsync <T>(IBoxRequest request)
            where T : class
        {
            // Need to account for special cases when the return type is a stream
            bool isStream                 = typeof(T) == typeof(Stream);
            var  retryCounter             = 0;
            ExponentialBackoff expBackoff = new ExponentialBackoff();

            try
            {
                // TODO: yhu@ better handling of different request
                var isMultiPartRequest = request.GetType() == typeof(BoxMultiPartRequest);
                var isBinaryRequest    = request.GetType() == typeof(BoxBinaryRequest);

                while (true)
                {
                    HttpRequestMessage httpRequest = getHttpRequest(request, isMultiPartRequest, isBinaryRequest);
                    Debug.WriteLine(string.Format("RequestUri: {0}", httpRequest.RequestUri));
                    HttpResponseMessage response = await getResponse(request, isStream, httpRequest).ConfigureAwait(false);

                    //need to wait for Retry-After seconds and then retry request
                    var retryAfterHeader = response.Headers.RetryAfter;

                    // If we get a retryable/transient error code and this is not a multi part request (meaning a file upload, which cannot be retried
                    // because the stream cannot be reset) and we haven't exceeded the number of allowed retries, then retry the request.
                    // If we get a 202 code and has a retry-after header, we will retry after
                    if (!isMultiPartRequest &&
                        (response.StatusCode == TooManyRequests
                         ||
                         response.StatusCode == HttpStatusCode.InternalServerError
                         ||
                         response.StatusCode == HttpStatusCode.BadGateway
                         ||
                         response.StatusCode == HttpStatusCode.ServiceUnavailable
                         ||
                         response.StatusCode == HttpStatusCode.GatewayTimeout
                         ||
                         (response.StatusCode == HttpStatusCode.Accepted && retryAfterHeader != null)) &&
                        retryCounter++ < RetryLimit)
                    {
                        TimeSpan delay = expBackoff.GetRetryTimeout(retryCounter);

                        Debug.WriteLine("HttpCode : {0}. Waiting for {1} seconds to retry request. RequestUri: {2}", response.StatusCode, delay.Seconds, httpRequest.RequestUri);

                        await Task.Delay(delay);
                    }
                    else
                    {
                        BoxResponse <T> boxResponse = await getBoxResponse <T>(isStream, response).ConfigureAwait(false);

                        return(boxResponse);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("Exception: {0}", ex.Message));
                throw;
            }
        }
        /// <summary>
        /// Create an exponential backoff retry policy given a detection strategy.
        /// </summary>
        /// <param name="strategy"></param>
        /// <returns></returns>
        private static RetryPolicy Exponential(ITransientErrorDetectionStrategy strategy, int retryCount)
        {
            if (retryCount == 0)
            {
                return(RetryPolicy.NoRetry);
            }

            if (retryCount == 1)
            {
                var retryPolicy = new RetryPolicy(strategy, 1);
                retryPolicy.RetryStrategy.FastFirstRetry = true;

                return(retryPolicy);
            }

            var minBackoff   = TimeSpan.FromSeconds(1);
            var maxBackoff   = TimeSpan.FromSeconds(10);
            var deltaBackoff = TimeSpan.FromSeconds(5);

            // if retryCount is equal to Int16.MaxValue (32767)
            // then increase the backoff intervals.
            if (retryCount == Int16.MaxValue)
            {
                minBackoff   = TimeSpan.FromSeconds(1);
                maxBackoff   = TimeSpan.FromSeconds(300);
                deltaBackoff = TimeSpan.FromSeconds(10);
            }

            // 30 60 120 240

            var exponentialBackoff = new ExponentialBackoff(retryCount, minBackoff, maxBackoff, deltaBackoff);

            return(new RetryPolicy(strategy, exponentialBackoff));
        }
示例#14
0
        private async Task <string> PostAsync(DataMessage requestMessage)
        {
            WebRequest wRequest = WebRequest.Create(baseUri);

            wRequest.ContentType = "application/json";
            wRequest.Method      = WebRequestMethods.Http.Post;

            wRequest.Headers.Add($"Authorization:key={apiKey}");

            using (var reqStream = await wRequest.GetRequestStreamAsync())
                using (var streamWriter = new StreamWriter(reqStream))
                {
                    string json = JSON.SerializeDynamic(requestMessage, Options.IncludeInherited);

                    await streamWriter.WriteAsync(json);
                }

            var retryStrategy = new ExponentialBackoff(3, TimeSpan.FromSeconds(2),
                                                       TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));

            var retryPolicy = new RetryPolicy <WebExceptionDetectionStrategy>(retryStrategy);

            return(await retryPolicy.ExecuteAction(async() =>
            {
                using (var httpResponse = await wRequest.GetResponseAsync())
                    using (var responseStream = httpResponse.GetResponseStream())
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            return await streamReader.ReadToEndAsync();
                        }
            }));
        }
示例#15
0
        public AzureMessageLogWriter(CloudStorageAccount account, string tableName)
        {
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }
            if (tableName == null)
            {
                throw new ArgumentNullException("tableName");
            }
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentException("tableName");
            }

            this.account     = account;
            this.tableName   = tableName;
            this.tableClient = account.CreateCloudTableClient();
#pragma warning disable 618
            // this.tableClient.RetryPolicy = RetryPolicies.NoRetry();
#pragma warning restore 618

            var retryStrategy = new ExponentialBackoff(10, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(1));
            this.retryPolicy = new RetryPolicy <StorageTransientErrorDetectionStrategy>(retryStrategy);
            var cloudTable = tableClient.GetTableReference(tableName);
            this.retryPolicy.ExecuteAction(() => cloudTable.CreateIfNotExists());
        }
        public static async Task <NotificationSendStatus> SendChannelNotification(ChannelAccount botAccount, string serviceUrl, string channelId, string messageText, Attachment attachment)
        {
            try
            {
                var replyMessage = Activity.CreateMessageActivity();
                replyMessage.Text = messageText;

                if (attachment != null)
                {
                    replyMessage.Attachments.Add(attachment);
                }

                using (var connectorClient = new ConnectorClient(new Uri(serviceUrl)))
                {
                    var parameters = new ConversationParameters
                    {
                        Bot         = botAccount,
                        ChannelData = new TeamsChannelData
                        {
                            Channel      = new ChannelInfo(channelId),
                            Notification = new NotificationInfo()
                            {
                                Alert = true
                            }
                        },
                        IsGroup  = true,
                        Activity = (Activity)replyMessage
                    };

                    var exponentialBackoffRetryStrategy = new ExponentialBackoff(3, TimeSpan.FromSeconds(2),
                                                                                 TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));


                    // Define the Retry Policy
                    var retryPolicy = new RetryPolicy(new BotSdkTransientExceptionDetectionStrategy(), exponentialBackoffRetryStrategy);

                    //var conversationResource = await retryPolicy.ExecuteAsync(() =>
                    //                        connectorClient.Conversations.CreateConversationAsync(parameters)
                    //                        ).ConfigureAwait(false);

                    var conversationResource = await
                                               connectorClient.Conversations.CreateConversationAsync(parameters)
                    ;

                    return(new NotificationSendStatus()
                    {
                        MessageId = conversationResource.Id, IsSuccessful = true
                    });
                }
            }
            catch (Exception ex)
            {
                ErrorLogService.LogError(ex);
                return(new NotificationSendStatus()
                {
                    IsSuccessful = false, FailureMessage = ex.Message
                });
            }
        }
            public void FirstWaitShouldNotSleep()
            {
                SystemTime.ClearOverride();

                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(100));

                Assert.Equal(TimeSpan.Zero, backoff.WaitUntilRetry());
            }
        public static RetryPolicy BuildRetryPolicy()
        {
            // Define the Retry Strategy
            var retryStrategy = new ExponentialBackoff(3, TimeSpan.FromSeconds(2),
                                                       TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));

            return(new RetryPolicy(new WebExceptionDetectionStrategy(), retryStrategy));
        }
            public void FirstWaitShouldNotSleep()
            {
                SystemTime.ClearOverride();

                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(100));

                Assert.Equal(TimeSpan.Zero, backoff.WaitUntilRetry());
            }
            public void TimeoutIfCannotRetry()
            {
                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(1));

                Thread.Sleep(20);

                Assert.Throws <TimeoutException>(() => backoff.WaitOrTimeout(new Exception()));
            }
示例#21
0
        public void TestInvalidDelayTimes()
        {
            int maxRetries           = 0;
            int delayMilliseconds    = 100;
            int maxDelayMilliseconds = -1000;

            var backoff = new ExponentialBackoff(maxRetries, delayMilliseconds, maxDelayMilliseconds);
        }
示例#22
0
        private void persitTracker(TrackerData trackerData)
        {
            if (trackerData.MinResolution == Resolution.NA || trackerData.TimeSlot.Kind == DateTimeKind.Local)
            {
                trackerData.TimeSlot = trackerData.TimeSlot.ToUniversalTime();
            }

            var retryStrategy = new ExponentialBackoff(_maxRetries, TimeSpan.FromMilliseconds(_minBackoff), TimeSpan.FromSeconds(_maxBackoff), TimeSpan.FromSeconds(_deltaBackoff));
            var retryPolicy   = new RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>(retryStrategy);

            retryPolicy.Retrying += (sender, args) => onTransientErrorOccurred(sender, args, trackerData);

            using (var connection = new SqlConnection(_connectionString))
            {
                connection.OpenWithRetry(retryPolicy);
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "dbo.UpdateTracker";
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@TrackerID", SqlDbType.NVarChar);
                    command.Parameters["@TrackerID"].Value = string.Format("{0}{1}", trackerData.TypeName, trackerData.KeyFilter).Replace(" ", "");

                    command.Parameters.Add("@Name", SqlDbType.NVarChar);
                    command.Parameters["@Name"].Value = trackerData.Name;

                    command.Parameters.Add("@TypeName", SqlDbType.NVarChar);
                    command.Parameters["@TypeName"].Value = trackerData.TypeName;

                    command.Parameters.Add("@MinResolution", SqlDbType.Int);
                    command.Parameters["@MinResolution"].Value = bucketResolutionToMinutes(trackerData.MinResolution);

                    command.Parameters.Add("@KeyFilter", SqlDbType.NVarChar);
                    command.Parameters["@KeyFilter"].Value = trackerData.KeyFilter;

                    command.Parameters.Add("@TimeSlot", SqlDbType.DateTime);
                    command.Parameters["@TimeSlot"].Value = _convertToUTC ? trackerData.TimeSlot.ToUniversalTime() : trackerData.TimeSlot;

                    var flParameter = command.Parameters.AddWithValue("@FilterList", createFilterDataTable(trackerData));
                    flParameter.SqlDbType = SqlDbType.Structured;
                    flParameter.TypeName  = "dbo.FilterList";

                    var mParameter = command.Parameters.AddWithValue("@Measurement", createMeasurementDataTable(trackerData));
                    mParameter.SqlDbType = SqlDbType.Structured;
                    mParameter.TypeName  = "dbo.Measurement";

                    using (var transaction = connection.BeginTransaction())
                    {
                        command.Transaction    = transaction;
                        command.CommandTimeout = command.CommandTimeout * 2;
                        command.ExecuteNonQueryWithRetry(retryPolicy);
                        transaction.Commit();
                    }
                }
            }
        }
            public void ReturnTrueIfSystemTimeLessThanTimeout()
            {
                var now = DateTime.UtcNow;

                SystemTime.OverrideWith(() => now);

                var backoff = new ExponentialBackoff(TimeSpan.FromMinutes(1));

                Assert.True(backoff.CanRetry);
            }
            public void SleepCannotExceedTimeRemaining()
            {
                SystemTime.ClearOverride();

                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(5), TimeSpan.FromSeconds(1));

                backoff.WaitUntilRetry();

                Assert.InRange(backoff.WaitUntilRetry(), TimeSpan.FromMilliseconds(3), TimeSpan.FromMilliseconds(5));
            }
        public void Does_Not_Throw_On_Max_Retries()
        {
            var mockOp  = new Mock <IOperation>();
            var backoff = ExponentialBackoff.Create(10, 1, 500);

            mockOp.SetupGet(op => op.Attempts).Returns(1_000_000);
            var delay = backoff.CalculateBackoff(mockOp.Object);

            Assert.Equal(TimeSpan.FromMilliseconds(500), delay);
        }
示例#26
0
        public RetryPolicy CreateRetryPolicy()
        {
            var strategy = new ExponentialBackoff(
                _maxNumberOfRetries,
                _minWaitTime,
                _maxWaitTime,
                RetryStrategy.DefaultClientBackoff);

            return(new RetryPolicy <RetryExceptionPolicy>(strategy));
        }
        public LoRaDeviceClient(string devEUI, DeviceClient deviceClient)
        {
            this.devEUI       = devEUI;
            this.deviceClient = deviceClient;

            this.noRetryPolicy      = new NoRetry();
            this.exponentialBackoff = new ExponentialBackoff(int.MaxValue, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(100));

            this.SetRetry(false);
        }
示例#28
0
 public static Task <T> ScrapeWithExponentialRetries <T>(Func <int, Task <T> > scraper, int hltbId)
 {
     return(ExponentialBackoff.ExecuteAsyncWithExponentialRetries(
                () => scraper(hltbId),
                (lastException, retryCount, delay) =>
                HltbScraperEventSource.Log.TransientHltbFault(hltbId, lastException.Message, retryCount, ScrapingRetries, (int)delay.TotalSeconds),
                ex => ex is TransientHltbFaultException,
                ScrapingRetries, HttpRetryClient.MinBackoff, HttpRetryClient.MaxBackoff, HttpRetryClient.DefaultClientBackoff,
                CancellationToken.None));
 }
            public void SleepCannotExceedMaximumWait()
            {
                SystemTime.ClearOverride();

                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(5), TimeSpan.FromMilliseconds(5));

                backoff.WaitUntilRetry();

                Assert.InRange(backoff.WaitUntilRetry(), TimeSpan.FromMilliseconds(3), TimeSpan.FromMilliseconds(5));
            }
            public void ReturnTrueIfSystemTimeLessThanTimeout()
            {
                var now = DateTime.UtcNow;

                SystemTime.OverrideWith(() => now);

                var backoff = new ExponentialBackoff(TimeSpan.FromMinutes(1));

                Assert.True(backoff.CanRetry);
            }
        /// <summary>
        /// Initialize the Sql Azure settings and connections
        /// </summary>
        public void InitializeSqlAzure()
        {
            this.SqlConnectionString = ConfigurationManager.AppSettings["SqlAzureConnectionString"];
            if (String.IsNullOrWhiteSpace(this.SqlConnectionString))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "SqlAzureConnectionString");
            }

            this.SqlTableName = ConfigurationManager.AppSettings["SqlAzureTableName"];
            if (String.IsNullOrWhiteSpace(this.SqlTableName))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "SqlAzureTableName");
            }

            var columns = ConfigurationManager.AppSettings["SqlAzureTableColumns"];

            if (String.IsNullOrWhiteSpace(columns))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "SqlAzureTableColumns");
            }

            this.SqlTableColumns = columns.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(c => c.Trim()).ToList();

            //Reference: https://msdn.microsoft.com/en-us/library/azure/dn864744.aspx
            //1. Define an Exponential Backoff retry strategy for Azure SQL Database throttling (ExponentialBackoff Class). An exponential back-off strategy will gracefully back off the load on the service.
            int retryCount = 4;
            int minBackoffDelayMilliseconds = 2000;
            int maxBackoffDelayMilliseconds = 8000;
            int deltaBackoffMilliseconds    = 2000;

            ExponentialBackoff exponentialBackoffStrategy =
                new ExponentialBackoff("exponentialBackoffStrategy",
                                       retryCount,
                                       TimeSpan.FromMilliseconds(minBackoffDelayMilliseconds),
                                       TimeSpan.FromMilliseconds(maxBackoffDelayMilliseconds),
                                       TimeSpan.FromMilliseconds(deltaBackoffMilliseconds));

            //2. Set a default strategy to Exponential Backoff.
            RetryManager manager = new RetryManager(
                new List <RetryStrategy>
            {
                exponentialBackoffStrategy
            },
                "exponentialBackoffStrategy");

            //3. Set a default Retry Manager. A RetryManager provides retry functionality, or if you are using declarative configuration, you can invoke the RetryPolicyFactory.CreateDefault
            RetryManager.SetDefault(manager);

            //4. Define a default SQL Connection retry policy and SQL Command retry policy. A policy provides a retry mechanism for unreliable actions and transient conditions.
            ConnectionRetryPolicy = manager.GetDefaultSqlConnectionRetryPolicy();
            CommandRetryPolicy    = manager.GetDefaultSqlCommandRetryPolicy();

            //5. Create a function that will retry the connection using a ReliableSqlConnection.
            InitializeSqlAzureConnection();
        }
示例#32
0
 public SyncHoleService(
     IWorkerFactory workerFactory,
     ILogger <SyncHoleService> logger,
     SyncManifest syncManifest)
 {
     _logger        = logger;
     _syncManifest  = syncManifest;
     _workerFactory = workerFactory;
     _cts           = new CancellationTokenSource();
     _expBackoff    = new ExponentialBackoff(100, 60000);
 }
示例#33
0
        public static RetryPolicy MakeHttpRetryPolicy()
        {
            var retryCount         = 1;
            var minBackoff         = TimeSpan.FromSeconds(1);
            var maxBackoff         = TimeSpan.FromSeconds(2);
            var deltaBackoff       = TimeSpan.FromSeconds(5);
            var exponentialBackoff = new ExponentialBackoff(retryCount, minBackoff, maxBackoff, deltaBackoff);
            var strategy           = new HttpTransientErrorDetectionStrategy();

            return(new RetryPolicy(strategy, exponentialBackoff));
        }
            public void ReturnFalseIfSystemTimeGreaterThanTimeout()
            {
                var now = DateTime.UtcNow;

                SystemTime.OverrideWith(() => now);

                var backoff = new ExponentialBackoff(TimeSpan.FromMinutes(1));

                SystemTime.OverrideWith(() => now.AddMinutes(2));

                Assert.False(backoff.CanRetry);
            }
 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;
     }
 }
            public void WaitIfCanRetry()
            {
                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(100));

                backoff.WaitOrTimeout(new Exception());
            }
示例#37
0
 /// <summary>
 /// Constructs a new istance of the <see cref="Crawler"/> class and initializes its
 /// properties with the default values. There should be only one instance of Crawler
 /// </summary>
 public Crawler()
 {
     //first of all get a reference to the global variables because they are needed
     //in order to initialize some variables.
     globals = Globals.Instance();
     mustStop = false;
     stopping = false;
     state = CrawlerState.Stopped;
     stats = new long[10] {0,0,0,0,0,0,0,0,0,0};
     numThreads = (int)globals.Settings.ConnectionSpeed;
     runningThreads = 0;
     sendResultsThread = null;
     synchronizeThread = null;
     crawlingThreads = null;
     syncBackOff = new ExponentialBackoff(BackoffSpeed.Declining);
     downloadBackOff = new ExponentialBackoff(BackoffSpeed.Fast);
     urlsToCrawl = new Queue();
     resultFileNames = new Queue();
     crawledUrls = new ArrayList();
     queueSize = 0;
     dataFileName = String.Empty;
     defaultEncoding = Encoding.GetEncoding("ISO-8859-7");
     htmlParser = HtmlParser.Instance();
     textParser = TextParser.Instance();
     pdfParser = PdfParser.Instance();
     swfParser = SwfParser.Instance();
     robotsFilter = RobotsFilter.Instance();
     domainFilter = DomainFilter.Instance();
     hostRequestFilter =HostRequestFilter.Instance();
     hostBanFilter = HostBanFilter.Instance();
     proxy = WebServiceProxy.Instance();
 }
            public void TimeoutIfCannotRetry()
            {
                var backoff = new ExponentialBackoff(TimeSpan.FromMilliseconds(1));

                Thread.Sleep(20);

                Assert.Throws<TimeoutException>(() => backoff.WaitOrTimeout(new Exception()));
            }