-
Notifications
You must be signed in to change notification settings - Fork 24
/
options.go
89 lines (72 loc) · 2.91 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
package event
import (
"sync/atomic"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/workerpool"
)
// WithMaxTriggerCount sets the maximum number of times an entity shall be triggered.
func WithMaxTriggerCount(maxTriggerCount uint64) Option {
return func(triggerSettings *triggerSettings) {
triggerSettings.maxTriggerCount = maxTriggerCount
}
}
// WithWorkerPool sets the worker pool that is used to process the trigger (nil forces execution in-place).
func WithWorkerPool(workerPool *workerpool.WorkerPool) Option {
if workerPool == nil {
return func(triggerSettings *triggerSettings) {
triggerSettings.workerPool = noWorkerPool
}
}
return func(triggerSettings *triggerSettings) {
triggerSettings.workerPool = workerPool
}
}
// WithPreTriggerFunc sets a function that is synchronously called before the trigger is executed.
func WithPreTriggerFunc(preTriggerFunc any) Option {
return func(triggerSettings *triggerSettings) {
triggerSettings.preTriggerFunc = preTriggerFunc
}
}
// triggerSettings is a struct that contains trigger related settings and logic.
type triggerSettings struct {
workerPool *workerpool.WorkerPool
triggerCount atomic.Uint64
maxTriggerCount uint64
preTriggerFunc any
}
// WasTriggered returns true if Trigger was called at least once.
func (t *triggerSettings) WasTriggered() bool {
return t.triggerCount.Load() > 0
}
// TriggerCount returns the number of times Trigger was called.
func (t *triggerSettings) TriggerCount() int {
return int(t.triggerCount.Load())
}
// MaxTriggerCount returns the maximum number of times Trigger can be called.
func (t *triggerSettings) MaxTriggerCount() int {
return int(t.maxTriggerCount)
}
// MaxTriggerCountReached returns true if the maximum number of times Trigger can be called was reached.
func (t *triggerSettings) MaxTriggerCountReached() bool {
return t.maxTriggerCount != 0 && t.triggerCount.Load() > t.maxTriggerCount
}
// WorkerPool returns the worker pool that shall be used to execute the triggered function.
func (t *triggerSettings) WorkerPool() *workerpool.WorkerPool {
return lo.Return2(t.hasWorkerPool())
}
// hasWorkerPool returns if a worker pool (and which one) is set.
func (t *triggerSettings) hasWorkerPool() (bool, *workerpool.WorkerPool) {
if t.workerPool == noWorkerPool {
return true, nil
}
return t.workerPool != nil, t.workerPool
}
// currentTriggerExceedsMaxTriggerCount returns true if the maximum number of times Trigger shall be called was reached.
func (t *triggerSettings) currentTriggerExceedsMaxTriggerCount() bool {
return t.triggerCount.Add(1) > t.maxTriggerCount && t.maxTriggerCount != 0
}
// noWorkerPool is a special value that indicates that no worker pool shall be used (forced).
var noWorkerPool = &workerpool.WorkerPool{}
// Option is a function that configures the triggerSettings.
type Option = options.Option[triggerSettings]