Debian Codegraph @

crt/aws-c-io/include/aws/io/retry_strategy.h

raw symbols SCIP
1 #ifndef AWS_IO_CLIENT_RETRY_STRATEGY_H
2 #define AWS_IO_CLIENT_RETRY_STRATEGY_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/io/exports.h>
8
9 #include <aws/common/atomics.h>
10 #include <aws/common/byte_buf.h>
11
12 AWS_PUSH_SANE_WARNING_LEVEL
13
14 struct aws_retry_strategy;
15 struct aws_retry_token;
16 struct aws_event_loop_group;
17
18 /**
19 * Invoked upon the acquisition, or failure to acquire a retry token. This function will always be invoked if and only
20 * if aws_retry_strategy_acquire_retry_token() returns AWS_OP_SUCCESS. It will never be invoked synchronously from
21 * aws_retry_strategy_acquire_retry_token(). Token will always be NULL if error_code is non-zero, and vice-versa. If
22 * token is non-null, it will have a reference count of 1, and you must call aws_retry_token_release() on it later. See
23 * the comments for aws_retry_strategy_on_retry_ready_fn for more info.
24 */
25 typedef void(aws_retry_strategy_on_retry_token_acquired_fn)(
26 struct aws_retry_strategy *retry_strategy,
27 int error_code,
28 struct aws_retry_token *token,
29 void *user_data);
30
31 /**
32 * Invoked after a successful call to aws_retry_strategy_schedule_retry(). This function will always be invoked if and
33 * only if aws_retry_strategy_schedule_retry() returns AWS_OP_SUCCESS. It will never be invoked synchronously from
34 * aws_retry_strategy_schedule_retry(). After attempting the operation, either call aws_retry_strategy_schedule_retry()
35 * with an aws_retry_error_type or call aws_retry_token_record_success() and then release the token via.
36 * aws_retry_token_release().
37 */
38 typedef void(aws_retry_strategy_on_retry_ready_fn)(struct aws_retry_token *token, int error_code, void *user_data);
39
40 /**
41 * Optional function to supply your own generate random implementation
42 */
43 typedef uint64_t(aws_generate_random_fn)(void *user_data);
44
45 enum aws_retry_error_type {
46 /** This is a connection level error such as a socket timeout, socket connect error, tls negotiation timeout etc...
47 * Typically these should never be applied for non-idempotent request types since in this scenario, it's impossible
48 * to know whether the operation had a side effect on the server. */
49 AWS_RETRY_ERROR_TYPE_TRANSIENT,
50 /** This is an error where the server explicitly told the client to back off, such as a 429 or 503 Http error. */
51 AWS_RETRY_ERROR_TYPE_THROTTLING,
52 /** This is a server error that isn't explicitly throttling but is considered by the client
53 * to be something that should be retried. */
54 AWS_RETRY_ERROR_TYPE_SERVER_ERROR,
55 /** Doesn't count against any budgets. This could be something like a 401 challenge in Http. */
56 AWS_RETRY_ERROR_TYPE_CLIENT_ERROR,
57 };
58
59 struct aws_retry_strategy_vtable {
60 void (*destroy)(struct aws_retry_strategy *retry_strategy);
61 int (*acquire_token)(
62 struct aws_retry_strategy *retry_strategy,
63 const struct aws_byte_cursor *partition_id,
64 aws_retry_strategy_on_retry_token_acquired_fn *on_acquired,
65 void *user_data,
66 uint64_t timeout_ms);
67 int (*schedule_retry)(
68 struct aws_retry_token *token,
69 enum aws_retry_error_type error_type,
70 aws_retry_strategy_on_retry_ready_fn *retry_ready,
71 void *user_data);
72 int (*record_success)(struct aws_retry_token *token);
73 void (*release_token)(struct aws_retry_token *token);
74 };
75
76 struct aws_retry_strategy {
77 struct aws_allocator *allocator;
78 struct aws_retry_strategy_vtable *vtable;
79 struct aws_atomic_var ref_count;
80 void *impl;
81 };
82
83 struct aws_retry_token {
84 struct aws_allocator *allocator;
85 struct aws_retry_strategy *retry_strategy;
86 struct aws_atomic_var ref_count;
87 void *impl;
88 };
89
90 /**
91 * Jitter mode for exponential backoff.
92 *
93 * For a great writeup on these options see:
94 * https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
95 */
96 enum aws_exponential_backoff_jitter_mode {
97 /* Uses AWS_EXPONENTIAL_BACKOFF_JITTER_FULL */
98 AWS_EXPONENTIAL_BACKOFF_JITTER_DEFAULT,
99 AWS_EXPONENTIAL_BACKOFF_JITTER_NONE,
100 AWS_EXPONENTIAL_BACKOFF_JITTER_FULL,
101 AWS_EXPONENTIAL_BACKOFF_JITTER_DECORRELATED,
102 };
103
104 /**
105 * Options for exponential backoff retry strategy. el_group must be set, any other option, if set to 0 will signify
106 * "use defaults"
107 */
108 struct aws_exponential_backoff_retry_options {
109 /* Event loop group to use for scheduling tasks. */
110 struct aws_event_loop_group *el_group;
111 /* Max retries to allow. The default value is 10 */
112 size_t max_retries;
113 /* Scaling factor to add for the backoff. Default is 500ms */
114 uint32_t backoff_scale_factor_ms;
115 /* Max retry backoff in seconds. Default is 20 seconds */
116 uint32_t max_backoff_secs;
117 /** Jitter mode to use, see comments for aws_exponential_backoff_jitter_mode.
118 * Default is AWS_EXPONENTIAL_BACKOFF_JITTER_DEFAULT */
119 enum aws_exponential_backoff_jitter_mode jitter_mode;
120
121 /** Deprecated. Use generate_random_impl instead
122 * By default this will be set to use aws_device_random. If you want something else, set it here.
123 * */
124 uint64_t (*generate_random)(void);
125
126 /*
127 * By default this will be set to use aws_device_random. If you want something else, set it here.
128 */
129 aws_generate_random_fn *generate_random_impl;
130 /**
131 * Optional user data for the generate random generate_random_impl.
132 */
133 void *generate_random_user_data;
134
135 /**
136 * Optional shutdown callback that gets invoked, with appropriate user data,
137 * when the resources used by the retry_strategy are no longer in use.
138 */
139 const struct aws_shutdown_callback_options *shutdown_options;
140 };
141
142 struct aws_no_retry_options {
143 /**
144 * Optional shutdown callback that gets invoked, with appropriate user data,
145 * when the resources used by the retry_strategy are no longer in use.
146 */
147 const struct aws_shutdown_callback_options *shutdown_options;
148 };
149
150 struct aws_standard_retry_options {
151 struct aws_exponential_backoff_retry_options backoff_retry_options;
152 /** capacity for partitions. Defaults to 500 */
153 size_t initial_bucket_capacity;
154 };
155
156 AWS_EXTERN_C_BEGIN
157 /**
158 * Acquire a reference count on retry_strategy.
159 */
160 AWS_IO_API void aws_retry_strategy_acquire(struct aws_retry_strategy *retry_strategy);
161 /**
162 * Releases a reference count on retry_strategy.
163 */
164 AWS_IO_API void aws_retry_strategy_release(struct aws_retry_strategy *retry_strategy);
165 /**
166 * Attempts to acquire a retry token for use with retries. On success, on_acquired will be invoked when a token is
167 * available, or an error will be returned if the timeout expires. partition_id identifies operations that should be
168 * grouped together. This allows for more sophisticated strategies such as AIMD and circuit breaker patterns. Pass NULL
169 * to use the global partition.
170 */
171 AWS_IO_API int aws_retry_strategy_acquire_retry_token(
172 struct aws_retry_strategy *retry_strategy,
173 const struct aws_byte_cursor *partition_id,
174 aws_retry_strategy_on_retry_token_acquired_fn *on_acquired,
175 void *user_data,
176 uint64_t timeout_ms);
177
178 /**
179 * Schedules a retry based on the backoff and token based strategies. retry_ready is invoked when the retry is either
180 * ready for execution or if it has been canceled due to application shutdown.
181 *
182 * This function can return an error to reject the retry attempt if, for example, a circuit breaker has opened. If this
183 * occurs users should fail their calls back to their callers.
184 *
185 * error_type is used for book keeping. See the comments above for aws_retry_error_type.
186 */
187 AWS_IO_API int aws_retry_strategy_schedule_retry(
188 struct aws_retry_token *token,
189 enum aws_retry_error_type error_type,
190 aws_retry_strategy_on_retry_ready_fn *retry_ready,
191 void *user_data);
192 /**
193 * Records a successful retry. This is used for making future decisions to open up token buckets, AIMD breakers etc...
194 * some strategies such as exponential backoff will ignore this, but you should always call it after a successful
195 * operation or your system will never recover during an outage.
196 */
197 AWS_IO_API int aws_retry_token_record_success(struct aws_retry_token *token);
198
199 /**
200 * Increments reference count for token. This should be called any time you seat the token to a pointer you own.
201 */
202 AWS_IO_API void aws_retry_token_acquire(struct aws_retry_token *token);
203
204 /**
205 * Releases the reference count for token. This should always be invoked after either calling
206 * aws_retry_strategy_schedule_retry() and failing, or after calling aws_retry_token_record_success().
207 */
208 AWS_IO_API void aws_retry_token_release(struct aws_retry_token *token);
209 /**
210 * Creates a retry strategy using exponential backoff. This strategy does not perform any bookkeeping on error types and
211 * success. There is no circuit breaker functionality in here. See the comments above for
212 * aws_exponential_backoff_retry_options.
213 */
214 AWS_IO_API struct aws_retry_strategy *aws_retry_strategy_new_exponential_backoff(
215 struct aws_allocator *allocator,
216 const struct aws_exponential_backoff_retry_options *config);
217
218 /**
219 * This is a retry implementation that cuts off traffic if it's
220 * detected that an endpoint partition is having availability
221 * problems. This is necessary to keep from making outages worse
222 * by scheduling work that's unlikely to succeed yet increases
223 * load on an already ailing system.
224 *
225 * We do this by creating a bucket for each partition. A partition
226 * is an arbitrary specifier. It can be anything: a region, a service,
227 * a combination of region and service, a literal dns name.... doesn't matter.
228 *
229 * Each bucket has a budget for maximum allowed retries. Different types of events
230 * carry different weights. Things that indicate an unhealthy partition such as
231 * transient errors (timeouts, unhealthy connection etc...) cost more.
232 * A retry for any other reason (service sending a 5xx response code) cost a bit less.
233 * When a retry is attempted this capacity is leased out to the retry. On success it is
234 * released back to the capacity pool. On failure, it remains leased.
235 * Operations that succeed without a retry slowly restore the capacity pool.
236 *
237 * If a partition runs out of capacity it is assumed unhealthy and retries will be blocked
238 * until capacity returns to the pool. To prevent a partition from staying unhealthy after
239 * an outage has recovered, new requests that succeed without a retry will increase the capacity
240 * slowly ( a new request gets a payback lease of 1, but the lease is never actually deducted from the capacity pool).
241 */
242 AWS_IO_API struct aws_retry_strategy *aws_retry_strategy_new_standard(
243 struct aws_allocator *allocator,
244 const struct aws_standard_retry_options *config);
245
246 /**
247 * This retry strategy is used to disable retries. Passed config can be null.
248 * Calling `aws_retry_strategy_acquire_retry_token` will raise error `AWS_IO_RETRY_PERMISSION_DENIED`.
249 * Calling any function apart from the `aws_retry_strategy_acquire_retry_token` and `aws_retry_strategy_release` will
250 * result in a fatal error.
251 */
252 AWS_IO_API struct aws_retry_strategy *aws_retry_strategy_new_no_retry(
253 struct aws_allocator *allocator,
254 const struct aws_no_retry_options *config);
255
256 AWS_EXTERN_C_END
257 AWS_POP_SANE_WARNING_LEVEL
258
259 #endif /* AWS_IO_CLIENT_RETRY_STRATEGY_H */