Last active
October 10, 2017 20:54
-
-
Save ramonsmits/26503d84b26fa8d63bd0708a01e93000 to your computer and use it in GitHub Desktop.
NServiceBus custom recoverability policy - Exponential backoff with jitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using NServiceBus; | |
| using NServiceBus.Transport; | |
| class ExponentialBackoffPolicy | |
| { | |
| static Random random = new Random(); | |
| public static RecoverabilityAction Process(RecoverabilityConfig config, ErrorContext context) | |
| { | |
| var action = DefaultRecoverabilityPolicy.Invoke(config, context); | |
| if (!(action is DelayedRetry)) return action; | |
| if (context.DelayedDeliveriesPerformed > config.Delayed.MaxNumberOfRetries) return action; | |
| double jitter; | |
| lock (random) jitter = random.NextDouble(); | |
| jitter = 1 + jitter * 0.2;// max 20% | |
| var delayInSeconds = config.Delayed.TimeIncrease.TotalSeconds; | |
| var retries = context.DelayedDeliveriesPerformed; | |
| if (retries > 0) delayInSeconds *= Math.Pow(2, retries); | |
| delayInSeconds *= jitter; | |
| return RecoverabilityAction.DelayedRetry(TimeSpan.FromSeconds(delayInSeconds)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment