|
23 | 23 | #include <string_view> |
24 | 24 |
|
25 | 25 | #include "absl/base/attributes.h" |
26 | | -#include "absl/base/call_once.h" |
27 | 26 | #include "absl/base/log_severity.h" |
| 27 | +#include "absl/base/no_destructor.h" |
| 28 | +#include "absl/base/thread_annotations.h" |
28 | 29 | #include "absl/debugging/leak_check.h" |
29 | 30 | #include "absl/log/absl_log.h" |
| 31 | +#include "absl/synchronization/mutex.h" |
30 | 32 | #include <aws/auth/auth.h> |
31 | 33 | #include <aws/cal/cal.h> |
32 | 34 | #include <aws/common/allocator.h> |
@@ -145,74 +147,104 @@ aws_logger s_absl_logger{ |
145 | 147 | }; |
146 | 148 |
|
147 | 149 | // AWS apis rely on global initialization; do that here. |
148 | | -ABSL_CONST_INIT absl::once_flag g_init; |
149 | | - |
150 | | -aws_event_loop_group *g_event_loop_group = nullptr; |
151 | | -aws_host_resolver *g_resolver = nullptr; |
152 | | -aws_client_bootstrap *g_client_bootstrap = nullptr; |
153 | | -aws_tls_ctx *g_tls_ctx = nullptr; |
154 | | - |
155 | | -void InitAwsLibraries() { |
156 | | - absl::LeakCheckDisabler disabler; |
157 | | - |
158 | | - auto *allocator = GetAwsAllocator(); |
159 | | - |
160 | | - /* Initialize AWS libraries.*/ |
161 | | - aws_common_library_init(allocator); |
162 | | - |
163 | | - s_absl_logger.allocator = allocator; |
164 | | - aws_logger_set(&s_absl_logger); |
165 | | - |
166 | | - aws_cal_library_init(allocator); |
167 | | - aws_io_library_init(allocator); |
168 | | - aws_http_library_init(allocator); |
169 | | - aws_auth_library_init(allocator); |
170 | | - |
171 | | - /* event loop */ |
172 | | - g_event_loop_group = aws_event_loop_group_new_default(allocator, 0, nullptr); |
173 | | - |
174 | | - /* resolver */ |
175 | | - aws_host_resolver_default_options resolver_options; |
176 | | - AWS_ZERO_STRUCT(resolver_options); |
177 | | - resolver_options.el_group = g_event_loop_group; |
178 | | - resolver_options.max_entries = 32; // defaults to 8? |
179 | | - g_resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
180 | | - |
181 | | - /* client bootstrap */ |
182 | | - aws_client_bootstrap_options bootstrap_options; |
183 | | - AWS_ZERO_STRUCT(bootstrap_options); |
184 | | - bootstrap_options.event_loop_group = g_event_loop_group; |
185 | | - bootstrap_options.host_resolver = g_resolver; |
186 | | - g_client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
187 | | - if (g_client_bootstrap == nullptr) { |
188 | | - ABSL_LOG(FATAL) << "ERROR initializing client bootstrap: " |
189 | | - << aws_error_debug_str(aws_last_error()); |
| 150 | +class AwsApi { |
| 151 | + public: |
| 152 | + AwsApi() : allocator_(aws_default_allocator()) { |
| 153 | + absl::LeakCheckDisabler disabler; |
| 154 | + |
| 155 | + /* Initialize AWS libraries.*/ |
| 156 | + aws_common_library_init(allocator_); |
| 157 | + |
| 158 | + s_absl_logger.allocator = allocator_; |
| 159 | + aws_logger_set(&s_absl_logger); |
| 160 | + |
| 161 | + aws_cal_library_init(allocator_); |
| 162 | + aws_io_library_init(allocator_); |
| 163 | + aws_http_library_init(allocator_); |
| 164 | + aws_auth_library_init(allocator_); |
| 165 | + } |
| 166 | + |
| 167 | + aws_allocator *allocator() { return allocator_; } |
| 168 | + |
| 169 | + aws_client_bootstrap *client_bootstrap() ABSL_LOCKS_EXCLUDED(mutex_) { |
| 170 | + absl::MutexLock l(&mutex_); |
| 171 | + init_client_bootstrap(); |
| 172 | + return client_bootstrap_; |
| 173 | + } |
| 174 | + |
| 175 | + aws_tls_ctx *tls_ctx() ABSL_LOCKS_EXCLUDED(mutex_) { |
| 176 | + absl::MutexLock l(&mutex_); |
| 177 | + init_tls_ctx(); |
| 178 | + return tls_ctx_; |
| 179 | + } |
| 180 | + |
| 181 | + private: |
| 182 | + void init_event_loop_group() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) { |
| 183 | + if (event_loop_group_ != nullptr) return; |
| 184 | + event_loop_group_ = |
| 185 | + aws_event_loop_group_new_default(allocator_, 0, nullptr); |
190 | 186 | } |
191 | 187 |
|
192 | | - AwsTlsCtx tls_ctx = AwsTlsCtxBuilder(allocator).Build(); |
193 | | - if (tls_ctx == nullptr) { |
194 | | - ABSL_LOG(FATAL) << "ERROR initializing TLS context: " |
195 | | - << aws_error_debug_str(aws_last_error()); |
| 188 | + void init_resolver() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) { |
| 189 | + if (resolver_ != nullptr) return; |
| 190 | + init_event_loop_group(); |
| 191 | + |
| 192 | + aws_host_resolver_default_options resolver_options; |
| 193 | + AWS_ZERO_STRUCT(resolver_options); |
| 194 | + resolver_options.el_group = event_loop_group_; |
| 195 | + resolver_options.max_entries = 32; // defaults to 8? |
| 196 | + resolver_ = aws_host_resolver_new_default(allocator_, &resolver_options); |
| 197 | + } |
| 198 | + |
| 199 | + void init_client_bootstrap() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) { |
| 200 | + if (client_bootstrap_ != nullptr) return; |
| 201 | + init_event_loop_group(); |
| 202 | + init_resolver(); |
| 203 | + |
| 204 | + aws_client_bootstrap_options bootstrap_options; |
| 205 | + AWS_ZERO_STRUCT(bootstrap_options); |
| 206 | + bootstrap_options.event_loop_group = event_loop_group_; |
| 207 | + bootstrap_options.host_resolver = resolver_; |
| 208 | + client_bootstrap_ = |
| 209 | + aws_client_bootstrap_new(allocator_, &bootstrap_options); |
| 210 | + if (client_bootstrap_ == nullptr) { |
| 211 | + ABSL_LOG(FATAL) << "ERROR initializing client bootstrap: " |
| 212 | + << aws_error_debug_str(aws_last_error()); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + void init_tls_ctx() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) { |
| 217 | + if (tls_ctx_ != nullptr) return; |
| 218 | + auto my_tls_ctx = AwsTlsCtxBuilder(allocator_).Build(); |
| 219 | + if (my_tls_ctx == nullptr) { |
| 220 | + ABSL_LOG(FATAL) << "ERROR initializing TLS context: " |
| 221 | + << aws_error_debug_str(aws_last_error()); |
| 222 | + } |
| 223 | + tls_ctx_ = my_tls_ctx.release(); |
196 | 224 | } |
197 | | - g_tls_ctx = tls_ctx.release(); |
| 225 | + |
| 226 | + absl::Mutex mutex_; |
| 227 | + aws_allocator *allocator_ = nullptr; |
| 228 | + aws_event_loop_group *event_loop_group_ ABSL_GUARDED_BY(mutex_) = nullptr; |
| 229 | + aws_host_resolver *resolver_ ABSL_GUARDED_BY(mutex_) = nullptr; |
| 230 | + aws_client_bootstrap *client_bootstrap_ ABSL_GUARDED_BY(mutex_) = nullptr; |
| 231 | + aws_tls_ctx *tls_ctx_ ABSL_GUARDED_BY(mutex_) = nullptr; |
| 232 | +}; |
| 233 | + |
| 234 | +AwsApi &GetAwsApi() { |
| 235 | + static absl::NoDestructor<AwsApi> aws_api; |
| 236 | + return *aws_api; |
198 | 237 | } |
199 | 238 |
|
200 | 239 | } // namespace |
201 | 240 |
|
202 | | -aws_allocator *GetAwsAllocator() { |
203 | | - // The default allocator is used for all AWS API objects. |
204 | | - return aws_default_allocator(); |
205 | | -} |
| 241 | +aws_allocator *GetAwsAllocator() { return GetAwsApi().allocator(); } |
206 | 242 |
|
207 | 243 | aws_client_bootstrap *GetAwsClientBootstrap() { |
208 | | - absl::call_once(g_init, InitAwsLibraries); |
209 | | - return g_client_bootstrap; |
| 244 | + return GetAwsApi().client_bootstrap(); |
210 | 245 | } |
211 | 246 |
|
212 | | -aws_tls_ctx *GetAwsTlsCtx() { |
213 | | - absl::call_once(g_init, InitAwsLibraries); |
214 | | - return g_tls_ctx; |
215 | | -} |
| 247 | +aws_tls_ctx *GetAwsTlsCtx() { return GetAwsApi().tls_ctx(); } |
216 | 248 |
|
217 | 249 | } // namespace internal_aws |
218 | 250 | } // namespace tensorstore |
0 commit comments