Skip to content

Commit

Permalink
handle idempotence key error (#1200)
Browse files Browse the repository at this point in the history
reading from rand can return an error and fail to read bytes, this
leads to the second component not being random.

This solution panics in that case, similar to getting a new uuid
https://github.com/google/uuid/blob/master/version4.go#L13-L15

the side effect is, in the case the second component is not random (an
error happened) we are now panicing instead of continuing

Signed-off-by: Andrew Klotz <[email protected]>
  • Loading branch information
KlotzAndrew authored Oct 7, 2020
1 parent 6bbf083 commit 01cecc3
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion params.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ type RangeQueryParams struct {
func NewIdempotencyKey() string {
now := time.Now().UnixNano()
buf := make([]byte, 4)
rand.Read(buf)
if _, err := rand.Read(buf); err != nil {
panic(err)
}
return fmt.Sprintf("%v_%v", now, base64.URLEncoding.EncodeToString(buf)[:6])
}

Expand Down

0 comments on commit 01cecc3

Please sign in to comment.