44import org .slf4j .LoggerFactory ;
55
66import java .net .http .HttpResponse ;
7+ import java .util .Random ;
78
89/**
910 * Simple retry with max attempts and linear backoff.
@@ -14,16 +15,31 @@ public class SimpleRetryHandler implements RetryHandler {
1415
1516 private final int maxRetries ;
1617 private final long backoffMillis ;
18+ private final int gitterMillis ;
19+ private final Random random ;
1720
1821 /**
1922 * Create with maximum number of retries and linear backoff time.
2023 *
2124 * @param maxRetries The maximum number of retry attempts
2225 * @param backoffMillis The linear backoff between attempts in milliseconds
26+ * @param gitterMillis The maximum amount of gitter that gets added to backoffMillis
2327 */
24- public SimpleRetryHandler (int maxRetries , long backoffMillis ) {
28+ public SimpleRetryHandler (int maxRetries , int backoffMillis , int gitterMillis ) {
2529 this .maxRetries = maxRetries ;
2630 this .backoffMillis = backoffMillis ;
31+ this .gitterMillis = gitterMillis ;
32+ this .random = new Random ();
33+ }
34+
35+ /**
36+ * Create with maximum number of retries and linear backoff time and no gitter.
37+ *
38+ * @param maxRetries The maximum number of retry attempts
39+ * @param backoffMillis The linear backoff between attempts in milliseconds
40+ */
41+ public SimpleRetryHandler (int maxRetries , int backoffMillis ) {
42+ this (maxRetries , backoffMillis , 0 );
2743 }
2844
2945 @ Override
@@ -33,7 +49,8 @@ public boolean isRetry(int retryCount, HttpResponse<?> response) {
3349 }
3450 log .debug ("retry count:{} status:{} uri:{}" , retryCount , response .statusCode (), response .uri ());
3551 try {
36- Thread .sleep (backoffMillis );
52+ int gitter = gitterMillis < 1 ? 0 : random .nextInt (gitterMillis );
53+ Thread .sleep (backoffMillis + gitter );
3754 } catch (InterruptedException e ) {
3855 Thread .currentThread ().interrupt ();
3956 return false ;
0 commit comments