Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable configurable client retries with backoff in RekorClient #1096

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ require (
sigs.k8s.io/release-utils v0.7.3
)

require golang.org/x/exp v0.0.0-20220823124025-807a23277127
require (
github.com/hashicorp/go-retryablehttp v0.7.1
golang.org/x/exp v0.0.0-20220823124025-807a23277127
)

require filippo.io/edwards25519 v1.0.0-rc.1 // indirect
require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
)

require (
cloud.google.com/go v0.103.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,16 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb
github.com/hanwen/go-fuse v1.0.0/go.mod h1:unqXarDXqzAk0rt98O2tVndEPIpUgLD9+rwFisZH3Ok=
github.com/hanwen/go-fuse/v2 v2.1.0/go.mod h1:oRyA5eK+pvJyv5otpO/DgccS8y/RvYMaO00GgRLGryc=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v1.2.1 h1:YQsLlGDJgwhXFpucSPyVbCBviQtjlHv3jLTlp8YmtEw=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-plugin v1.4.4 h1:NVdrSdFRt3SkZtNckJ6tog7gbpRrcbOjQi/rgF7JYWQ=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-secure-stdlib/mlock v0.1.2 h1:p4AKXPPS24tO8Wc8i1gLvSKdmkiSY5xuju57czJ/IJQ=
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 h1:om4Al8Oy7kCm/B86rLCLah4Dt5Aa0Fr5rYBG60OzwHQ=
Expand Down
41 changes: 38 additions & 3 deletions pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,39 @@

package client

import "net/http"
import (
"log"
"net/http"
"os"

"github.com/hashicorp/go-retryablehttp"
)

// Option is a functional option for customizing static signatures.
type Option func(*options)

type options struct {
UserAgent string
UserAgent string
RetryCount uint
Logger retryablehttp.Logger
}

const (
// DefaultRetryCount is the default number of retries.
DefaultRetryCount = 3
)

var DefaultLogger retryablehttp.Logger

func init() {
DefaultLogger = log.New(os.Stderr, "", log.LstdFlags)
}

func makeOptions(opts ...Option) *options {
o := &options{
UserAgent: "",
UserAgent: "",
RetryCount: DefaultRetryCount,
Logger: DefaultLogger,
}

for _, opt := range opts {
Expand All @@ -42,6 +63,20 @@ func WithUserAgent(userAgent string) Option {
}
}

// WithRetryCount sets the number of retries.
func WithRetryCount(retryCount uint) Option {
return func(o *options) {
o.RetryCount = retryCount
}
}

// WithLogger sets the logger.
func WithLogger(logger retryablehttp.Logger) Option {
return func(o *options) {
o.Logger = logger
}
}

type roundTripper struct {
http.RoundTripper
UserAgent string
Expand Down
20 changes: 16 additions & 4 deletions pkg/client/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,43 @@
package client

import (
"log"
"net/http"
"os"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestMakeOptions(t *testing.T) {
customLogger := log.New(os.Stdout, "", log.LstdFlags)

tests := []struct {
desc string

opts []Option
want *options
}{{
desc: "no opts",
want: &options{},
want: &options{RetryCount: DefaultRetryCount, Logger: DefaultLogger},
}, {
desc: "WithUserAgent",
opts: []Option{WithUserAgent("test user agent")},
want: &options{UserAgent: "test user agent"},
want: &options{UserAgent: "test user agent", RetryCount: DefaultRetryCount, Logger: DefaultLogger},
}, {
desc: "WithRetryCount",
opts: []Option{WithRetryCount(2)},
want: &options{UserAgent: "", RetryCount: 2, Logger: DefaultLogger},
}, {
desc: "WithLogger",
opts: []Option{WithLogger(customLogger)},
want: &options{UserAgent: "", RetryCount: DefaultRetryCount, Logger: customLogger},
}}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got := makeOptions(tc.opts...)
if d := cmp.Diff(tc.want, got); d != "" {
t.Errorf("makeOptions() returned unexpected result (-want +got): %s", d)
if d := cmp.Diff(tc.want, got, cmp.Comparer(func(a, b *log.Logger) bool { return a == b })); d != "" {
t.Errorf("makeOptions(%v) returned unexpected result (-want +got): %s", tc.desc, d)
}
})
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/client/rekor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/util"
"github.com/spf13/viper"
Expand All @@ -32,7 +33,11 @@ func GetRekorClient(rekorServerURL string, opts ...Option) (*client.Rekor, error
}
o := makeOptions(opts...)

rt := httptransport.New(url.Host, client.DefaultBasePath, []string{url.Scheme})
retryableClient := retryablehttp.NewClient()
asraa marked this conversation as resolved.
Show resolved Hide resolved
retryableClient.RetryMax = int(o.RetryCount)
retryableClient.Logger = o.Logger

rt := httptransport.NewWithClient(url.Host, client.DefaultBasePath, []string{url.Scheme}, retryableClient.StandardClient())
rt.Consumers["application/json"] = runtime.JSONConsumer()
rt.Consumers["application/x-pem-file"] = runtime.TextConsumer()
rt.Consumers["application/pem-certificate-chain"] = runtime.TextConsumer()
Expand Down
30 changes: 29 additions & 1 deletion pkg/client/rekor_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAPIKey(t *testing.T) {

}

func TestGetRekorClientWithOptions(t *testing.T) {
func TestGetRekorClientWithUserAgent(t *testing.T) {
t.Parallel()
expectedUserAgent := "test User-Agent"
requestReceived := false
Expand All @@ -105,3 +105,31 @@ func TestGetRekorClientWithOptions(t *testing.T) {
t.Fatal("no requests were received")
}
}

func TestGetRekorClientWithRetryCount(t *testing.T) {
t.Parallel()
expectedCount := 2
actualCount := 0
testServer := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
actualCount++
file := []byte{}

if actualCount < expectedCount {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
_, _ = w.Write(file)
}
}))
defer testServer.Close()

client, err := GetRekorClient(testServer.URL, WithRetryCount(2))
if err != nil {
t.Error(err)
}
_, err = client.Tlog.GetLogInfo(nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}