|
| 1 | +// Copyright 2018 Google LLC All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package transport |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/google/go-containerregistry/pkg/internal/retry" |
| 22 | + "k8s.io/apimachinery/pkg/util/wait" |
| 23 | +) |
| 24 | + |
| 25 | +// Sleep for 0.1, 0.3, 0.9, 2.7 seconds. This should cover networking blips. |
| 26 | +var defaultBackoff = wait.Backoff{ |
| 27 | + Duration: 100 * time.Millisecond, |
| 28 | + Factor: 3.0, |
| 29 | + Jitter: 0.1, |
| 30 | + Steps: 5, |
| 31 | +} |
| 32 | + |
| 33 | +var _ http.RoundTripper = (*retryTransport)(nil) |
| 34 | + |
| 35 | +// retryTransport wraps a RoundTripper and retries temporary network errors. |
| 36 | +type retryTransport struct { |
| 37 | + inner http.RoundTripper |
| 38 | + backoff wait.Backoff |
| 39 | + predicate retry.Predicate |
| 40 | +} |
| 41 | + |
| 42 | +// Option is a functional option for retryTransport. |
| 43 | +type Option func(*options) |
| 44 | + |
| 45 | +type options struct { |
| 46 | + backoff wait.Backoff |
| 47 | + predicate retry.Predicate |
| 48 | +} |
| 49 | + |
| 50 | +// WithBackoff sets the backoff for retry operations. |
| 51 | +func WithBackoff(backoff wait.Backoff) Option { |
| 52 | + return func(o *options) { |
| 53 | + o.backoff = backoff |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// WithPredicate sets the predicate for retry operations. |
| 58 | +func WithPredicate(predicate func(error) bool) Option { |
| 59 | + return func(o *options) { |
| 60 | + o.predicate = predicate |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// NewRetry returns a transport that retries errors. |
| 65 | +func NewRetry(inner http.RoundTripper, opts ...Option) http.RoundTripper { |
| 66 | + o := &options{ |
| 67 | + backoff: defaultBackoff, |
| 68 | + predicate: retry.IsTemporary, |
| 69 | + } |
| 70 | + |
| 71 | + for _, opt := range opts { |
| 72 | + opt(o) |
| 73 | + } |
| 74 | + |
| 75 | + return &retryTransport{ |
| 76 | + inner: inner, |
| 77 | + backoff: o.backoff, |
| 78 | + predicate: o.predicate, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (t *retryTransport) RoundTrip(in *http.Request) (out *http.Response, err error) { |
| 83 | + roundtrip := func() error { |
| 84 | + out, err = t.inner.RoundTrip(in) |
| 85 | + return err |
| 86 | + } |
| 87 | + retry.Retry(roundtrip, retry.WithBackoff(t.backoff), retry.WithPredicate(t.predicate)) |
| 88 | + return |
| 89 | +} |
0 commit comments