...

Source file src/cloud.google.com/go/internal/retry.go

Documentation: cloud.google.com/go/internal

     1  // Copyright 2016 Google LLC
     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 internal
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	gax "github.com/googleapis/gax-go/v2"
    23  )
    24  
    25  // Retry calls the supplied function f repeatedly according to the provided
    26  // backoff parameters. It returns when one of the following occurs:
    27  // When f's first return value is true, Retry immediately returns with f's second
    28  // return value.
    29  // When the provided context is done, Retry returns with an error that
    30  // includes both ctx.Error() and the last error returned by f.
    31  func Retry(ctx context.Context, bo gax.Backoff, f func() (stop bool, err error)) error {
    32  	return retry(ctx, bo, f, gax.Sleep)
    33  }
    34  
    35  func retry(ctx context.Context, bo gax.Backoff, f func() (stop bool, err error),
    36  	sleep func(context.Context, time.Duration) error) error {
    37  	var lastErr error
    38  	for {
    39  		stop, err := f()
    40  		if stop {
    41  			return err
    42  		}
    43  		// Remember the last "real" error from f.
    44  		if err != nil && err != context.Canceled && err != context.DeadlineExceeded {
    45  			lastErr = err
    46  		}
    47  		p := bo.Pause()
    48  		if ctxErr := sleep(ctx, p); ctxErr != nil {
    49  			if lastErr != nil {
    50  				return wrappedCallErr{ctxErr: ctxErr, wrappedErr: lastErr}
    51  			}
    52  			return ctxErr
    53  		}
    54  	}
    55  }
    56  
    57  // Use this error type to return an error which allows introspection of both
    58  // the context error and the error from the service.
    59  type wrappedCallErr struct {
    60  	ctxErr     error
    61  	wrappedErr error
    62  }
    63  
    64  func (e wrappedCallErr) Error() string {
    65  	return fmt.Sprintf("retry failed with %v; last error: %v", e.ctxErr, e.wrappedErr)
    66  }
    67  
    68  func (e wrappedCallErr) Unwrap() error {
    69  	return e.wrappedErr
    70  }
    71  
    72  // Is allows errors.Is to match the error from the call as well as context
    73  // sentinel errors.
    74  func (e wrappedCallErr) Is(err error) bool {
    75  	return e.ctxErr == err || e.wrappedErr == err
    76  }
    77  

View as plain text