-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
81 lines (65 loc) · 1.51 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
package gopool
import "time"
type Task = interface{} // 任务
// the func to batch process items
type BatchExecutor = func([]Task) error
// Executor
type Executor = func(Task) error
// the func to handle error
type BatchErrorCallback = func(err error, items []Task, processor BatchExecutor)
// ErrorCallback error callback
type ErrorCallback = func(err error, item Task, processor Executor)
type Option = func(c *PoolConfig)
type PoolConfig struct {
batchSize int
workers int
chanSize int
linger time.Duration
batchExe BatchExecutor
batchErrCb BatchErrorCallback
exe Executor
errCb ErrorCallback
}
func BatchSize(sz int) Option {
return func(c *PoolConfig) {
c.batchSize = sz
}
}
func WorkerCount(count int) Option {
return func(c *PoolConfig) {
c.workers = count
}
}
func ChanSize(size int) Option {
return func(c *PoolConfig) {
c.chanSize = size
}
}
// Linger linger time for batcher
func Linger(d time.Duration) Option {
return func(c *PoolConfig) {
c.linger = d
}
}
func BatchExecute(batchExe BatchExecutor) Option {
return func(c *PoolConfig) {
c.batchExe = batchExe
}
}
// BatchErrCallback batcher handle error callback
func BatchErrCallback(batchErrCb BatchErrorCallback) Option {
return func(c *PoolConfig) {
c.batchErrCb = batchErrCb
}
}
func Execute(exe Executor) Option {
return func(c *PoolConfig) {
c.exe = exe
}
}
// ErrCallback woker handle error callback
func ErrCallback(errCb ErrorCallback) Option {
return func(c *PoolConfig) {
c.errCb = errCb
}
}