Files
curlx/options.go
T

121 lines
2.7 KiB
Go
Raw Normal View History

2024-01-01 14:10:18 +08:00
package curlx
2024-01-21 21:02:05 +08:00
import (
"context"
"log"
"time"
)
2024-01-01 14:10:18 +08:00
2026-03-01 23:06:20 +08:00
type ClientOptions struct {
TimeOut time.Duration
InsecureSkipVerify bool
Logger OptionLogger
LoggerLength int // 日志输出长度
CertFingerprint string // 证书指纹验证
// 连接池配置
MaxIdleConns int
MaxIdleConnsPerHost int
MaxConnsPerHost int
IdleConnTimeout time.Duration
2024-01-01 14:10:18 +08:00
}
2026-03-01 23:06:20 +08:00
func defaultOptions() ClientOptions {
return ClientOptions{
TimeOut: time.Second * 120, // 默认超时120秒
Logger: defaultLogger{},
LoggerLength: 100,
MaxIdleConns: 100, // 默认连接池大小
MaxIdleConnsPerHost: 10, // 每主机默认空闲连接数
MaxConnsPerHost: 50, // 每主机最大连接数
IdleConnTimeout: 90 * time.Second, // 空闲连接超时
2024-01-01 14:10:18 +08:00
}
}
2026-03-01 23:06:20 +08:00
type Option func(*ClientOptions)
2024-01-01 14:10:18 +08:00
/**
* 设置超时时间
*/
2026-03-01 23:06:20 +08:00
func WithOptionTimeOut(t time.Duration) Option {
return func(options *ClientOptions) {
2024-01-01 14:10:18 +08:00
options.TimeOut = t
}
}
2026-03-01 23:06:20 +08:00
// 添加证书指纹验证选项
func WithOptionTLSPin(certFingerprint string) Option {
return func(options *ClientOptions) {
options.CertFingerprint = certFingerprint
}
}
2024-01-01 14:10:18 +08:00
/**
* 不校验HTTPS证书
*/
2026-03-01 23:06:20 +08:00
func WithOptionTLSInsecureSkipVerify() Option {
return func(options *ClientOptions) {
2024-01-01 14:10:18 +08:00
options.InsecureSkipVerify = true
}
}
2024-01-21 21:02:05 +08:00
/**
* 设置日志输出
*/
2026-03-01 23:06:20 +08:00
func WithOptionLog(log OptionLogger) Option {
return func(options *ClientOptions) {
2024-01-21 21:02:05 +08:00
options.Logger = log
}
}
2025-12-28 15:14:05 +08:00
/**
* 设置日志输出长度
*/
2026-03-01 23:06:20 +08:00
func WithOptionLoggerLength(length int) Option {
return func(options *ClientOptions) {
2025-12-28 15:14:05 +08:00
options.LoggerLength = length
}
}
2026-03-01 23:06:20 +08:00
// 连接池配置选项
func WithMaxIdleConns(maxIdleConns int) Option {
return func(options *ClientOptions) {
options.MaxIdleConns = maxIdleConns
}
}
func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) Option {
return func(options *ClientOptions) {
options.MaxIdleConnsPerHost = maxIdleConnsPerHost
}
}
func WithMaxConnsPerHost(maxConnsPerHost int) Option {
return func(options *ClientOptions) {
options.MaxConnsPerHost = maxConnsPerHost
}
}
func WithIdleConnTimeout(timeout time.Duration) Option {
return func(options *ClientOptions) {
options.IdleConnTimeout = timeout
}
}
2024-01-21 21:02:05 +08:00
type OptionLogger interface {
2026-03-01 13:26:38 +08:00
Infof(ctx context.Context, format string, args ...any)
Errorf(ctx context.Context, format string, args ...any)
2024-01-21 21:02:05 +08:00
}
type defaultLogger struct{}
2026-03-01 13:26:38 +08:00
func (defaultLogger) Errorf(ctx context.Context, format string, args ...any) {
2024-01-21 21:02:05 +08:00
// 输出日志
log.Printf(format, args...)
}
2026-03-01 13:26:38 +08:00
func (defaultLogger) Infof(ctx context.Context, format string, args ...any) {
2024-01-21 21:02:05 +08:00
// 输出日志
log.Printf(format, args...)
}