1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 package software.amazon.awssdk.crt.io;
7 
8 /**
9  * Configuration options for the exponential backoff retry strategy for http requests
10  */
11 public class ExponentialBackoffRetryOptions {
12 
13     /**
14      * What kind of jitter or randomization to apply to the backoff time interval
15      *
16      * https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
17      */
18     public enum JitterMode {
19 
20         /** Maps to Full */
21         Default(0),
22 
23         /** Do not apply any jitter or randomization to the backoff interval */
24         None(1),
25 
26         /** Choose an actual backoff interval between [0, MaxCurrentBackoff] where MaxCurrentBackoff is the standard
27          * exponential backoff value */
28         Full(2),
29 
30         /** Backoff is taken randomly from the interval between the base backoff
31          * interval and a scaling (greater than 1) of the current backoff value */
32         Decorrelated(3);
33 
34         private int value;
35 
JitterMode(int value)36         JitterMode(int value) {
37             this.value = value;
38         }
39 
getValue()40         int getValue() {
41             return value;
42         }
43     }
44 
45     private EventLoopGroup eventLoopGroup;
46     private long maxRetries;
47     private long backoffScaleFactorMS;
48     private JitterMode jitterMode;
49 
50     /**
51      * Default constructor
52      */
ExponentialBackoffRetryOptions()53     public ExponentialBackoffRetryOptions() {
54         this.jitterMode = JitterMode.Default;
55     }
56 
57     /**
58      * Configure the event loop group to use to schedule the backoff/retry tasks
59      * @param eventLoopGroup event loop group to use
60      * @return this options object
61      */
withEventLoopGroup(EventLoopGroup eventLoopGroup)62     public ExponentialBackoffRetryOptions withEventLoopGroup(EventLoopGroup eventLoopGroup) {
63         this.eventLoopGroup = eventLoopGroup;
64         return this;
65     }
66 
67     /**
68      * @return The event loop group currently configured to do backoff/retry
69      */
getEventLoopGroup()70     public EventLoopGroup getEventLoopGroup() {
71         return this.eventLoopGroup;
72     }
73 
74     /**
75      * Configure the maximum number of retries to make while using a strategy sourced from these options
76      * @param maxRetries maximum number of retries
77      * @return this options object
78      */
withMaxRetries(long maxRetries)79     public ExponentialBackoffRetryOptions withMaxRetries(long maxRetries) {
80         this.maxRetries = maxRetries;
81         return this;
82     }
83 
84     /**
85      * @return the maximum number of retries to make while using a strategy sourced from these options
86      */
getMaxRetries()87     public long getMaxRetries() {
88         return this.maxRetries;
89     }
90 
91     /**
92      * Configures the initial (base) unscaled backoff interval in milliseconds
93      * @param backoffScaleFactorMS the initial (base) unscaled backoff interval in milliseconds
94      * @return this options object
95      */
withBackoffScaleFactorMS(long backoffScaleFactorMS)96     public ExponentialBackoffRetryOptions withBackoffScaleFactorMS(long backoffScaleFactorMS) {
97         this.backoffScaleFactorMS = backoffScaleFactorMS;
98         return this;
99     }
100 
101     /**
102      * @return the initial (base) unscaled backoff interval in milliseconds while using a strategy sourced from
103      * these options
104      */
getBackoffScaleFactorMS()105     public long getBackoffScaleFactorMS() {
106         return this.backoffScaleFactorMS;
107     }
108 
109     /**
110      * Configure the type of jitter to apply to the backoff interval calculations
111      * @param jitterMode the type of jitter to apply to the backoff interval calculations
112      * @return this options object
113      */
withJitterMode(JitterMode jitterMode)114     public ExponentialBackoffRetryOptions withJitterMode(JitterMode jitterMode) {
115         this.jitterMode = jitterMode;
116         return this;
117     }
118 
119     /**
120      * @return the type of jitter to apply to the backoff interval calculations while using a strategy sourced
121      * from these options
122      */
getJitterMode()123     public JitterMode getJitterMode() {
124         return this.jitterMode;
125     }
126 }
127