-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
100 lines (83 loc) · 2.19 KB
/
option.go
File metadata and controls
100 lines (83 loc) · 2.19 KB
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
package dsf
import (
"context"
"time"
)
type Option interface{}
type OptionBuilder interface {
Build() Option
WithContext(ctx context.Context) OptionBuilder
WithRedisClient(client RedisClient) OptionBuilder
WithNamespace(namespace string) OptionBuilder
WithLockExpiration(exp time.Duration) OptionBuilder
WithDataExpiration(exp time.Duration) OptionBuilder
KeepLock(flag bool) OptionBuilder
WithWaitTime(duration time.Duration) OptionBuilder
WithInterval(makeInterval func(retryTimes int) time.Duration) OptionBuilder
}
func NewOptionBuilder() OptionBuilder {
return &optionBuilderImpl{
ctx: context.Background(),
namespace: "default",
lockExp: time.Second,
dataExp: 2 * time.Second,
resultWaitTime: 2 * time.Second,
makeInterval: func(retryTimes int) time.Duration {
return 50 * time.Millisecond
},
}
}
type optionBuilderImpl struct {
ctx context.Context
client RedisClient
namespace string
lockExp time.Duration
dataExp time.Duration
keepLock bool
resultWaitTime time.Duration
makeInterval func(retryTimes int) time.Duration
}
func (b *optionBuilderImpl) Build() Option {
if b.client == nil ||
b.lockExp <= 0 ||
b.dataExp <= 0 ||
b.resultWaitTime <= 0 {
return nil
}
cpy := *b
return &cpy
}
func (b *optionBuilderImpl) WithContext(ctx context.Context) OptionBuilder {
b.ctx = ctx
return b
}
func (b *optionBuilderImpl) WithRedisClient(client RedisClient) OptionBuilder {
b.client = client
return b
}
func (b *optionBuilderImpl) WithNamespace(namespace string) OptionBuilder {
b.namespace = namespace
return b
}
func (b *optionBuilderImpl) WithLockExpiration(t time.Duration) OptionBuilder {
b.lockExp = t
return b
}
func (b *optionBuilderImpl) WithDataExpiration(t time.Duration) OptionBuilder {
b.dataExp = t
return b
}
func (b *optionBuilderImpl) KeepLock(flag bool) OptionBuilder {
b.keepLock = flag
return b
}
func (b *optionBuilderImpl) WithWaitTime(duration time.Duration) OptionBuilder {
b.resultWaitTime = duration
return b
}
func (b *optionBuilderImpl) WithInterval(
makeInterval func(retryTimes int) time.Duration,
) OptionBuilder {
b.makeInterval = makeInterval
return b
}