option.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package httpclient
  2. import (
  3. "sync"
  4. "time"
  5. "git.bvbej.com/bvbej/base-golang/pkg/trace"
  6. "go.uber.org/zap"
  7. )
  8. var (
  9. cache = &sync.Pool{
  10. New: func() any {
  11. return &option{
  12. header: make(map[string][]string),
  13. basicAuth: new(basicAuth),
  14. }
  15. },
  16. }
  17. )
  18. // Mock 定义接口Mock数据
  19. type Mock func() (body []byte)
  20. // Option 自定义设置http请求
  21. type Option func(*option)
  22. type basicAuth struct {
  23. enabled bool
  24. username string
  25. password string
  26. }
  27. type option struct {
  28. ttl time.Duration
  29. basicAuth *basicAuth
  30. header map[string][]string
  31. trace *trace.Trace
  32. dialog *trace.Dialog
  33. logger *zap.Logger
  34. retryTimes int
  35. retryDelay time.Duration
  36. retryVerify RetryVerify
  37. alarmTitle string
  38. alarmObject AlarmObject
  39. alarmVerify AlarmVerify
  40. mock Mock
  41. }
  42. func (o *option) reset() {
  43. o.ttl = 0
  44. o.basicAuth = new(basicAuth)
  45. o.header = make(map[string][]string)
  46. o.trace = nil
  47. o.dialog = nil
  48. o.logger = nil
  49. o.retryTimes = 0
  50. o.retryDelay = 0
  51. o.retryVerify = nil
  52. o.alarmTitle = ""
  53. o.alarmObject = nil
  54. o.alarmVerify = nil
  55. o.mock = nil
  56. }
  57. func getOption() *option {
  58. return cache.Get().(*option)
  59. }
  60. func releaseOption(opt *option) {
  61. opt.reset()
  62. cache.Put(opt)
  63. }
  64. // WithTTL 本次http请求最长执行时间
  65. func WithTTL(ttl time.Duration) Option {
  66. return func(opt *option) {
  67. opt.ttl = ttl
  68. }
  69. }
  70. // WithHeader 设置http header,可以调用多次设置多对key-value
  71. func WithHeader(key, value string) Option {
  72. return func(opt *option) {
  73. opt.header[key] = []string{value}
  74. }
  75. }
  76. // WithBasicAuth 设置基础认证权限
  77. func WithBasicAuth(username, password string) Option {
  78. return func(opt *option) {
  79. opt.basicAuth.enabled = true
  80. opt.basicAuth.username = username
  81. opt.basicAuth.password = password
  82. }
  83. }
  84. // WithTrace 设置trace信息
  85. func WithTrace(t trace.T) Option {
  86. return func(opt *option) {
  87. if t != nil {
  88. opt.trace = t.(*trace.Trace)
  89. opt.dialog = new(trace.Dialog)
  90. }
  91. }
  92. }
  93. // WithLogger 设置logger以便打印关键日志
  94. func WithLogger(logger *zap.Logger) Option {
  95. return func(opt *option) {
  96. opt.logger = logger
  97. }
  98. }
  99. // WithMock 设置 mock 数据
  100. func WithMock(m Mock) Option {
  101. return func(opt *option) {
  102. opt.mock = m
  103. }
  104. }
  105. // WithOnFailedAlarm 设置告警通知
  106. func WithOnFailedAlarm(alarmTitle string, alarmObject AlarmObject, alarmVerify AlarmVerify) Option {
  107. return func(opt *option) {
  108. opt.alarmTitle = alarmTitle
  109. opt.alarmObject = alarmObject
  110. opt.alarmVerify = alarmVerify
  111. }
  112. }
  113. // WithOnFailedRetry 设置失败重试
  114. func WithOnFailedRetry(retryTimes int, retryDelay time.Duration, retryVerify RetryVerify) Option {
  115. return func(opt *option) {
  116. opt.retryTimes = retryTimes
  117. opt.retryDelay = retryDelay
  118. opt.retryVerify = retryVerify
  119. }
  120. }