-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathoption.go
99 lines (84 loc) · 3.39 KB
/
option.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
package worker
import "github.com/ankorstore/yokai/generate/uuid"
const (
DefaultDeferredStartThreshold = 0
DefaultMaxExecutionsAttempts = 1
DefaultMetricsNamespace = ""
DefaultMetricsSubsystem = ""
)
// PoolOptions are options for the [WorkerPoolFactory] implementations.
type PoolOptions struct {
GlobalDeferredStartThreshold float64
GlobalMaxExecutionsAttempts int
Metrics *WorkerMetrics
Generator uuid.UuidGenerator
Registrations map[string]*WorkerRegistration
}
// DefaultWorkerPoolOptions are the default options used in the [DefaultWorkerPoolFactory].
func DefaultWorkerPoolOptions() PoolOptions {
return PoolOptions{
GlobalDeferredStartThreshold: DefaultDeferredStartThreshold,
GlobalMaxExecutionsAttempts: DefaultMaxExecutionsAttempts,
Metrics: NewWorkerMetrics(DefaultMetricsNamespace, DefaultMetricsSubsystem),
Generator: uuid.NewDefaultUuidGenerator(),
Registrations: make(map[string]*WorkerRegistration),
}
}
// WorkerPoolOption are functional options for the [WorkerPoolFactory] implementations.
type WorkerPoolOption func(o *PoolOptions)
// WithGlobalDeferredStartThreshold is used to specify the global workers deferred start threshold, in seconds.
func WithGlobalDeferredStartThreshold(threshold float64) WorkerPoolOption {
return func(o *PoolOptions) {
o.GlobalDeferredStartThreshold = threshold
}
}
// WithGlobalMaxExecutionsAttempts is used to specify the global workers max execution attempts.
func WithGlobalMaxExecutionsAttempts(max int) WorkerPoolOption {
return func(o *PoolOptions) {
o.GlobalMaxExecutionsAttempts = max
}
}
// WithMetrics is used to specify the [WorkerMetrics] to use by the [WorkerPool].
func WithMetrics(metrics *WorkerMetrics) WorkerPoolOption {
return func(o *PoolOptions) {
o.Metrics = metrics
}
}
// WithGenerator is used to specify the [uuid.UuidGenerator] to use by the [WorkerPool].
func WithGenerator(generator uuid.UuidGenerator) WorkerPoolOption {
return func(o *PoolOptions) {
o.Generator = generator
}
}
// WithWorker is used to register a [Worker] in the [WorkerPool], with an optional list of [WorkerPoolOption].
func WithWorker(worker Worker, options ...WorkerExecutionOption) WorkerPoolOption {
return func(o *PoolOptions) {
o.Registrations[worker.Name()] = NewWorkerRegistration(worker, options...)
}
}
// ExecutionOptions are options for the [Worker] executions.
type ExecutionOptions struct {
DeferredStartThreshold float64
MaxExecutionsAttempts int
}
// DefaultWorkerExecutionOptions are the default options for the [Worker] executions.
func DefaultWorkerExecutionOptions() ExecutionOptions {
return ExecutionOptions{
DeferredStartThreshold: DefaultDeferredStartThreshold,
MaxExecutionsAttempts: DefaultMaxExecutionsAttempts,
}
}
// WorkerExecutionOption are functional options for the [Worker] executions.
type WorkerExecutionOption func(o *ExecutionOptions)
// WithDeferredStartThreshold is used to specify the worker deferred start threshold, in seconds.
func WithDeferredStartThreshold(t float64) WorkerExecutionOption {
return func(o *ExecutionOptions) {
o.DeferredStartThreshold = t
}
}
// WithMaxExecutionsAttempts is used to specify the worker max execution attempts.
func WithMaxExecutionsAttempts(l int) WorkerExecutionOption {
return func(o *ExecutionOptions) {
o.MaxExecutionsAttempts = l
}
}