1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
option.go 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package httpclient
import (
"sync"
"time"
"gitee.com/carlmax_my/console-core-go/pkg/trace"
"go.uber.org/zap"
)
var (
cache = &sync.Pool{
New: func() interface{} {
return &option{
header: make(map[string][]string),
}
},
}
)
// Mock define Mock data
type Mock func() (body []byte)
// Option customize http request options
type Option func(*option)
type option struct {
ttl time.Duration
header map[string][]string
trace *trace.Trace
dialog *trace.Dialog
logger *zap.Logger
retryTimes int
retryDelay time.Duration
retryVerify RetryVerify
alarmTitle string
alarmObject AlarmObject
alarmVerify AlarmVerify
mock Mock
}
func (o *option) reset() {
o.ttl = 0
o.header = make(map[string][]string)
o.trace = nil
o.dialog = nil
o.logger = nil
o.retryTimes = 0
o.retryDelay = 0
o.retryVerify = nil
o.alarmTitle = ""
o.alarmObject = nil
o.alarmVerify = nil
o.mock = nil
}
func getOption() *option {
return cache.Get().(*option)
}
func releaseOption(opt *option) {
opt.reset()
cache.Put(opt)
}
// WithTTL The longest execution time for current HTTP request
func WithTTL(ttl time.Duration) Option {
return func(opt *option) {
opt.ttl = ttl
}
}
// WithHeader set http header,can call multiple and set multiple key-value pairs
func WithHeader(key, value string) Option {
return func(opt *option) {
opt.header[key] = []string{value}
}
}
// WithTrace set trace config
func WithTrace(t trace.T) Option {
return func(opt *option) {
if t != nil {
opt.trace = t.(*trace.Trace)
opt.dialog = new(trace.Dialog)
}
}
}
// WithLogger set logger to print key log
func WithLogger(logger *zap.Logger) Option {
return func(opt *option) {
opt.logger = logger
}
}
// WithMock set mock data
func WithMock(m Mock) Option {
return func(opt *option) {
opt.mock = m
}
}
// WithOnFailedAlarm set alert notice
func WithOnFailedAlarm(alarmTitle string, alarmObject AlarmObject, alarmVerify AlarmVerify) Option {
return func(opt *option) {
opt.alarmTitle = alarmTitle
opt.alarmObject = alarmObject
opt.alarmVerify = alarmVerify
}
}
// WithOnFailedRetry set failure retry
func WithOnFailedRetry(retryTimes int, retryDelay time.Duration, retryVerify RetryVerify) Option {
return func(opt *option) {
opt.retryTimes = retryTimes
opt.retryDelay = retryDelay
opt.retryVerify = retryVerify
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.30

搜索帮助