From 660bb31d6dfc2c2f846d113fff0b788fdec95b39 Mon Sep 17 00:00:00 2001 From: Cody Oss Date: Tue, 21 Sep 2021 09:22:01 -0600 Subject: [PATCH 1/2] fix(compute/metadata): remove heavy gax dependency Because of how the gax package is structured today it ends up pulling in proto and gRPC which can add a lot of extra deps to the tree if all you really need is this package. Fixes: #4783 --- compute/metadata/metadata.go | 4 +--- compute/metadata/retry.go | 40 +++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/compute/metadata/metadata.go b/compute/metadata/metadata.go index 471adfdb1de..b6e1f7b614d 100644 --- a/compute/metadata/metadata.go +++ b/compute/metadata/metadata.go @@ -32,8 +32,6 @@ import ( "strings" "sync" "time" - - "github.com/googleapis/gax-go/v2" ) const ( @@ -317,7 +315,7 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) { code = res.StatusCode } if delay, shouldRetry := retryer.Retry(code, reqErr); shouldRetry { - if err := gax.Sleep(ctx, delay); err != nil { + if err := sleep(ctx, delay); err != nil { return "", "", err } continue diff --git a/compute/metadata/retry.go b/compute/metadata/retry.go index b1d1e7981a1..d19f96e188e 100644 --- a/compute/metadata/retry.go +++ b/compute/metadata/retry.go @@ -15,11 +15,11 @@ package metadata import ( + "context" "io" + "math/rand" "net/http" "time" - - "github.com/googleapis/gax-go/v2" ) const ( @@ -30,8 +30,42 @@ var ( syscallRetryable = func(err error) bool { return false } ) +// defaultBackoff is basically equivalent to gax.Backoff without the need for +// the dependency. +type defaultBackoff struct { + initial time.Duration + max time.Duration + mul float64 + cur time.Duration +} + +func (b *defaultBackoff) Pause() time.Duration { + d := time.Duration(1 + rand.Int63n(int64(b.cur))) + b.cur = time.Duration(float64(b.cur) * b.mul) + if b.cur > b.max { + b.cur = b.max + } + return d +} + +// sleep is the equivalent of gax.Sleep without the need for the dependency. +func sleep(ctx context.Context, d time.Duration) error { + t := time.NewTimer(d) + select { + case <-ctx.Done(): + t.Stop() + return ctx.Err() + case <-t.C: + return nil + } +} + func newRetryer() *metadataRetryer { - return &metadataRetryer{bo: &gax.Backoff{Initial: 100 * time.Millisecond}} + return &metadataRetryer{bo: &defaultBackoff{ + initial: 100 * time.Millisecond, + max: 30 * time.Second, + mul: 2, + }} } type backoff interface { From e150beaaaa35b8aaaab8e0d629d36c113f9189e3 Mon Sep 17 00:00:00 2001 From: Cody Oss Date: Tue, 21 Sep 2021 10:02:35 -0600 Subject: [PATCH 2/2] fix bug --- compute/metadata/retry.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/compute/metadata/retry.go b/compute/metadata/retry.go index d19f96e188e..0f18f3cda1e 100644 --- a/compute/metadata/retry.go +++ b/compute/metadata/retry.go @@ -33,10 +33,9 @@ var ( // defaultBackoff is basically equivalent to gax.Backoff without the need for // the dependency. type defaultBackoff struct { - initial time.Duration - max time.Duration - mul float64 - cur time.Duration + max time.Duration + mul float64 + cur time.Duration } func (b *defaultBackoff) Pause() time.Duration { @@ -62,9 +61,9 @@ func sleep(ctx context.Context, d time.Duration) error { func newRetryer() *metadataRetryer { return &metadataRetryer{bo: &defaultBackoff{ - initial: 100 * time.Millisecond, - max: 30 * time.Second, - mul: 2, + cur: 100 * time.Millisecond, + max: 30 * time.Second, + mul: 2, }} }