github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/client/retry.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/volts-dev/volts/internal/errors"
     7  )
     8  
     9  // note that returning either false or a non-nil error will result in the call not being retried
    10  type RetryFunc func(ctx context.Context, req IRequest, retryCount int, err error) (bool, error)
    11  
    12  // RetryAlways always retry on error
    13  func RetryAlways(ctx context.Context, req IRequest, retryCount int, err error) (bool, error) {
    14  	return true, nil
    15  }
    16  
    17  // RetryOnError retries a request on a 500 or timeout error
    18  func RetryOnError(ctx context.Context, req IRequest, retryCount int, err error) (bool, error) {
    19  	if err == nil {
    20  		return false, nil
    21  	}
    22  
    23  	e := errors.Parse(err.Error())
    24  	if e == nil {
    25  		return false, nil
    26  	}
    27  
    28  	switch e.Code {
    29  	// retry on timeout or internal server error
    30  	case 408, 500:
    31  		return true, nil
    32  	default:
    33  		return false, nil
    34  	}
    35  }