-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
52 lines (44 loc) · 1.47 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
// -----------------------------------------------------------------------------
// worker for running tasks enqueued in background (options.go)
//
// Copyright (C) 2024 Frank Mueller / Oldenburg / Germany / World
// -----------------------------------------------------------------------------
package worker // import "tideland.dev/go/worker"
import "time"
// Option defines the signature of an option setting function.
type Option func(act *Worker) error
const defaultRate = int(time.Second)
const defaultBurst = 0
// WithRateBurst sets the rate and the burst values of a worker. Rate is the
// number of tasks processed per second, burst the number of tasks processed
// at once (means enqueued to the worker at ounce). If burst is 0 the rate
// is used as burst.
//
// Be aware that a high rate and burst can lead to a high CPU load.
func WithRateBurst(rate, burst int) Option {
return func(w *Worker) error {
if rate < 0 {
rate = defaultRate
}
if burst < 0 {
burst = defaultBurst
}
w.rate = rate
w.burst = burst
return nil
}
}
// WithTimeout sets the timeout for different actions like enqueueing a task.
const defaultTimeout = 5 * time.Second
func WithTimeout(d time.Duration) Option {
return func(w *Worker) error {
if d < 0 {
d = defaultTimeout
}
w.timeout = d
return nil
}
}
// -----------------------------------------------------------------------------
// end of file
// -----------------------------------------------------------------------------