-
Notifications
You must be signed in to change notification settings - Fork 17
/
options.go
156 lines (135 loc) · 4.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package machine
import (
"time"
)
// goOpts holds options for creating a goroutine. It is configured via GoOpt functions.
type goOpts struct {
id string
tags []string
timeout *time.Duration
deadline *time.Time
middlewares []Middleware
key interface{}
val interface{}
}
// GoOpt is a function that configures GoOpts
type GoOpt func(o *goOpts)
// GoWithTags is a GoOpt that adds an array of strings as "tags" to the Routine.
func GoWithTags(tags ...string) GoOpt {
return func(o *goOpts) {
o.tags = append(o.tags, tags...)
}
}
// GoWithPID is a GoOpt that sets/overrides the process ID of the Routine. A random id is assigned if this option is not used.
func GoWithPID(id string) GoOpt {
return func(o *goOpts) {
o.id = id
}
}
// GoWithTimeout is a GoOpt that creates the Routine's context with the given timeout value
func GoWithTimeout(to time.Duration) GoOpt {
return func(o *goOpts) {
o.timeout = &to
}
}
// GoWithDeadline is a GoOpt that creates the Routine's context with the given deadline.
func GoWithDeadline(deadline time.Time) GoOpt {
return func(o *goOpts) {
o.deadline = &deadline
}
}
// GoWithMiddlewares wraps the gived function with the input middlewares.
func GoWithMiddlewares(middlewares ...Middleware) GoOpt {
return func(o *goOpts) {
o.middlewares = append(o.middlewares, middlewares...)
}
}
// GoWithValues adds the k/v to the routine's root context. It can be retrieved with routine.Context().Value()
func GoWithValues(key, val interface{}) GoOpt {
return func(o *goOpts) {
o.key = key
o.val = val
}
}
// opts are options when creating a machine instance
type option struct {
id string
// MaxRoutines throttles goroutines at the given count
maxRoutines int
children []*Machine
middlewares []Middleware
pubsub PubSub
cache Cache
tags []string
key interface{}
val interface{}
timeout time.Duration
deadline time.Time
}
// 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
}
}
// WithPubSub sets the pubsub implementation for the machine instance. An inmemory implementation is used if none is provided.
func WithPubSub(pubsub PubSub) Opt {
return func(o *option) {
o.pubsub = pubsub
}
}
// WithCache sets the cache implementation for the machine instance. An inmemory implementation is used if none is provided.
func WithCache(cache Cache) Opt {
return func(o *option) {
o.cache = cache
}
}
// WithChildren sets the machine instances children
func WithChildren(children ...*Machine) Opt {
return func(o *option) {
o.children = append(o.children, children...)
}
}
// WithMiddlewares wraps every goroutine function executed by the machine with the given middlewares.
// Middlewares can be added to individual goroutines with GoWithMiddlewares
func WithMiddlewares(middlewares ...Middleware) Opt {
return func(o *option) {
o.middlewares = append(o.middlewares, middlewares...)
}
}
// WithTags sets the machine instances tags
func WithTags(tags ...string) Opt {
return func(o *option) {
o.tags = append(o.tags, tags...)
}
}
// WithValue adds the k/v to the Machine's root context. It can be retrieved with context.Value() in all sub routine contexts
func WithValue(key, val interface{}) Opt {
return func(o *option) {
o.key = key
o.val = val
}
}
// WithTimeout is an Opt that creates the Machine's context with the given timeout value
func WithTimeout(to time.Duration) Opt {
return func(o *option) {
o.timeout = to
}
}
// WithDeadline is an Opt that creates the Machine's context with the given deadline.
func WithDeadline(deadline time.Time) Opt {
return func(o *option) {
o.deadline = deadline
}
}
// WithID sets the machine instances unique id. If one isn't provided, a unique id will be assigned
func WithID(id string) Opt {
return func(o *option) {
o.id = id
}
}