再次修改options的使用
This commit is contained in:
+62
-19
@@ -6,37 +6,55 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type clientOptions struct {
|
||||
TimeOut time.Duration
|
||||
InsecureSkipVerify bool
|
||||
Logger OptionLogger
|
||||
LoggerLength int // 日志输出长度
|
||||
type ClientOptions struct {
|
||||
TimeOut time.Duration
|
||||
InsecureSkipVerify bool
|
||||
Logger OptionLogger
|
||||
LoggerLength int // 日志输出长度
|
||||
CertFingerprint string // 证书指纹验证
|
||||
|
||||
// 连接池配置
|
||||
MaxIdleConns int
|
||||
MaxIdleConnsPerHost int
|
||||
MaxConnsPerHost int
|
||||
IdleConnTimeout time.Duration
|
||||
}
|
||||
|
||||
func defaultOptions() clientOptions {
|
||||
return clientOptions{
|
||||
TimeOut: time.Second * 120, // 默认超时120
|
||||
Logger: defaultLogger{},
|
||||
LoggerLength: 100,
|
||||
func defaultOptions() ClientOptions {
|
||||
return ClientOptions{
|
||||
TimeOut: time.Second * 120, // 默认超时120秒
|
||||
Logger: defaultLogger{},
|
||||
LoggerLength: 100,
|
||||
MaxIdleConns: 100, // 默认连接池大小
|
||||
MaxIdleConnsPerHost: 10, // 每主机默认空闲连接数
|
||||
MaxConnsPerHost: 50, // 每主机最大连接数
|
||||
IdleConnTimeout: 90 * time.Second, // 空闲连接超时
|
||||
}
|
||||
}
|
||||
|
||||
type Option func(*clientOptions)
|
||||
type Option func(*ClientOptions)
|
||||
|
||||
/**
|
||||
* 设置超时时间
|
||||
*/
|
||||
func SetOptionTimeOut(t time.Duration) Option {
|
||||
return func(options *clientOptions) {
|
||||
func WithOptionTimeOut(t time.Duration) Option {
|
||||
return func(options *ClientOptions) {
|
||||
options.TimeOut = t
|
||||
}
|
||||
}
|
||||
|
||||
// 添加证书指纹验证选项
|
||||
func WithOptionTLSPin(certFingerprint string) Option {
|
||||
return func(options *ClientOptions) {
|
||||
options.CertFingerprint = certFingerprint
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 不校验HTTPS证书
|
||||
*/
|
||||
func SetOptionTLSInsecureSkipVerify() Option {
|
||||
return func(options *clientOptions) {
|
||||
func WithOptionTLSInsecureSkipVerify() Option {
|
||||
return func(options *ClientOptions) {
|
||||
options.InsecureSkipVerify = true
|
||||
}
|
||||
}
|
||||
@@ -44,8 +62,8 @@ func SetOptionTLSInsecureSkipVerify() Option {
|
||||
/**
|
||||
* 设置日志输出
|
||||
*/
|
||||
func SetOptionLog(log OptionLogger) Option {
|
||||
return func(options *clientOptions) {
|
||||
func WithOptionLog(log OptionLogger) Option {
|
||||
return func(options *ClientOptions) {
|
||||
options.Logger = log
|
||||
}
|
||||
}
|
||||
@@ -53,12 +71,37 @@ func SetOptionLog(log OptionLogger) Option {
|
||||
/**
|
||||
* 设置日志输出长度
|
||||
*/
|
||||
func WithLoggerLength(length int) Option {
|
||||
return func(options *clientOptions) {
|
||||
func WithOptionLoggerLength(length int) Option {
|
||||
return func(options *ClientOptions) {
|
||||
options.LoggerLength = length
|
||||
}
|
||||
}
|
||||
|
||||
// 连接池配置选项
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
type OptionLogger interface {
|
||||
Infof(ctx context.Context, format string, args ...any)
|
||||
Errorf(ctx context.Context, format string, args ...any)
|
||||
|
||||
Reference in New Issue
Block a user