public OrdersClientController()
        {
            var client = new Client();
            var services = client.Agent.Services().Response;
            foreach (var service in services)
            {
                bool isOrderService = service.Value.Tags.Any(tag => tag == "Orders");
                if (isOrderService)
                {
                    var server = new Server()
                    {
                        Timeout = 5000,
                        Uri = new Uri(string.Format("{0}:{1}/", service.Value.Address, service.Value.Port))
                    };
                    _serverList.Add(server);
                }
            }

            _retryPolicy = Policy
                .Handle<AggregateException>()
                .Or<Exception>()
                .WaitAndRetry(new[]
                {
                    TimeSpan.FromMilliseconds(50),
                    TimeSpan.FromMilliseconds(150),
                    TimeSpan.FromMilliseconds(250),
                }, (exception, timespan) =>
                {
                    LogError(exception);
                    TrySelectNextServer();
                });

            TrySelectNextServer();
        }
        internal CircuitBreakingDelegatingHandler(int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak)
        {
            this.exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
            this.durationOfBreak = durationOfBreak;

            circuitBreakerPolicy = Policy.Handle<HttpRequestException>().CircuitBreaker(exceptionsAllowedBeforeBreaking, durationOfBreak);
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes from attribute parameters. This will get the <see cref="IAmAPolicyRegistry"/> from the <see cref="IRequestContext"/> and query it for the
        /// policy identified in <see cref="UsePolicyAttribute"/>
        /// </summary>
        /// <param name="initializerList">The initializer list.</param>
        /// <exception cref="System.ArgumentException">Could not find the policy for this attribute, did you register it with the command processor's container;initializerList</exception>
        public override void InitializeFromAttributeParams(params object[] initializerList)
        {
            //we expect the first and only parameter to be a string
            var policyName = (string)initializerList[0];

            _policy = Context.Policies.Get(policyName);
        }
Esempio n. 4
0
        public async Task <IList <ExtractedFile> > GetExtractedFiles(int expectedDocumentCount)
        {
            var req = new SearchRequest <ExtractedFile>(_indexName)
            {
                Query = EverythingQuery(),
                Size  = 1000 //Limit of max results returned at a time
            };

            return(await Policy
                   .HandleResult <IList <ExtractedFile> >(

                       /*
                        * This will retry when the condition is FALSE,
                        * but it won't throw an exception if it stays FALSE after all retries.
                        * This is what we want here, because we want to retry in the case that only
                        * 20/30 docs have been processed by the ingest pipeline so far.
                        * But some docs (those with passwords) will fail in the pipeline and will never be returned.
                        * So we may never get 30/30 back, but we should pause for a few seconds and give ES a chance to catch up.
                        */
                       files => files.Count != expectedDocumentCount
                       )
                   .WaitAndRetryAsync(
                       new[] { 1, 3, 5 }
                       .Select(n => TimeSpan.FromSeconds(n))
                       )
                   .ExecuteAsync(async() =>
            {
                var resp = await _esClient.SearchAsync <ExtractedFile>(req);
                var docs = resp.Documents.ToList();
                return docs;
            }));
        }
Esempio n. 5
0
 protected internal Broker(EnvironmentConfiguration configuration, IConnectionBuilder connectionBuilder)
 {
     _configuration = configuration;
     _connectionBuilder = connectionBuilder;
     _retryPolicy = _connectionBuilder.RetryPolicy;
     _circuitBreakerPolicy = _connectionBuilder.CircuitBreakerPolicy;
 }
        public OrdersClientController()
        {
            var gatewayConfiguration = HttpGatewayConfiguration.GetConfiguration();
            foreach (OrderAPIServerElement serverDefinition in gatewayConfiguration.OrderServiceConfiguration.Servers)
                _serverList.Add(
                    new Server()
                    {
                        Uri = serverDefinition.Uri,
                        Timeout = Convert.ToDouble(serverDefinition.Timeout)
                    }
                );

            _retryPolicy = Policy
                .Handle<AggregateException>()
                .Or<Exception>()
                .WaitAndRetry(new[]
                {
                    TimeSpan.FromMilliseconds(50),
                    TimeSpan.FromMilliseconds(150),
                    TimeSpan.FromMilliseconds(250),
                }, (exception, timespan) =>
                {
                    LogError(exception);
                    TrySelectNextServer();
                });

            TrySelectNextServer();
        }
        private static async void SendReadToElastic(IExecutionSegment transaction, ElasticDocument message)
        {
            var span =
                transaction.StartSpan("Send message to Elastic", "Elastic");

            try
            {
                var indexSpan = span.StartSpan("Index to Elastic", ApiConstants.TypeDb);
                try
                {
                    var res = await Policy
                              .Handle <ElasticsearchClientException>()
                              .WaitAndRetryAsync(5, i => TimeSpan.FromSeconds(3))
                              .ExecuteAsync(() => ElasticClient.IndexAsync(message,
                                                                           idx => idx.Index(Environment.GetEnvironmentVariable("Elastic_Index_Name"))));
                }
                catch (Exception e)
                {
                    ElasticClient.Index(message, idx => idx.Index($"proof-reads-{Today}"));
                    indexSpan.CaptureException(e, "Index to Elastic");
                }
                finally
                {
                    indexSpan.End();
                }
            }
            catch (Exception e)
            {
                span.CaptureException(e, "SendDataToElastic");
            }
            finally
            {
                span.End();
            }
        }
Esempio n. 8
0
        public TestEngineService(
            ICacheProvider cacheProvider,
            ISpecificationRepository specificationRepository,
            ILogger logger,
            ITestEngine testEngine,
            IScenariosRepository scenariosRepository,
            IProviderSourceDatasetsRepository providerSourceDatasetsRepository,
            ITestResultsService testResultsService,
            ITestResultsRepository testResultsRepository,
            IBuildProjectRepository buildProjectRepository,
            ITelemetry telemetry,
            ITestRunnerResiliencePolicies resiliencePolicies,
            ICalculationsRepository calculationsRepository)
        {
            _cacheProvider           = cacheProvider;
            _specificationRepository = specificationRepository;
            _logger              = logger;
            _testEngine          = testEngine;
            _scenariosRepository = scenariosRepository;
            _providerSourceDatasetsRepository = providerSourceDatasetsRepository;
            _testResultsService    = testResultsService;
            _testResultsRepository = testResultsRepository;
            _telemetry             = telemetry;

            _cacheProviderPolicy                    = resiliencePolicies.CacheProviderRepository;
            _specificationRepositoryPolicy          = resiliencePolicies.SpecificationRepository;
            _scenariosRepositoryPolicy              = resiliencePolicies.ScenariosRepository;
            _providerSourceDatasetsRepositoryPolicy = resiliencePolicies.ProviderSourceDatasetsRepository;
            _testResultsRepositoryPolicy            = resiliencePolicies.TestResultsRepository;
            _builProjectsRepositoryPolicy           = resiliencePolicies.BuildProjectRepository;
            _buildProjectRepository                 = buildProjectRepository;
            _calculationsRepository                 = calculationsRepository;
        }
        public CircuitBreakingDelegatingHandler(int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, HttpMessageHandler innerHandler)
            : base(innerHandler)
        {
            this.exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
            this.durationOfBreak = durationOfBreak;

            circuitBreakerPolicy = Policy.Handle<HttpRequestException>().CircuitBreaker(exceptionsAllowedBeforeBreaking, durationOfBreak);
        }
Esempio n. 10
0
 public RetryPolicyHandler(LoggingSettings loggingSettings, RetryPolicySettings settings, HttpMessageHandler inner) 
     : base(inner)
 {
     _loggingSettings = loggingSettings;
     _settings = settings;
     _retryPolicy = Policy
     .Handle<TransientHttpRequestException>()
     .WaitAndRetryAsync(settings.RetryPolicyIntervals);
 }
Esempio n. 11
0
        public PollyWebClient()
        {
            this._webClient = new WebClient();

            this._policy = Policy
                .Handle<WebException>()
                .WaitAndRetry(3, retryAttempt =>
                    TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                );
        }
        public CircuitBreakerPolicyHandler(LoggingSettings loggingSettings, CircuitBreakerSettings settings, HttpMessageHandler inner)
            : base(inner)
        {
            _loggingSettings = loggingSettings;
            _circuitBreakerEnabled = settings.CircuitBreakerEnabled;

            _circuitBreaker = Policy
            .Handle<Exception>()
            .CircuitBreakerAsync(settings.ExceptionAllowedBeforeCircuitBroken, TimeSpan.FromMilliseconds(settings.DurationOfCircuitBreakMiliseconds));
        }
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Object"/> class.
 /// </summary>
 public Consumer(ILastReadFeedItemDAO lastReadFeedItemDao, IAmACommandProcessor commandProcessor, ILog logger)
 {
     _lastReadFeedItemDao = lastReadFeedItemDao;
     _commandProcessor = commandProcessor;
     _logger = logger;
     _atomFeedGateway = new AtomFeedGateway(_lastReadFeedItemDao, _logger);
     _retryPolicy = Policy
         .Handle<ApplicationException>()
         .RetryForever(exception => logger.WarnException("Error connecting to the server - will retry", exception));
 }
Esempio n. 14
0
        protected PolicyResult RetryAction(Action action, int timeoutInSeconds = 30, int retryIntervalInSeconds = 1)
        {
            Guard.NotNull(action, nameof(action), "Requires disposing function to be retried");
            Guard.NotLessThan(timeoutInSeconds, 0, nameof(timeoutInSeconds), "Requires a timeout (in sec) greater than zero");
            Guard.NotLessThan(retryIntervalInSeconds, 0, nameof(retryIntervalInSeconds), "Requires a retry interval (in sec) greater than zero");

            return(Policy.Timeout(TimeSpan.FromSeconds(timeoutInSeconds))
                   .Wrap(Policy.Handle <Exception>()
                         .WaitAndRetryForever(index => TimeSpan.FromSeconds(retryIntervalInSeconds)))
                   .ExecuteAndCapture(action));
        }
        public virtual async Task <BulkResponse> BulkAsync(IBulkRequest request, string callerName = "")
        {
            var timer  = Stopwatch.StartNew();
            var policy = Policy
                         .Handle <TooManyRequestsException>()
                         .WaitAndRetry(5, retryAttempt =>
                                       TimeSpan.FromSeconds(Math.Pow(2, retryAttempt) * 10));

            var result = await policy.Execute(() => BulkData(request));

            SendLog(null, null, timer.ElapsedMilliseconds, $"Elasticsearch.BulkAsync.{callerName}");
            return(result);
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageGateway"/> class.
        /// Use if you need to inject a test logger
        /// <param name="connection">The amqp uri and exchange to connect to</param>
        /// </summary>
        protected MessageGateway(RmqMessagingGatewayConnection connection)
        {
            Connection = connection;

            var connectionPolicyFactory = new ConnectionPolicyFactory(Connection);

            _retryPolicy = connectionPolicyFactory.RetryPolicy;
            _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy;

            _connectionFactory = new ConnectionFactory { Uri = Connection.AmpqUri.Uri.ToString(), RequestedHeartbeat = 30 };

            DelaySupported = this is IAmAMessageGatewaySupportingDelay && Connection.Exchange.SupportDelay;
        }
Esempio n. 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageGateway"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        public MessageGateway(ILog logger)
        {
            Logger = logger;
            Configuration = RMQMessagingGatewayConfigurationSection.GetConfiguration();

            var connectionPolicyFactory = new ConnectionPolicyFactory(logger);

            _retryPolicy = connectionPolicyFactory.RetryPolicy;
            _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy;

            _connectionFactory = new ConnectionFactory { Uri = Configuration.AMPQUri.Uri.ToString(), RequestedHeartbeat = 30 };

            DelaySupported = (this is IAmAMessageGatewaySupportingDelay) && Configuration.Exchange.SupportDelay;
        }
        public ForecastGateway(IMessageQueue messageQueue) : base("Weather")
        {
            _messageQueue = messageQueue;
            _apiKey = ConfigurationManager.AppSettings["forecast.apikey"];
            _canCreateDevices = true;

            _policy = Policy
                .Handle<WebException>()
                .WaitAndRetry(new[]
                {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(2),
                    TimeSpan.FromSeconds(5)
                });
        }
        public ISearchResponse <T> Search <T>(Func <SearchDescriptor <T>, ISearchRequest> selector, [CallerMemberName] string callerName = "")
            where T : class
        {
            var timer  = Stopwatch.StartNew();
            var policy = Policy
                         .Handle <FailedToPingSpecifiedNodeException>()
                         .WaitAndRetry(5, retryAttempt =>
                                       TimeSpan.FromSeconds(Math.Pow(2, retryAttempt) * 10));

            var result = policy.Execute(() => SearchData(selector));

            ValidateResponse(result);
            SendLog(result.ApiCall, result.Took, timer.ElapsedMilliseconds, $"Elasticsearch.Search.{callerName}");
            return(result);
        }
        internal InfluxDbHttpTransport(Uri uri, string username, string password, ConfigOptions config)
        {
            _uri = uri;
            _config = config;

            var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password));

            _client = new HttpClient()
            {
                DefaultRequestHeaders =
                {
                    Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray))
                }
            };

            _policy = new Rate(_config.BreakerRate).AsPolicy();
        }
