|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_FEATURE_TRACKER_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_FEATURE_TRACKER_H |
| 17 | + |
| 18 | +#include "google/cloud/storage/version.h" |
| 19 | +#include "google/cloud/options.h" |
| 20 | +#include <atomic> |
| 21 | +#include <cstdint> |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | + |
| 25 | +namespace google { |
| 26 | +namespace cloud { |
| 27 | +namespace storage { |
| 28 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 29 | +namespace internal { |
| 30 | + |
| 31 | +inline constexpr auto kFeatureTrackerHeaderName = "x-goog-storage-cpp-features"; |
| 32 | + |
| 33 | +// Tracked features represented as bit positions in a bitmask. |
| 34 | +enum class TrackedFeature : std::uint32_t { |
| 35 | + // Operation-Driven Optimizations |
| 36 | + kMultiStreamInMRD = 0, |
| 37 | + kPCU = 1, |
| 38 | + |
| 39 | + // Configuration-Driven Options |
| 40 | + kGrpcDirectPathEnforced = 2, |
| 41 | + kJsonReads = 3, |
| 42 | +}; |
| 43 | + |
| 44 | +// Converts a feature bitmask to a Base64-encoded string, stripping leading |
| 45 | +// zero bytes to maintain compact representation. |
| 46 | +std::string EncodeFeatureTrackerBitmask(std::uint32_t mask); |
| 47 | + |
| 48 | +// A thread-safe state object that can be shared between operation connections |
| 49 | +// (like ObjectDescriptorImpl) and RPC factories (like OpenStreamFactory) |
| 50 | +// to track which features are adopted during an operation. |
| 51 | +class FeatureTracker { |
| 52 | + public: |
| 53 | + FeatureTracker() = default; |
| 54 | + explicit FeatureTracker(std::uint32_t initial) : mask_(initial) {} |
| 55 | + |
| 56 | + void RegisterFeature(TrackedFeature feature) { |
| 57 | + mask_.fetch_or(1U << static_cast<std::uint32_t>(feature), |
| 58 | + std::memory_order_relaxed); |
| 59 | + } |
| 60 | + |
| 61 | + std::uint32_t GetMask() const { |
| 62 | + return mask_.load(std::memory_order_relaxed); |
| 63 | + } |
| 64 | + |
| 65 | + std::string HeaderValue() const { |
| 66 | + return EncodeFeatureTrackerBitmask(GetMask()); |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + std::atomic<std::uint32_t> mask_{0}; |
| 71 | +}; |
| 72 | + |
| 73 | +struct FeatureTrackerOption { |
| 74 | + using Type = std::shared_ptr<FeatureTracker>; |
| 75 | +}; |
| 76 | + |
| 77 | +// Evaluates client configuration options, creates a shared FeatureTracker |
| 78 | +// initialized with configuration-driven feature flags (if any), and stores it |
| 79 | +// into the Options list under FeatureTrackerOption. |
| 80 | +Options SetupFeatureTracker(Options opts); |
| 81 | + |
| 82 | +} // namespace internal |
| 83 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 84 | +} // namespace storage |
| 85 | +} // namespace cloud |
| 86 | +} // namespace google |
| 87 | + |
| 88 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_FEATURE_TRACKER_H |
0 commit comments