option.go 2.6 KB

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