This commit is contained in:
		
							
								
								
									
										26
									
								
								src/StellaOps.Zastava.Observer/Worker/BackoffCalculator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/StellaOps.Zastava.Observer/Worker/BackoffCalculator.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| using StellaOps.Zastava.Observer.Configuration; | ||||
|  | ||||
| namespace StellaOps.Zastava.Observer.Worker; | ||||
|  | ||||
| internal static class BackoffCalculator | ||||
| { | ||||
|     public static TimeSpan ComputeDelay(ObserverBackoffOptions options, int attempt, Random random) | ||||
|     { | ||||
|         ArgumentNullException.ThrowIfNull(options); | ||||
|         ArgumentNullException.ThrowIfNull(random); | ||||
|  | ||||
|         var cappedAttempt = Math.Max(1, attempt); | ||||
|         var baseDelayMs = options.Initial.TotalMilliseconds * Math.Pow(2, cappedAttempt - 1); | ||||
|         baseDelayMs = Math.Min(baseDelayMs, options.Max.TotalMilliseconds); | ||||
|  | ||||
|         if (options.JitterRatio <= 0) | ||||
|         { | ||||
|             return TimeSpan.FromMilliseconds(baseDelayMs); | ||||
|         } | ||||
|  | ||||
|         var jitterWindow = baseDelayMs * options.JitterRatio; | ||||
|         var jitter = (random.NextDouble() * 2 - 1) * jitterWindow; | ||||
|         var jittered = Math.Clamp(baseDelayMs + jitter, options.Initial.TotalMilliseconds, options.Max.TotalMilliseconds); | ||||
|         return TimeSpan.FromMilliseconds(jittered); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user