Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_IO_HOST_RESOLVER_H
2 #define AWS_IO_HOST_RESOLVER_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/common/ref_count.h>
9 #include <aws/io/io.h>
10
11 AWS_PUSH_SANE_WARNING_LEVEL
12
13 struct aws_event_loop_group;
14
15 enum aws_address_record_type {
16 /* ipv4 address. */
17 AWS_ADDRESS_RECORD_TYPE_A,
18 /* ipv6 address. */
19 AWS_ADDRESS_RECORD_TYPE_AAAA
20 };
21
22 enum aws_get_host_address_flags {
23 /* get number of ipv4 addresses. */
24 AWS_GET_HOST_ADDRESS_COUNT_RECORD_TYPE_A = 0x00000001,
25 /* get number of ipv6 addresses. */
26 AWS_GET_HOST_ADDRESS_COUNT_RECORD_TYPE_AAAA = 0x00000002
27 };
28
29 struct aws_string;
30
31 struct aws_host_address {
32 struct aws_allocator *allocator;
33 const struct aws_string *host;
34 const struct aws_string *address;
35 enum aws_address_record_type record_type;
36 uint64_t expiry;
37 /* This next section is strictly for mitigating the impact of sticky hosts that aren't performing well. */
38 /*for use in DNS-based load balancing.*/
39 size_t use_count;
40 /* give a hint on when to remove a bad host from service. */
41 size_t connection_failure_count;
42 /* we don't implement this yet, but we will asap. */
43 uint8_t weight;
44 };
45
46 struct aws_host_resolver;
47 /**
48 * Invoked once an address has been resolved for host. The type in host_addresses is struct aws_host_address (by-value).
49 * The caller does not own this memory and you must copy the host address before returning from this function if you
50 * plan to use it later. For convenience, we've provided the aws_host_address_copy() and aws_host_address_clean_up()
51 * functions.
52 */
53 typedef void(aws_on_host_resolved_result_fn)(
54 struct aws_host_resolver *resolver,
55 const struct aws_string *host_name,
56 int err_code,
57 const struct aws_array_list *host_addresses,
58 void *user_data);
59
60 /**
61 * Function signature for configuring your own resolver (the default just uses getaddrinfo()). The type in
62 * output_addresses is struct aws_host_address (by-value). We assume this function blocks, hence this absurdly
63 * complicated design.
64 */
65 typedef int(aws_resolve_host_implementation_fn)(
66 struct aws_allocator *allocator,
67 const struct aws_string *host_name,
68 struct aws_array_list *output_addresses,
69 void *user_data);
70
71 struct aws_host_resolution_config {
72 aws_resolve_host_implementation_fn *impl;
73 size_t max_ttl;
74 void *impl_data;
75 uint64_t resolve_frequency_ns; /* 0 defaults to 1 second interval */
76 };
77
78 struct aws_host_resolver_purge_host_options {
79 /* the host to purge the cache for */
80 const struct aws_string *host;
81 /* Callback to invoke when the purge is complete */
82 aws_simple_completion_callback *on_host_purge_complete_callback;
83 /* user_data will be passed as it is in the callback. */
84 void *user_data;
85 };
86
87 /** should you absolutely disdain the default implementation, feel free to implement your own. */
88 struct aws_host_resolver_vtable {
89 /** clean up everything you allocated, but not resolver itself. */
90 void (*destroy)(struct aws_host_resolver *resolver);
91
92 /** resolve the host by host_name, the user owns host_name, so it needs to be copied if you persist it,
93 * invoke res with the result. This function should never block. */
94 int (*resolve_host)(
95 struct aws_host_resolver *resolver,
96 const struct aws_string *host_name,
97 aws_on_host_resolved_result_fn *res,
98 const struct aws_host_resolution_config *config,
99 void *user_data);
100
101 /** gives your implementation a hint that an address has some failed connections occuring. Do whatever you want (or
102 * nothing) about it.
103 */
104 int (*record_connection_failure)(struct aws_host_resolver *resolver, const struct aws_host_address *address);
105
106 /**
107 * @Deprecated Use purge_cache_with_callback instead
108 * wipe out anything you have cached. */
109 int (*purge_cache)(struct aws_host_resolver *resolver);
110
111 /** wipe out anything you have cached. */
112 int (*purge_cache_with_callback)(
113 struct aws_host_resolver *resolver,
114 aws_simple_completion_callback *on_purge_cache_complete_callback,
115 void *user_data);
116
117 /** wipe out anything cached for a specific host */
118 int (*purge_host_cache)(
119 struct aws_host_resolver *resolver,
120 const struct aws_host_resolver_purge_host_options *options);
121
122 /** get number of addresses for a given host. */
123 size_t (*get_host_address_count)(
124 struct aws_host_resolver *resolver,
125 const struct aws_string *host_name,
126 uint32_t flags);
127 };
128
129 struct aws_host_resolver {
130 struct aws_allocator *allocator;
131 void *impl;
132 struct aws_host_resolver_vtable *vtable;
133 struct aws_ref_count ref_count;
134 struct aws_shutdown_callback_options shutdown_options;
135 };
136
137 struct aws_host_resolver_default_options {
138 size_t max_entries;
139 struct aws_event_loop_group *el_group;
140 const struct aws_shutdown_callback_options *shutdown_options;
141 aws_io_clock_fn *system_clock_override_fn;
142 };
143
144 AWS_EXTERN_C_BEGIN
145
146 /**
147 * Copies `from` to `to`.
148 */
149 AWS_IO_API int aws_host_address_copy(const struct aws_host_address *from, struct aws_host_address *to);
150
151 /**
152 * Moves `from` to `to`. After this call, from is no longer usable. Though, it could be resused for another
153 * move or copy operation.
154 */
155 AWS_IO_API void aws_host_address_move(struct aws_host_address *from, struct aws_host_address *to);
156
157 /**
158 * Cleans up the memory for `address`
159 */
160 AWS_IO_API void aws_host_address_clean_up(struct aws_host_address *address);
161
162 /** WARNING! do not call this function directly (getaddrinfo()): it blocks. Provide a pointer to this function for other
163 * resolution functions. */
164 AWS_IO_API int aws_default_dns_resolve(
165 struct aws_allocator *allocator,
166 const struct aws_string *host_name,
167 struct aws_array_list *output_addresses,
168 void *user_data);
169
170 /**
171 * Creates a host resolver with the default behavior. Here's the behavior:
172 *
173 * Since there's not a reliable way to do non-blocking DNS without a ton of risky work that would need years of testing
174 * on every Unix system in existence, we work around it by doing a threaded implementation.
175 *
176 * When you request an address, it checks the cache. If the entry isn't in the cache it creates a new one.
177 * Each entry has a potentially short lived back-ground thread based on ttl for the records. Once we've populated the
178 * cache and you keep the resolver active, the resolution callback will be invoked immediately. When it's idle, it will
179 * take a little while in the background thread to fetch more, evaluate TTLs etc... In that case your callback will be
180 * invoked from the background thread.
181 *
182 * --------------------------------------------------------------------------------------------------------------------
183 *
184 * A few things to note about TTLs and connection failures.
185 *
186 * We attempt to honor your max ttl but will not honor it if dns queries are failing or all of your connections are
187 * marked as failed. Once we are able to query dns again, we will re-evaluate the TTLs.
188 *
189 * Upon notification connection failures, we move them to a separate list. Eventually we retry them when it's likely
190 * that the endpoint is healthy again or we don't really have another choice, but we try to keep them out of your
191 * hot path.
192 *
193 * ---------------------------------------------------------------------------------------------------------------------
194 *
195 * Finally, this entire design attempts to prevent problems where developers have to choose between large TTLs and thus
196 * sticky hosts or short TTLs and good fleet utilization but now higher latencies. In this design, we resolve every
197 * second in the background (only while you're actually using the record), but we do not expire the earlier resolved
198 * addresses until max ttl has passed.
199 *
200 * This for example, should enable you to hit thousands of hosts in the Amazon S3 fleet instead of just one or two.
201 */
202 AWS_IO_API struct aws_host_resolver *aws_host_resolver_new_default(
203 struct aws_allocator *allocator,
204 const struct aws_host_resolver_default_options *options);
205
206 /**
207 * Increments the reference count on the host resolver, allowing the caller to take a reference to it.
208 *
209 * Returns the same host resolver passed in.
210 */
211 AWS_IO_API struct aws_host_resolver *aws_host_resolver_acquire(struct aws_host_resolver *resolver);
212
213 /**
214 * Decrements a host resolver's ref count. When the ref count drops to zero, the resolver will be destroyed.
215 */
216 AWS_IO_API void aws_host_resolver_release(struct aws_host_resolver *resolver);
217
218 /**
219 * calls resolve_host on the vtable. config will be copied.
220 */
221 AWS_IO_API int aws_host_resolver_resolve_host(
222 struct aws_host_resolver *resolver,
223 const struct aws_string *host_name,
224 aws_on_host_resolved_result_fn *res,
225 const struct aws_host_resolution_config *config,
226 void *user_data);
227
228 /**
229 * calls record_connection_failure on the vtable.
230 */
231 AWS_IO_API int aws_host_resolver_record_connection_failure(
232 struct aws_host_resolver *resolver,
233 const struct aws_host_address *address);
234
235 /**
236 * @Deprecated Use purge_cache_with_callback instead
237 * calls purge_cache on the vtable.
238 */
239 AWS_IO_API int aws_host_resolver_purge_cache(struct aws_host_resolver *resolver);
240
241 /**
242 * Calls aws_host_resolver_purge_cache_with_callback on the vtable which will wipe out everything host resolver has
243 * cached.
244 */
245 AWS_IO_API int aws_host_resolver_purge_cache_with_callback(
246 struct aws_host_resolver *resolver,
247 aws_simple_completion_callback *on_purge_cache_complete_callback,
248 void *user_data);
249
250 /**
251 * Removes the cache for a host asynchronously.
252 */
253 AWS_IO_API int aws_host_resolver_purge_host_cache(
254 struct aws_host_resolver *resolver,
255 const struct aws_host_resolver_purge_host_options *options);
256
257 /**
258 * get number of addresses for a given host.
259 */
260 AWS_IO_API size_t aws_host_resolver_get_host_address_count(
261 struct aws_host_resolver *resolver,
262 const struct aws_string *host_name,
263 uint32_t flags);
264
265 /**
266 * Returns the default host resolution config used internally if none specified.
267 *
268 * @return default host resolution config
269 */
270 AWS_IO_API struct aws_host_resolution_config aws_host_resolver_init_default_resolution_config(void);
271
272 AWS_EXTERN_C_END
273 AWS_POP_SANE_WARNING_LEVEL
274
275 #endif /* AWS_IO_HOST_RESOLVER_H */