...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/transport/retry.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote/transport

     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/internal/retry"
    22  )
    23  
    24  // Sleep for 0.1 then 0.3 seconds. This should cover networking blips.
    25  var defaultBackoff = retry.Backoff{
    26  	Duration: 100 * time.Millisecond,
    27  	Factor:   3.0,
    28  	Jitter:   0.1,
    29  	Steps:    3,
    30  }
    31  
    32  var _ http.RoundTripper = (*retryTransport)(nil)
    33  
    34  // retryTransport wraps a RoundTripper and retries temporary network errors.
    35  type retryTransport struct {
    36  	inner     http.RoundTripper
    37  	backoff   retry.Backoff
    38  	predicate retry.Predicate
    39  	codes     []int
    40  }
    41  
    42  // Option is a functional option for retryTransport.
    43  type Option func(*options)
    44  
    45  type options struct {
    46  	backoff   retry.Backoff
    47  	predicate retry.Predicate
    48  	codes     []int
    49  }
    50  
    51  // Backoff is an alias of retry.Backoff to expose this configuration option to consumers of this lib
    52  type Backoff = retry.Backoff
    53  
    54  // WithRetryBackoff sets the backoff for retry operations.
    55  func WithRetryBackoff(backoff Backoff) Option {
    56  	return func(o *options) {
    57  		o.backoff = backoff
    58  	}
    59  }
    60  
    61  // WithRetryPredicate sets the predicate for retry operations.
    62  func WithRetryPredicate(predicate func(error) bool) Option {
    63  	return func(o *options) {
    64  		o.predicate = predicate
    65  	}
    66  }
    67  
    68  // WithRetryStatusCodes sets which http response codes will be retried.
    69  func WithRetryStatusCodes(codes ...int) Option {
    70  	return func(o *options) {
    71  		o.codes = codes
    72  	}
    73  }
    74  
    75  // NewRetry returns a transport that retries errors.
    76  func NewRetry(inner http.RoundTripper, opts ...Option) http.RoundTripper {
    77  	o := &options{
    78  		backoff:   defaultBackoff,
    79  		predicate: retry.IsTemporary,
    80  	}
    81  
    82  	for _, opt := range opts {
    83  		opt(o)
    84  	}
    85  
    86  	return &retryTransport{
    87  		inner:     inner,
    88  		backoff:   o.backoff,
    89  		predicate: o.predicate,
    90  		codes:     o.codes,
    91  	}
    92  }
    93  
    94  func (t *retryTransport) RoundTrip(in *http.Request) (out *http.Response, err error) {
    95  	roundtrip := func() error {
    96  		out, err = t.inner.RoundTrip(in)
    97  		if !retry.Ever(in.Context()) {
    98  			return nil
    99  		}
   100  		if out != nil {
   101  			for _, code := range t.codes {
   102  				if out.StatusCode == code {
   103  					return retryError(out)
   104  				}
   105  			}
   106  		}
   107  		return err
   108  	}
   109  	retry.Retry(roundtrip, t.predicate, t.backoff)
   110  	return
   111  }
   112  

View as plain text