Esempio n. 21
0
 public DroidHttpErrorManager()
 {
     _droidHttpHandlePolicy = Policy
             .Handle<System.Net.WebException> (webEx =>
                 webEx.Status == System.Net.WebExceptionStatus.ConnectFailure
     || webEx.Status == System.Net.WebExceptionStatus.SendFailure
     || webEx.Status == System.Net.WebExceptionStatus.UnknownError
     || webEx.Status == System.Net.WebExceptionStatus.ConnectionClosed
     || webEx.Status == System.Net.WebExceptionStatus.KeepAliveFailure
     || webEx.Status == System.Net.WebExceptionStatus.PipelineFailure
     || webEx.Status == System.Net.WebExceptionStatus.ReceiveFailure
     || webEx.Status == System.Net.WebExceptionStatus.SecureChannelFailure
     || webEx.Status == System.Net.WebExceptionStatus.TrustFailure)
             .Or<Exception> (ex =>
                 ex.Message.IndexOf ("Bad file descriptor", StringComparison.OrdinalIgnoreCase) != -1
     || ex.Message.IndexOf ("Invalid argument", StringComparison.OrdinalIgnoreCase) != -1)
             .RetryAsync (3, LogRetryException);
 }
 public RetryingDelegatingHandler(int retryCount, Func<int, TimeSpan> sleepDurations, HttpMessageHandler innerHandler)
     : base(innerHandler)
 {
     this.retryCount = retryCount;
     retryPolicy = Policy.Handle<HttpRequestException>().Or<HttpRequestTimeoutException>().WaitAndRetry(retryCount, sleepDurations);
 }
 internal RetryingDelegatingHandler(int retryCount, Func<int, TimeSpan> sleepDurations)
 {
     this.retryCount = retryCount;
     retryPolicy = Policy.Handle<HttpRequestException>().Or<HttpRequestTimeoutException>().WaitAndRetry(retryCount, sleepDurations);
 }
 internal RetryingDelegatingHandler(int retryCount)
 {
     this.retryCount = retryCount;
     retryPolicy = Policy.Handle<HttpRequestException>().Or<HttpRequestTimeoutException>().Retry(retryCount);
 }
 public RetryingDelegatingHandler(int retryCount, HttpMessageHandler innerHandler)
     : base(innerHandler)
 {
     this.retryCount = retryCount;
     retryPolicy = Policy.Handle<HttpRequestException>().Or<HttpRequestTimeoutException>().Retry(retryCount);
 }