options.go

v0.6.0
Doc Versions Source
1
package chronos
2
3
import (
4
	"log/slog"
5
	"time"
6
)
7
8
// supervisorOpts holds the resolved configuration of a [Supervisor],
9
// assembled from the defaults and the [Option] values passed to [New].
10
type supervisorOpts struct {
11
	loc         *time.Location
12
	logger      *slog.Logger
13
	timeout     time.Duration
14
	maxInflight int
15
	slotWait    time.Duration
16
	clock       Clock
17
	jitter      func(max time.Duration) time.Duration
18
}
19
20
// Option configures a [Supervisor] at construction time.
21
type Option func(*supervisorOpts)
22
23
// WithLocation sets the time zone schedules are evaluated in (AS2). The
24
// default is [time.Local].
25
func WithLocation(loc *time.Location) Option {
26
	return func(o *supervisorOpts) { o.loc = loc }
27
}
28
29
// WithLogger sets the logger used for all supervisor logging (PC4). The
30
// default is [slog.Default].
31
func WithLogger(l *slog.Logger) Option {
32
	return func(o *supervisorOpts) { o.logger = l }
33
}
34
35
// WithTimeout sets the default per-run timeout applied to jobs that do
36
// not set their own with [Job.Timeout]. Zero means no timeout.
37
func WithTimeout(d time.Duration) Option {
38
	return func(o *supervisorOpts) { o.timeout = d }
39
}
40
41
// WithMaxInflight bounds the number of runs executing concurrently
42
// across all jobs (IV9). n<=0 is unlimited.
43
func WithMaxInflight(n int) Option {
44
	return func(o *supervisorOpts) { o.maxInflight = n }
45
}
46
47
// WithSlotWait sets the default slot-wait applied to jobs that do not
48
// set their own with [Job.SlotWait] (IV10): 0 sheds when full, <0 waits
49
// indefinitely, >0 waits up to the duration.
50
func WithSlotWait(d time.Duration) Option {
51
	return func(o *supervisorOpts) { o.slotWait = d }
52
}
53
54
// WithClock injects a [Clock], chiefly for deterministic tests. The
55
// default is the package wall clock.
56
func WithClock(c Clock) Option {
57
	return func(o *supervisorOpts) { o.clock = c }
58
}
59
60
// WithJitter injects the source of per-fire jitter spread (PC3): given a
61
// job's [Job.Delta], it returns the offset added to that fire, expected in
62
// [0, max). The default draws uniformly from math/rand/v2. Chiefly for
63
// deterministic tests.
64
func WithJitter(fn func(max time.Duration) time.Duration) Option {
65
	return func(o *supervisorOpts) { o.jitter = fn }
66
}
67

Source Files