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_) {
@@ -256,26 +255,33 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
256255 std::string const & bucket_name) {
257256 CleanupCompletedTasks ();
258257
259- if (!BucketMetadataCache::Singleton () .StartFetch (bucket_name)) {
258+ if (!cache_ .StartFetch (bucket_name)) {
260259 return ;
261260 }
262261
263- auto f = std::async (std::launch::async, [bucket_name, options]() {
264- google::cloud::internal::OptionsSpan span (options);
265- auto conn = MakeStorageConnection (options);
266- auto const normalized =
267- BucketMetadataCache::NormalizeBucketName (bucket_name);
268- storage::internal::GetBucketMetadataRequest request (normalized);
269- auto metadata = conn->GetBucketMetadata (request);
270- if (metadata.ok ()) {
271- BucketMetadataCache::Singleton ().Put (
272- bucket_name, BucketCacheEntry::FromMetadata (*metadata));
273- } else if (metadata.status ().code () == StatusCode::kPermissionDenied ) {
274- BucketMetadataCache::Singleton ().Put (
275- bucket_name, {" projects/_/buckets/" + normalized, " global" });
262+ std::shared_ptr<storage::internal::StorageConnection> conn;
263+ {
264+ std::lock_guard<std::mutex> lk (mu_);
265+ if (!sync_conn_) {
266+ sync_conn_ = MakeStorageConnection (impl_->options ());
276267 }
277- BucketMetadataCache::Singleton ().EndFetch (bucket_name);
278- });
268+ conn = sync_conn_;
269+ }
270+
271+ auto f =
272+ std::async (std::launch::async, [bucket_name, options, conn, this ]() {
273+ google::cloud::internal::OptionsSpan span (options);
274+ auto const normalized_bucket_name =
275+ BucketMetadataCache::NormalizeBucketName (bucket_name);
276+ storage::internal::GetBucketMetadataRequest request (
277+ normalized_bucket_name);
278+ auto metadata = conn->GetBucketMetadata (request);
279+ auto entry = BucketCacheEntry::Create (bucket_name, metadata);
280+ if (entry) {
281+ cache_.Put (bucket_name, std::move (*entry));
282+ }
283+ cache_.EndFetch (bucket_name);
284+ });
279285
280286 std::unique_lock<std::mutex> lk (mu_);
281287 bg_tasks_.push_back (std::move (f));
@@ -284,7 +290,7 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
284290 void EnrichSpan (opentelemetry::trace::Span& span, Options const & options,
285291 std::string const & bucket_name) {
286292 if (bucket_name.empty ()) return ;
287- auto entry = BucketMetadataCache::Singleton () .Get (bucket_name);
293+ auto entry = cache_ .Get (bucket_name);
288294 if (entry.has_value ()) {
289295 span.SetAttribute (" gcp.resource.destination.id" , entry->id );
290296 span.SetAttribute (" gcp.resource.destination.location" , entry->location );
@@ -294,6 +300,8 @@ class AsyncConnectionTracing : public storage::AsyncConnection {
294300 }
295301
296302 std::shared_ptr<storage::AsyncConnection> impl_;
303+ BucketMetadataCache& cache_;
304+ std::shared_ptr<storage::internal::StorageConnection> sync_conn_;
297305 std::vector<std::future<void >> bg_tasks_;
298306 std::mutex mu_;
299307};
0 commit comments