public void starts_low() { using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) { Assert.Equal(10, sut.AvailableDegreesOfParallelism); } }
public SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, ILogger <SubscriptionReceiver> logger) { this.settings = settings; this.topic = topic; this.subscription = subscription; this.processInParallel = processInParallel; this.logger = logger; this.client = new SubscriptionClient(settings.ConnectionString, topic, subscription); if (this.processInParallel) { this.client.PrefetchCount = 18; } else { this.client.PrefetchCount = 14; } dynamicThrottling = new DynamicThrottling( maxDegreeOfParallelism: 100, minDegreeOfParallelism: 50, penaltyAmount: 3, workFailedPenaltyAmount: 5, workCompletedParallelismGain: 1, intervalForRestoringDegreeOfParallelism: 8000); receiveRetryPolicy = Policy.Handle <Exception>().WaitAndRetryAsync(3, (r) => TimeSpan.FromMilliseconds(900),//, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(1)); (ex, ts, attemps, cc) => { this.dynamicThrottling.Penalize(); logger.LogWarning($"An error occurred in attempt number {attemps} to receive a message from subscription {subscription}: {ex.Message}"); }); }
private static void IncreaseDegreesOfParallelism(DynamicThrottling sut) { for (int i = 0; i < 10; i++) { // increase degrees to avoid being in the minimum boundary sut.NotifyWorkStarted(); sut.NotifyWorkCompleted(); } }
public void increases_on_completed_work() { using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) { sut.NotifyWorkStarted(); var startingValue = sut.AvailableDegreesOfParallelism; sut.NotifyWorkCompleted(); Assert.True(startingValue < sut.AvailableDegreesOfParallelism); } }
public void decreases_on_penalize() { using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) { IncreaseDegreesOfParallelism(sut); sut.NotifyWorkStarted(); var startingValue = sut.AvailableDegreesOfParallelism; sut.Penalize(); Assert.True(startingValue > sut.AvailableDegreesOfParallelism); } }
public void penalize_decreases_less_than_completed_with_error() { using (var sut1 = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) using (var sut2 = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) { IncreaseDegreesOfParallelism(sut1); IncreaseDegreesOfParallelism(sut2); sut1.NotifyWorkStarted(); sut2.NotifyWorkStarted(); sut1.Penalize(); sut2.NotifyWorkCompletedWithError(); Assert.True(sut1.AvailableDegreesOfParallelism > sut2.AvailableDegreesOfParallelism); } }
public EventStoreBusPublisher(IMessageSender sender, IPendingEventsQueue queue, IEventStoreBusPublisherInstrumentation instrumentation) { this.sender = sender; this.queue = queue; this.instrumentation = instrumentation; this.enqueuedKeys = new BlockingCollection <string>(); this.dynamicThrottling = new DynamicThrottling( maxDegreeOfParallelism: 230, minDegreeOfParallelism: 30, penaltyAmount: 3, workFailedPenaltyAmount: 10, workCompletedParallelismGain: 1, intervalForRestoringDegreeOfParallelism: 8000); this.queue.Retrying += (s, e) => this.dynamicThrottling.Penalize(); this.sender.Retrying += (s, e) => this.dynamicThrottling.Penalize(); }
public EventStoreBusPublisher(IMessageSender sender, IPendingEventsQueue queue, IEventStoreBusPublisherInstrumentation instrumentation) { this.sender = sender; this.queue = queue; this.instrumentation = instrumentation; enqueuedKeys = new BlockingCollection <string>(); dynamicThrottling = new DynamicThrottling( 230, 30, 3, 10, 1, 8000); this.queue.Retrying += (s, e) => dynamicThrottling.Penalize(); this.sender.Retrying += (s, e) => dynamicThrottling.Penalize(); }
protected SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, ISubscriptionReceiverInstrumentation instrumentation, int maxNumberRetry, RetryStrategy backgroundRetryStrategy) { this.settings = settings; this.topic = topic; this.subscription = subscription; this.processInParallel = processInParallel; this.instrumentation = instrumentation; var messagingFactory = MessagingFactory.CreateFromConnectionString(this.settings.ConnectionString); this.client = messagingFactory.CreateSubscriptionClient(topic, subscription); if (this.processInParallel) { this.client.PrefetchCount = 500; } else { this.client.PrefetchCount = 100; } this.dynamicThrottling = new DynamicThrottling( maxDegreeOfParallelism: 10000, minDegreeOfParallelism: 50, penaltyAmount: 3, workFailedPenaltyAmount: 5, workCompletedParallelismGain: 1, intervalForRestoringDegreeOfParallelism: 8000); this.retryStrategy = backgroundRetryStrategy; this.maxNumberRetry = maxNumberRetry; this.receiveRetryPolicy = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy); this.receiveRetryPolicy.Retrying += (s, e) => { this.dynamicThrottling.Penalize(); Trace.TraceWarning( "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}", e.LastException.Message, e.CurrentRetryCount, this.subscription); }; }
/// <summary> /// Initializes a new instance of the <see cref="SessionSubscriptionReceiver"/> class, /// automatically creating the topic and subscription if they don't exist. /// </summary> protected SessionSubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool requiresSequentialProcessing, ISessionSubscriptionReceiverInstrumentation instrumentation, RetryStrategy backgroundRetryStrategy) { this.settings = settings; this.topic = topic; this.subscription = subscription; this.requiresSequentialProcessing = requiresSequentialProcessing; this.instrumentation = instrumentation; this.tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey); this.serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath); var messagingFactory = MessagingFactory.Create(this.serviceUri, tokenProvider); this.client = messagingFactory.CreateSubscriptionClient(topic, subscription); if (this.requiresSequentialProcessing) { this.client.PrefetchCount = 10; } else { this.client.PrefetchCount = 15; } this.dynamicThrottling = new DynamicThrottling( maxDegreeOfParallelism: 160, minDegreeOfParallelism: 30, penaltyAmount: 3, workFailedPenaltyAmount: 5, workCompletedParallelismGain: 1, intervalForRestoringDegreeOfParallelism: 10000); this.receiveRetryPolicy = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy); this.receiveRetryPolicy.Retrying += (s, e) => { this.dynamicThrottling.Penalize(); Trace.TraceWarning( "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}", e.LastException.Message, e.CurrentRetryCount, this.subscription); }; }
/// <summary> /// Initializes a new instance of the <see cref="SubscriptionReceiver" /> class, /// automatically creating the topic and subscription if they don't exist. /// </summary> protected SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, ISubscriptionReceiverInstrumentation instrumentation, RetryStrategy backgroundRetryStrategy) { this.settings = settings; this.topic = topic; this.subscription = subscription; this.processInParallel = processInParallel; this.instrumentation = instrumentation; tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey); serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath); var messagingFactory = MessagingFactory.Create(serviceUri, tokenProvider); client = messagingFactory.CreateSubscriptionClient(topic, subscription); if (this.processInParallel) { client.PrefetchCount = 18; } else { client.PrefetchCount = 14; } dynamicThrottling = new DynamicThrottling( 100, 50, 3, 5, 1, 8000); receiveRetryPolicy = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy); receiveRetryPolicy.Retrying += (s, e) => { dynamicThrottling.Penalize(); Trace.TraceWarning( "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}", e.LastException.Message, e.CurrentRetryCount, this.subscription); }; }
/// <summary> /// Initializes a new instance of the <see cref="SessionSubscriptionReceiver"/> class, /// automatically creating the topic and subscription if they don't exist. /// </summary> public SessionSubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool requiresSequentialProcessing, ILogger <SessionSubscriptionReceiver> logger) { this.settings = settings; this.topic = topic; this.subscription = subscription; this.requiresSequentialProcessing = requiresSequentialProcessing; this.logger = logger; client = new SubscriptionClient(settings.ConnectionString, topic, subscription, ReceiveMode.PeekLock, Microsoft.Azure.ServiceBus.RetryPolicy.Default); if (this.requiresSequentialProcessing) { this.client.PrefetchCount = 10; } else { this.client.PrefetchCount = 15; } this.dynamicThrottling = new DynamicThrottling( maxDegreeOfParallelism: 160, minDegreeOfParallelism: 30, penaltyAmount: 3, workFailedPenaltyAmount: 5, workCompletedParallelismGain: 1, intervalForRestoringDegreeOfParallelism: 10000); this.receiveRetryPolicy = Policy.Handle <Exception>() .WaitAndRetryAsync(10, retry => TimeSpan.FromSeconds(Math.Pow(2, retry)), (ex, ts, attempt, context) => { this.dynamicThrottling.Penalize(); logger.LogWarning($"An error occurred in attempt number {attempt} to receive a message from subscription {subscription}: {ex.Message}"); }); }
/// <summary> /// Initializes a new instance of the <see cref="SessionSubscriptionReceiver"/> class, /// automatically creating the topic and subscription if they don't exist. /// </summary> protected SessionSubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool requiresSequentialProcessing, ISessionSubscriptionReceiverInstrumentation instrumentation, RetryStrategy backgroundRetryStrategy) { this.settings = settings; this.topic = topic; this.subscription = subscription; this.requiresSequentialProcessing = requiresSequentialProcessing; this.instrumentation = instrumentation; this.tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey); this.serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath); var messagingFactory = MessagingFactory.Create(this.serviceUri, tokenProvider); this.client = messagingFactory.CreateSubscriptionClient(topic, subscription); if (this.requiresSequentialProcessing) { this.client.PrefetchCount = 10; } else { this.client.PrefetchCount = 15; } this.dynamicThrottling = new DynamicThrottling( maxDegreeOfParallelism: 160, minDegreeOfParallelism: 30, penaltyAmount: 3, workFailedPenaltyAmount: 5, workCompletedParallelismGain: 1, intervalForRestoringDegreeOfParallelism: 10000); this.receiveRetryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy); this.receiveRetryPolicy.Retrying += (s, e) => { this.dynamicThrottling.Penalize(); Trace.TraceWarning( "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}", e.LastException.Message, e.CurrentRetryCount, this.subscription); }; }