feat(storage): setting idempotency token header - #27338
shubhangi-google wants to merge 15 commits into
Conversation
There was a problem hiding this comment.
AI review: there are two issues.
-
Global Default Scope Leak (Critical Issue):
By settingRequestOptions.default.add_idempotency_token_header = trueingoogle/apis/options.rb, this PR enables the GCS-specificX-Goog-Gcs-Idempotency-Tokenheader globally for all 400+ Google API clients usinggoogle-apis-core(e.g., YouTube, Drive, Compute Engine, Calendar, Bigtable). This violates service abstraction boundaries and adds a 36-byte UUID overhead and confusing headers to every non-GCS request.
Fix: Defaultadd_idempotency_token_headertofalseingoogle-apis-coreand explicitly enable it in thegoogle-apis-storage_v1client generation or configuration. -
Redundant Memoization:
Inset_idempotency_token_header, the@idempotency_token ||= SecureRandom.uuidmemoization is functionally dead code because of the early return on the line right before it:
return if header.any? { |k, _| k.to_s.downcase == 'x-goog-gcs-idempotency-token' }
@idempotency_token ||= SecureRandom.uuidBecause the header Hash is mutated in place and retained across retries of the ApiCommand instance, the first network attempt injects the header into header. On subsequent retries, the loop evaluates return if header.any? { ... }, successfully finding the original key, and returning early. Therefore, @idempotency_token is never evaluated or used again across retries.
Fix: You can simply do header['X-Goog-Gcs-Idempotency-Token'] = SecureRandom.uuid, as caching the UUID as an instance variable is unnecessary.
This pull request introduces support for sending an idempotency token header (X-Goog-Gcs-Idempotency-Token) in API commands, enabled by default via the new add_idempotency_token_header option.
linked storage library Pr : googleapis/google-cloud-ruby#34956