1414
1515#include " google/cloud/storage/internal/async/connection_tracing.h"
1616#include " google/cloud/storage/async/writer_connection.h"
17- #include " google/cloud/storage/internal/async/default_options.h"
1817#include " google/cloud/storage/internal/async/object_descriptor_connection_tracing.h"
1918#include " google/cloud/storage/internal/async/reader_connection_tracing.h"
2019#include " google/cloud/storage/internal/async/rewriter_connection_tracing.h"
@@ -42,7 +41,7 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
4241 public:
4342 explicit AsyncConnectionTracing (
4443 std::shared_ptr<storage::AsyncConnection> impl)
45- : impl_(std::move(impl)) {}
44+ : impl_(std::move(impl)), cache_(BucketMetadataCache::Singleton()) {}
4645
4746 ~AsyncConnectionTracing () override {
4847 for (auto & f : bg_tasks_) {
@@ -263,26 +262,33 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
263262 std::string const & bucket_name) {
264263 CleanupCompletedTasks ();
265264
266- if (!BucketMetadataCache::Singleton () .StartFetch (bucket_name)) {
265+ if (!cache_ .StartFetch (bucket_name)) {
267266 return ;
268267 }
269268
270- auto f = std::async (std::launch::async, [bucket_name, options]() {
271- google::cloud::internal::OptionsSpan span (options);
272- auto conn = MakeStorageConnection (options);
273- auto const normalized =
274- BucketMetadataCache::NormalizeBucketName (bucket_name);
275- storage::internal::GetBucketMetadataRequest request (normalized);
276- auto metadata = conn->GetBucketMetadata (request);
277- if (metadata.ok ()) {
278- BucketMetadataCache::Singleton ().Put (
279- bucket_name, BucketCacheEntry::FromMetadata (*metadata));
280- } else if (metadata.status ().code () == StatusCode::kPermissionDenied ) {
281- BucketMetadataCache::Singleton ().Put (
282- bucket_name, {" projects/_/buckets/" + normalized, " global" });
269+ std::shared_ptr<storage::internal::StorageConnection> conn;
270+ {
271+ std::lock_guard<std::mutex> lk (mu_);
272+ if (!sync_conn_) {
273+ sync_conn_ = MakeStorageConnection (impl_->options ());
283274 }
284- BucketMetadataCache::Singleton ().EndFetch (bucket_name);
285- });
275+ conn = sync_conn_;
276+ }
277+
278+ auto f =
279+ std::async (std::launch::async, [bucket_name, options, conn, this ]() {
280+ google::cloud::internal::OptionsSpan span (options);
281+ auto const normalized_bucket_name =
282+ BucketMetadataCache::NormalizeBucketName (bucket_name);
283+ storage::internal::GetBucketMetadataRequest request (
284+ normalized_bucket_name);
285+ auto metadata = conn->GetBucketMetadata (request);
286+ auto entry = BucketCacheEntry::Create (bucket_name, metadata);
287+ if (entry) {
288+ cache_.Put (bucket_name, std::move (*entry));
289+ }
290+ cache_.EndFetch (bucket_name);
291+ });
286292
287293 std::unique_lock<std::mutex> lk (mu_);
288294 bg_tasks_.push_back (std::move (f));
@@ -291,7 +297,7 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
291297 void EnrichSpan (opentelemetry::trace::Span& span, Options const & options,
292298 std::string const & bucket_name) {
293299 if (bucket_name.empty ()) return ;
294- auto entry = BucketMetadataCache::Singleton () .Get (bucket_name);
300+ auto entry = cache_ .Get (bucket_name);
295301 if (entry.has_value ()) {
296302 span.SetAttribute (" gcp.resource.destination.id" , entry->id );
297303 span.SetAttribute (" gcp.resource.destination.location" , entry->location );
@@ -301,6 +307,8 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
301307 }
302308
303309 std::shared_ptr<storage::AsyncConnection> impl_;
310+ BucketMetadataCache& cache_;
311+ std::shared_ptr<storage::internal::StorageConnection> sync_conn_;
304312 std::vector<std::future<void >> bg_tasks_;
305313 std::mutex mu_;
306314};
0 commit comments