github.com/searKing/golang/go@v1.2.117/net/http/dowithbackoff.options.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import (
     8  	"net/http"
     9  
    10  	time_ "github.com/searKing/golang/go/time"
    11  )
    12  
    13  // WithDoWithBackoffOptionChainUnaryInterceptor returns a DoWithBackoffOption that specifies the chained
    14  // interceptor for http clients. The first interceptor will be the outer most,
    15  // while the last interceptor will be the inner most wrapper around the real call.
    16  // All interceptors added by this method will be chained, and the interceptor
    17  // defined by WithClientInterceptor will always be prepended to the chain.
    18  func WithDoWithBackoffOptionChainUnaryInterceptor(interceptors ...ClientInterceptor) DoWithBackoffOption {
    19  	return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
    20  		o.ChainClientInterceptors = append(o.ChainClientInterceptors, interceptors...)
    21  	})
    22  }
    23  
    24  func WithDoWithBackoffOptionRetryAfter(f RetryAfterHandler) DoWithBackoffOption {
    25  	return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
    26  		o.RetryAfter = f
    27  	})
    28  }
    29  
    30  func WithDoWithBackoffOptionDoRetryHandler(f DoRetryHandler) DoWithBackoffOption {
    31  	return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
    32  		o.DoRetryHandler = f
    33  	})
    34  }
    35  
    36  func WithDoWithBackoffOptionExponentialBackOffOption(opts ...time_.ExponentialBackOffOption) DoWithBackoffOption {
    37  	return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
    38  		o.ExponentialBackOffOption = append(o.ExponentialBackOffOption, opts...)
    39  	})
    40  }
    41  
    42  func WithDoWithBackoffOptionMaxRetries(retries int) DoWithBackoffOption {
    43  	return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
    44  		o.ExponentialBackOffOption = append(o.ExponentialBackOffOption, time_.WithExponentialBackOffOptionMaxElapsedCount(retries))
    45  	})
    46  }
    47  
    48  func WithDoWithBackoffOptionGrpcBackOff(retries int) DoWithBackoffOption {
    49  	return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
    50  		o.ExponentialBackOffOption = append(o.ExponentialBackOffOption, time_.WithExponentialBackOffOptionGRPC())
    51  	})
    52  }
    53  
    54  // WithDoWithBackoffOptionRoundTripper returns a DoWithBackoffOption.
    55  func WithDoWithBackoffOptionRoundTripper(rt http.RoundTripper) DoWithBackoffOption {
    56  	return WithDoWithBackoffOptionDoRetryHandler(
    57  		func(req *http.Request, retry int) (*http.Response, error) {
    58  			return rt.RoundTrip(req)
    59  		})
    60  }
    61  
    62  // WithDoWithBackoffOptionTarget returns a DoWithBackoffOption.
    63  // TargetUrl is proxy's url, like socks5://127.0.0.1:8080
    64  // Host is proxy's addr, replace the HOST in TargetUrl if not empty
    65  func WithDoWithBackoffOptionTarget(target string) DoWithBackoffOption {
    66  	if target == "" {
    67  		return EmptyDoWithBackoffOption{}
    68  	}
    69  	cli := NewClientWithTarget(target)
    70  	return WithDoWithBackoffOptionDoRetryHandler(
    71  		func(req *http.Request, retry int) (*http.Response, error) {
    72  			return cli.Do(req)
    73  		})
    74  }
    75  
    76  // WithDoWithBackoffOptionProxy returns a DoWithBackoffOption.
    77  // TargetUrl is proxy's url, like socks5://127.0.0.1:8080
    78  // Host is proxy's addr, replace the HOST in TargetUrl if not empty
    79  func WithDoWithBackoffOptionProxy(proxyUrl string, proxyTarget string) DoWithBackoffOption {
    80  	if proxyUrl == "" {
    81  		return EmptyDoWithBackoffOption{}
    82  	}
    83  	cli := NewClientWithProxy(proxyUrl, proxyTarget)
    84  	return WithDoWithBackoffOptionDoRetryHandler(
    85  		func(req *http.Request, retry int) (*http.Response, error) {
    86  			return cli.Do(req)
    87  		})
    88  }