-
Notifications
You must be signed in to change notification settings - Fork 16
/
options.go
77 lines (65 loc) · 2.04 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package machine
import "time"
// goOpts holds options for creating a goroutine. It is configured via GoOpt functions.
type goOpts struct {
id int
tags []string
timeout *time.Duration
}
// GoOpt is a function that configures GoOpts
type GoOpt func(o *goOpts)
// WithTags is a GoOpt that adds an array of strings as "tags" to the Routine.
func WithTags(tags ...string) GoOpt {
return func(o *goOpts) {
o.tags = append(o.tags, tags...)
}
}
// WithPID is a GoOpt that sets/overrides the process ID of the Routine. A random id is assigned if this option is not used.
func WithPID(id int) GoOpt {
return func(o *goOpts) {
o.id = id
}
}
// WithTimeout is a GoOpt that creates the Routine's context with the given timeout value
func WithTimeout(to time.Duration) GoOpt {
return func(o *goOpts) {
o.timeout = &to
}
}
// opts are options when creating a machine instance
type option struct {
middlewares []Middleware
// MaxRoutines throttles goroutines at the given count
maxRoutines int
subChannelLength int
cache Cache
}
// Opt is a single option when creating a machine instance with New
type Opt func(o *option)
// WithMaxRoutines throttles goroutines at the input number. It will panic if <= zero.
func WithMaxRoutines(max int) Opt {
return func(o *option) {
if max <= 0 {
panic("max routines must be greater than zero!")
}
o.maxRoutines = max
}
}
// WithSubscribeChannelBuffer sets the buffer length of the channel returned from a Routine subscribeTo
func WithSubscribeChannelBuffer(length int) Opt {
return func(o *option) {
o.subChannelLength = length
}
}
// WithMiddlewares adds middlewares to the machine that will wrap every machine.Go Func that is executed by the machine instance.
func WithMiddlewares(middlewares ...Middleware) Opt {
return func(o *option) {
o.middlewares = append(o.middlewares, middlewares...)
}
}
// WithCache sets the in memory, concurrency safe cache. If not set, a default sync.Map implementation is used.
func WithCache(cache Cache) Opt {
return func(o *option) {
o.cache = cache
}
}