-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
53 lines (45 loc) · 1.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
package workerpool
type Mode int
type LogFunc func(msgFormat string, args ...interface{})
type Option struct {
Mode Mode
Capacity int
NumberWorkers int
LogFunc LogFunc
}
type OptionFunc func(opt *Option)
func WithMode(mode Mode) OptionFunc {
return func(opt *Option) {
opt.Mode = mode
}
}
func WithNumberWorkers(numberWorkers int) OptionFunc {
return func(opt *Option) {
opt.NumberWorkers = numberWorkers
}
}
func WithCapacity(capacity int) OptionFunc {
return func(opt *Option) {
opt.Capacity = capacity
}
}
func WithLogFunc(logFunc LogFunc) OptionFunc {
return func(opt *Option) {
opt.LogFunc = logFunc
}
}
func makeDefaultOption(option *Option) {
if option.LogFunc == nil {
option.LogFunc = defaultLogFunc
}
if option.Mode != FixedSize && option.Mode != FlexibleSize {
option.Mode = FixedSize
option.LogFunc("Invalid pool mode, fallback to FixedSize")
}
if option.NumberWorkers <= 0 {
option.NumberWorkers = defaultNumberWorkers
}
if option.Capacity <= 0 {
option.Capacity = option.NumberWorkers * defaultCapacityRatio
}
}