forked from go-pkgz/syncs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_options.go
More file actions
37 lines (30 loc) · 889 Bytes
/
group_options.go
File metadata and controls
37 lines (30 loc) · 889 Bytes
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
package syncs
import "context"
type options struct {
ctx context.Context
cancel context.CancelFunc
preLock bool
termOnError bool
discardIfFull bool
}
// GroupOption functional option type
type GroupOption func(o *options)
// Context passes ctx and makes it cancelable
func Context(ctx context.Context) GroupOption {
return func(o *options) {
o.ctx, o.cancel = context.WithCancel(ctx)
}
}
// Preemptive sets locking mode preventing spawning waiting goroutine. May cause Go call to block!
func Preemptive(o *options) {
o.preLock = true
}
// TermOnErr prevents new goroutines to start after first error
func TermOnErr(o *options) {
o.termOnError = true
}
// Discard will discard new goroutines if semaphore is full, i.e. no more goroutines allowed
func Discard(o *options) {
o.discardIfFull = true
o.preLock = true // discard implies preemptive
}