github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/refertable/cli.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package refertable
     7  
     8  import (
     9  	"net/http"
    10  	"time"
    11  
    12  	"github.com/hashicorp/go-retryablehttp"
    13  )
    14  
    15  type cliLogger struct{}
    16  
    17  func (*cliLogger) Error(msg string, kv ...any) {
    18  	l.Error(msg, kv)
    19  }
    20  
    21  func (*cliLogger) Warn(msg string, kv ...any) {
    22  	l.Warn(msg, kv)
    23  }
    24  
    25  func (*cliLogger) Info(msg string, kv ...any) {
    26  	l.Info(msg, kv)
    27  }
    28  
    29  func (*cliLogger) Debug(msg string, kv ...any) {
    30  	l.Debug(msg, kv)
    31  }
    32  
    33  func newRetryCli(client *http.Client, timeout time.Duration) *retryablehttp.Client {
    34  	cli := retryablehttp.NewClient()
    35  
    36  	cli.RetryMax = 3
    37  	cli.RetryWaitMin = time.Second
    38  	cli.RetryWaitMax = time.Second * 5
    39  
    40  	cli.RequestLogHook = func(_ retryablehttp.Logger, r *http.Request, n int) {
    41  		if n > 0 {
    42  			l.Warnf("retry %d time on API %s", n, r.URL.Path)
    43  		}
    44  	}
    45  
    46  	cli.HTTPClient = client
    47  	cli.HTTPClient.Timeout = timeout
    48  	cli.Logger = &cliLogger{}
    49  
    50  	return cli
    51  }