Skip to content

Commit

Permalink
chore(storage): add idempotency header (#7602)
Browse files Browse the repository at this point in the history
This additional header will allow idempotent retries on certain requests which are non-idempotent currently.
  • Loading branch information
tritone authored Mar 21, 2023
1 parent c967961 commit 75e992d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions storage/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ func setRetryHeaderHTTP(req interface{ Header() http.Header }) func(string, int)
return
}
header := req.Header()
// TODO(b/274504690): Consider dropping gccl-invocation-id key since it
// duplicates the X-Goog-Gcs-Idempotency-Token header (added in v1.31.0).
invocationHeader := fmt.Sprintf("gccl-invocation-id/%v gccl-attempt-count/%v", invocationID, attempts)
xGoogHeader := strings.Join([]string{invocationHeader, xGoogDefaultHeader}, " ")
header.Set("x-goog-api-client", xGoogHeader)
// Also use the invocationID for the idempotency token header, which will
// enable idempotent retries for more operations.
header.Set("x-goog-gcs-idempotency-token", invocationID)
}
}

Expand Down
23 changes: 14 additions & 9 deletions storage/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ func TestInvoke(t *testing.T) {
t.Run(test.desc, func(s *testing.T) {
counter := 0
req := &fakeApiaryRequest{header: http.Header{}}
var initialHeader string
var initialClientHeader, initialIdempotencyHeader string
call := func() error {
if counter == 0 {
initialHeader = req.Header()["X-Goog-Api-Client"][0]
initialClientHeader = req.Header()["X-Goog-Api-Client"][0]
initialIdempotencyHeader = req.Header()["X-Goog-Gcs-Idempotency-Token"][0]
}
counter++
if counter <= test.count {
Expand All @@ -186,22 +187,26 @@ func TestInvoke(t *testing.T) {
} else if !test.expectFinalErr && got != test.initialErr {
s.Errorf("got %v, want %v", got, test.initialErr)
}
gotHeader := req.Header()["X-Goog-Api-Client"][0]
gotClientHeader := req.Header()["X-Goog-Api-Client"][0]
gotIdempotencyHeader := req.Header()["X-Goog-Gcs-Idempotency-Token"][0]
wantAttempts := 1 + test.count
if !test.expectFinalErr {
wantAttempts = 1
}
wantHeader := strings.ReplaceAll(initialHeader, "gccl-attempt-count/1", fmt.Sprintf("gccl-attempt-count/%v", wantAttempts))
if gotHeader != wantHeader {
t.Errorf("case %q, retry header:\ngot %v\nwant %v", test.desc, gotHeader, wantHeader)
wantClientHeader := strings.ReplaceAll(initialClientHeader, "gccl-attempt-count/1", fmt.Sprintf("gccl-attempt-count/%v", wantAttempts))
if gotClientHeader != wantClientHeader {
t.Errorf("case %q, retry header:\ngot %v\nwant %v", test.desc, gotClientHeader, wantClientHeader)
}
wantHeaderFormat := "gccl-invocation-id/.{36} gccl-attempt-count/[0-9]+ gl-go/.* gccl/"
match, err := regexp.MatchString(wantHeaderFormat, gotHeader)
wantClientHeaderFormat := "gccl-invocation-id/.{36} gccl-attempt-count/[0-9]+ gl-go/.* gccl/"
match, err := regexp.MatchString(wantClientHeaderFormat, gotClientHeader)
if err != nil {
s.Fatalf("compiling regexp: %v", err)
}
if !match {
s.Errorf("X-Goog-Api-Client header has wrong format\ngot %v\nwant regex matching %v", gotHeader, wantHeaderFormat)
s.Errorf("X-Goog-Api-Client header has wrong format\ngot %v\nwant regex matching %v", gotClientHeader, wantClientHeaderFormat)
}
if gotIdempotencyHeader != initialIdempotencyHeader {
t.Errorf("case %q, idempotency header:\ngot %v\nwant %v", test.desc, gotIdempotencyHeader, initialIdempotencyHeader)
}
})
}
Expand Down

0 comments on commit 75e992d

Please sign in to comment.