-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
134 lines (116 loc) · 3.45 KB
/
types.go
File metadata and controls
134 lines (116 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package httpc
import (
"bytes"
"context"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
// 默认配置常量
const (
defaultBufferSize = 32 << 10 // 32KB
defaultMaxBufferPool = 100
defaultUserAgent = "Touka HTTP Client/v0"
defaultMaxIdleConns = 128
defaultIdleConnTimeout = 90 * time.Second
defaultDialTimeout = 10 * time.Second
defaultKeepAliveTimeout = 30 * time.Second
defaultTLSHandshakeTimeout = 10 * time.Second
defaultExpectContinueTimeout = 1 * time.Second
defaultResolverTimeout = 5 * time.Second
)
// RoundTripperFunc 是一个适配器,允许使用普通函数作为 HTTP RoundTripper
type RoundTripperFunc func(req *http.Request) (*http.Response, error)
// RoundTrip 实现了 http.RoundTripper 接口
func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
// MiddlewareFunc 是客户端中间件的类型
// 它接收一个 http.RoundTripper (代表下一个处理器) 并返回一个新的 http.RoundTripper
type MiddlewareFunc func(next http.RoundTripper) http.RoundTripper
var bufferPool = sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, defaultBufferSize))
},
}
var stringsBuilderPool = sync.Pool{
New: func() any {
return &strings.Builder{}
},
}
// DumpLogFunc 定义日志记录函数
type DumpLogFunc func(ctx context.Context, log string)
// Client 主客户端结构
type Client struct {
client *http.Client
transport *http.Transport
checkRedirect func(req *http.Request, via []*http.Request) error
maxRedirects int
followRedirect bool
retryOpts RetryOptions
randomFloat64 func() float64
bufferPool BufferPool
userAgent string
dumpLog DumpLogFunc // 日志记录函数
maxIdleConns int // 最大空闲连接数
bufferSize int // 缓冲池 buffer 大小
maxBufferPool int // 最大缓冲池数量
timeout time.Duration // 默认请求超时时间 (可选)
middlewares []MiddlewareFunc // 中间件链
dialer *net.Dialer // dialer实例
}
// RetryOptions 重试配置
type RetryOptions struct {
MaxAttempts int
BaseDelay time.Duration
MaxDelay time.Duration
RetryStatuses []int
Jitter bool // 是否启用 Jitter 抖动
}
// BufferPool 缓冲池接口
type BufferPool interface {
Get() *bytes.Buffer
Put(*bytes.Buffer)
}
// 默认缓冲池实现
type defaultPool struct {
bufferSize int
}
func newDefaultPool(bufferSize int) *defaultPool {
return &defaultPool{bufferSize: bufferSize}
}
func (p *defaultPool) Get() *bytes.Buffer {
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
return buf
}
func (p *defaultPool) Put(buf *bytes.Buffer) {
if buf.Cap() > p.bufferSize*2 { // 防止内存泄漏,基于配置的 bufferSize
return
}
bufferPool.Put(buf)
}
// Option 配置选项类型
type Option func(*Client)
// ProtocolsConfig 协议版本配置结构体
type ProtocolsConfig struct {
Http1 bool // 是否启用 HTTP/1.1
Http2 bool // 是否启用 HTTP/2
Http2_Cleartext bool // 是否启用 H2C
ForceH2C bool // 是否强制启用 H2C
}
// RequestBuilder 用于构建请求的结构体
type RequestBuilder struct {
client *Client
method string
url string
header http.Header
query url.Values
body io.Reader
context context.Context
noDefaultHeaders bool
}