-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
114 lines (88 loc) · 2.07 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package drivercommon
import (
"time"
cryptocommon "github.com/foxxorcat/DriverCore/common/crypto"
encodercommon "github.com/foxxorcat/DriverCore/common/encoder"
)
type DriverOption struct {
Timeout time.Duration // 超时时间
Attempt int // 重试次数
WaitTime time.Duration // 重试等待时间
MaxWaitTime time.Duration // 重试最长等待时间
Encoder encodercommon.EncoderPlugin // 编码器
Crypto cryptocommon.CryptoPlugin // 加密器
}
func (b *DriverOption) SetOption(options ...Option) error {
var err error
for _, option := range options {
if err = option.Apply(b); err != nil {
return err
}
}
return nil
}
type Option interface {
Apply(*DriverOption) error
}
// 设置重试
type attempt int
func WithAttempt(t int) Option {
return (*attempt)(&t)
}
func (t *attempt) Apply(o *DriverOption) error {
o.Attempt = int(*t)
return nil
}
// 设置超时
type timeout time.Duration
func WithTimeout(t time.Duration) Option {
return (*timeout)(&t)
}
func (t *timeout) Apply(o *DriverOption) error {
o.Timeout = time.Duration(*t)
return nil
}
// 设置重试等待时间
type waitTime time.Duration
func WithWaitTime(t time.Duration) Option {
return (*waitTime)(&t)
}
func (t waitTime) Apply(o *DriverOption) error {
o.WaitTime = time.Duration(t)
return nil
}
// 设置重试最大等待时间
type maxWaitTime time.Duration
func WithMaxWaitTime(t time.Duration) Option {
return (*maxWaitTime)(&t)
}
func (t maxWaitTime) Apply(o *DriverOption) error {
o.MaxWaitTime = time.Duration(t)
return nil
}
// 设置encoder
type encoder struct {
encodercommon.EncoderPlugin
}
func WithEncoder(t encodercommon.EncoderPlugin) Option {
return &encoder{t}
}
func (t *encoder) Apply(o *DriverOption) error {
if t.EncoderPlugin != nil {
o.Encoder = t.EncoderPlugin
}
return nil
}
// 设置crypto
type crypto struct {
cryptocommon.CryptoPlugin
}
func WithCrypto(t cryptocommon.CryptoPlugin) Option {
return &crypto{t}
}
func (t *crypto) Apply(o *DriverOption) error {
if t.CryptoPlugin != nil {
o.Crypto = t.CryptoPlugin
}
return nil
}