forked from shmel1k/gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
71 lines (59 loc) · 1.75 KB
/
config.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
package gop
import "time"
var (
defaultMaxWorkers = 10
defaultUnstoppableWorkers = 10
defaultWorkerTTL = time.Second
)
// Config is a configuration parameters for pool.
type Config struct {
// MaxWorkers is a number of workers run until the pool is stopped.
// In this count also included extra workers that added if the pool
// queue runs out of limit.
MaxWorkers int
// UnstoppableWorkers is a number of workers that run
// forever until the pool is stopped.
UnstoppableWorkers int
// MaxQueueSize defines maximum work
// queue size. If MaxQueueSize is 0, then queue is unlimited.
MaxQueueSize int
// ExtraWorkerTTL determines the timeout after which
// extra worker shuts down.
ExtraWorkerTTL time.Duration
// TaskScheduleTimeout determines the timeout
// for task to be added to the queue.
TaskScheduleTimeout time.Duration
// OnTaskTaken determines a callback is called after any task
// from queue is taken.
OnTaskTaken func()
// OnTaskFinished determines a callback is called after any task
// is completed.
OnTaskFinished func()
// OnExtraWorkerSpawned determines a callback is called after
// extra worker is spawned.
OnExtraWorkerSpawned func()
// OnExtraWorkerFinished determines a callback is called
// after extra worker is finished.
OnExtraWorkerFinished func()
}
func (c Config) withDefaults() Config {
if c.MaxWorkers == 0 {
c.MaxWorkers = defaultMaxWorkers
}
if c.ExtraWorkerTTL == 0 {
c.ExtraWorkerTTL = defaultWorkerTTL
}
if c.OnTaskTaken == nil {
c.OnTaskTaken = func() {}
}
if c.OnTaskFinished == nil {
c.OnTaskFinished = func() {}
}
if c.OnExtraWorkerSpawned == nil {
c.OnExtraWorkerSpawned = func() {}
}
if c.OnExtraWorkerFinished == nil {
c.OnExtraWorkerFinished = func() {}
}
return c
}