Skip to content

Commit

Permalink
feat(spanner): make randIDForProcess hexadecimal in x-goog-spanner-re…
Browse files Browse the repository at this point in the history
…quest-id

Implements a new requirement that randIDForProcess should
be a hexadecimal value with the format specifier "%016x".

Fixes #11545
  • Loading branch information
odeke-em committed Jan 31, 2025
1 parent 0dd7d3d commit 70ebf7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions spanner/request_id_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ import (
var randIDForProcess string

func init() {
bigMaxInt64, _ := new(big.Int).SetString(fmt.Sprintf("%d", uint64(math.MaxUint64)), 10)
if g, w := bigMaxInt64.Uint64(), uint64(math.MaxUint64); g != w {
bigMaxUint64, _ := new(big.Int).SetString(fmt.Sprintf("%d", uint64(math.MaxUint64)), 10)
if g, w := bigMaxUint64.Uint64(), uint64(math.MaxUint64); g != w {
panic(fmt.Sprintf("mismatch in randIDForProcess.maxUint64:\n\tGot: %d\n\tWant: %d", g, w))
}
r64, err := rand.Int(rand.Reader, bigMaxInt64)
r64, err := rand.Int(rand.Reader, bigMaxUint64)
if err != nil {
panic(err)
}
randIDForProcess = r64.String()
randIDForProcess = fmt.Sprintf("%016x", r64.Uint64())
}

// Please bump this version whenever this implementation
Expand Down
11 changes: 11 additions & 0 deletions spanner/request_id_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package spanner

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"math"
Expand Down Expand Up @@ -1925,3 +1926,13 @@ func TestRequestIDHeader_SingleUseReadOnly_ExecuteStreamingSql_UnavailableDuring
t.Fatalf("RequestID streaming segments mismatch: got - want +\n%s", diff)
}
}

func TestRequestID_randIDForProcessIsHexadecimal(t *testing.T) {
decoded, err := hex.DecodeString(randIDForProcess)
if err != nil {
t.Fatal(err)
}
if len(decoded) == 0 {
t.Fatal("Expected a non-empty decoded result")
}
}

0 comments on commit 70ebf7c

Please sign in to comment.