-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
54 lines (43 loc) · 1.07 KB
/
options.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
package multilimiter
const DEFAULT_RATE = 1.0
const DEFAULT_CONCURRENCY = 1
// Base interface for all options
type Option interface {
apply(*options)
}
// Contains all possible options
type options struct {
rateLimit *RateLimitOption
concLimit *ConcLimitOption
}
// Creates an instance of options out of a slice of Options
func CreateOptions(opts ...Option) *options {
allOpts := &options{}
for _, opt := range opts {
opt.apply(allOpts)
}
setDefaultOpts(allOpts)
return allOpts
}
func setDefaultOpts(allOpts *options) {
if allOpts.rateLimit == nil {
allOpts.rateLimit = &RateLimitOption{NewRateLimiter(DEFAULT_RATE)}
}
if allOpts.concLimit == nil {
allOpts.concLimit = &ConcLimitOption{NewConcLimiter(DEFAULT_CONCURRENCY)}
}
}
// option for controlling rate limiting
type RateLimitOption struct {
Limiter RateLimiter
}
func (me *RateLimitOption) apply(allopts *options) {
allopts.rateLimit = me
}
// option for controlling concurrency
type ConcLimitOption struct {
Limiter ConcLimiter
}
func (me *ConcLimitOption) apply(allopts *options) {
allopts.concLimit = me
}