httpclient

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 14 Imported by: 0

README

http client 模块介绍

这是一个 HTTP 客户端的扩展和封装, 对外提供统一的http调用

预警

  1. 现在http client和hystrix默认超时都是2s, 如果使用方业务比较耗时, 请设置较长的超时时间

描述

  1. http client 是在http原生client的封装
  2. http client 提供了强大的Middleware机制, 让扩展功能更加简单方便
  3. http client提供了重试机制和超时设置
  4. http client提供option的机制用于灵活配置

默认值

  1. 默认超时时间是2s
  2. 默认超时重试机制 NewRetrier(NewConstantBackoff(10*time.Millisecond, 50*time.Millisecond))
  3. hystrix默认最大超时2s
  4. hystrix默认最大并发请求5000
  5. hystrix默认错误百分比阈值10
  6. hystrix默认Sleep Window 10
  7. hystrix默认名字 http.client

使用

import "github.com/gowins/dionysus/httpclient"
简单的GET请求
client := httpclient.New(httpclient.WithHTTPTimeout(100 * time.Millisecond))
rsp, err := client.Get(URL, nil)
if err != nil{
	panic(err)
}

// rsp是标准的*http.Response对象
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
简单的Do请求
client := httpclient.New(httpclient.WithHTTPTimeout(100 * time.Millisecond))
req, _ := http.NewRequest(http.MethodGet, URL, nil)
rsp, err := client.Do(req)
if err != nil {
	panic(err)
}

// rsp是标准的*http.Response对象 
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
超时重试设置
client := httpclient.New(
    httpclient.WithHTTPTimeout(100 * time.Millisecond),
    httpclient.WithRetryCount(2),
    httpclient.WithRetrier(httpclient.NewRetrier(httpclient.NewConstantBackoff(10*time.Millisecond, 50*time.Millisecond))),
)

使用hystrix中间件

client := httpclient.New(
    httpclient.WithHTTPTimeout(100 * time.Millisecond),
    httpclient.WithRetryCount(2),
    httpclient.WithRetrier(httpclient.NewRetrier(httpclient.NewConstantBackoff(10*time.Millisecond, 50*time.Millisecond))),
    httpclient.WithMiddleware(httphystrix.Middleware(
    		httphystrix.WithCommandName("MyCommand"),
    		httphystrix.WithMaxConcurrentRequests(100),
    		httphystrix.WithErrorPercentThreshold(25),
    		httphystrix.WithSleepWindow(10),
    		httphystrix.WithRequestVolumeThreshold(10),
    	)),
)

transport设置

  1. http client默认支持的是http.DefaultTransport Client.Transports属性中包含: MaxIdleConns 所有host的连接池最大连接数量,默认无穷大 MaxIdleConnsPerHost 每个host的连接池最大空闲连接数,默认2 MaxConnsPerHost 对每个host的最大连接数量,0表示不限制 如果MaxConnsPerHost=1,则只有一个http client被创建. 如果MaxIdleConnsPerHost=1,则会缓存一个http client.
// http client 默认的配置
&http.Transport{
    Proxy: http.ProxyFromEnvironment,
    DialContext: (&net.Dialer{
        Timeout:   30 * time.Second, //限制建立TCP连接的时间
        KeepAlive: 30 * time.Second,
        DualStack: true,
    }).DialContext,
    ForceAttemptHTTP2:     true,
    MaxIdleConns:          100,
    IdleConnTimeout:       90 * time.Second,
    TLSHandshakeTimeout:   10 * time.Second, //限制TLS握手的时间
    ExpectContinueTimeout: 1 * time.Second, //限制client在发送包含 Expect: 100-continue的header到收到继续发送body的response之间的时间等待。
    MaxIdleConnsPerHost:   runtime.GOMAXPROCS(0) + 1, // http client缓存数量
}
  1. 设置和替换自己的transport
httpclient.New(httpclient.WithTransport("自定义transport"))
httpclient.Clone(httpclient.WithTransport("自定义transport"))

// 在使用的时候可以通过option的方式设置自己的transport
// 在调用Clone方法时, 直接复用父client的transport

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff = httpclient.Backoff

Backoff interface defines contract for backoff strategies

func NewConstantBackoff

func NewConstantBackoff(backoffInterval, maximumJitterInterval time.Duration) Backoff

NewConstantBackoff returns an instance of ConstantBackoff

func NewExponentialBackoff

func NewExponentialBackoff(initialTimeout, maxTimeout time.Duration, exponentFactor float64, maximumJitterInterval time.Duration) Backoff

NewExponentialBackoff returns an instance of ExponentialBackoff

type Client

type Client = httpclient.Client

func New

func New(opts ...Option) Client

New returns a new instance of http client

func NewWithTracer added in v1.2.0

func NewWithTracer(opts ...Option) Client

NewWithTracer returns a new instance of http client with tracer

type DoFunc

type DoFunc = httpclient.DoFunc

type Middleware

type Middleware = httpclient.Middleware

type Option

type Option = httpclient.Option

Option represents the http client options

func WithHTTPTimeout

func WithHTTPTimeout(timeout time.Duration) Option

WithHTTPTimeout sets hystrix timeout

func WithMiddleware

func WithMiddleware(m Middleware) Option

WithMiddleware sets the strategy for retrying

func WithRetrier

func WithRetrier(retrier httpclient.Retriable) Option

WithRetrier sets the strategy for retrying

func WithRetryCount

func WithRetryCount(retryCount int) Option

WithRetryCount sets the retry count for the client

func WithTracerEnable added in v1.2.0

func WithTracerEnable() Option

WithTracerEnable sets the Tracer enable ture

func WithTransport

func WithTransport(t http.RoundTripper) Option

WithTransport sets the Transport of client

type Options

type Options = httpclient.Options

Options http client options

type RequestOption added in v1.3.0

type RequestOption = httpclient.RequestOption

RequestOption http request option

func WithRequestContext added in v1.3.0

func WithRequestContext(ctx context.Context) RequestOption

func WithRequestRetrier added in v1.3.0

func WithRequestRetrier(retrier Retriable) RequestOption

func WithRequestRetryCount added in v1.3.0

func WithRequestRetryCount(count int) RequestOption

type RequestOptions added in v1.3.0

type RequestOptions = httpclient.RequestOptions

RequestOptions represents the http request options

type Retriable

type Retriable = httpclient.Retriable

Retriable defines contract for retriers to implement

func NewNoRetrier

func NewNoRetrier() Retriable

NewNoRetrier returns a null object for retriable

func NewRetrier

func NewRetrier(backoff Backoff) Retriable

NewRetrier returns retrier with some backoff strategy

func NewRetrierFunc

func NewRetrierFunc(f RetriableFunc) Retriable

NewRetrierFunc returns a retrier with a retry function defined

type RetriableFunc

type RetriableFunc = httpclient.RetriableFunc

RetriableFunc is an adapter to allow the use of ordinary functions as a Retriable

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL