Documentation
¶
Overview ¶
Package dispatch delivers due tasks over HTTP.
Exactly one dispatcher is active at a time (see store.Dispatch.TryLead); other replicas stand by and take over if the leader dies. The leader runs many deliveries concurrently. Delivery is at-least-once: a task whose delivery outcome is unknown (crash, shutdown) is delivered again.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DialControl ¶
DialControl vets a delivery connection's resolved address before it's made; see net.Dialer.Control.
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
func New ¶
func New(opts Options) *Dispatcher
type Options ¶
type Options struct {
Store store.Store
Logger *slog.Logger
SigningSecret []byte
// PreviousSigningSecret, if set, also signs deliveries while endpoints
// move to SigningSecret.
PreviousSigningSecret []byte
// SigningSecrets, if set, signs each workspace's deliveries with its own
// secrets instead.
SigningSecrets SecretsFunc
UserAgent string
// MaxInFlight caps concurrent deliveries across all queues. Queues can
// set lower limits of their own. Default 100.
MaxInFlight int
// WorkspaceMaxInFlight, if set, returns a workspace's own cap on
// concurrent deliveries, within MaxInFlight; 0 means none.
WorkspaceMaxInFlight func(ctx context.Context, ws workspace.ID) (int, error)
// MaxRunsInFlight caps concurrent deliveries of schedule runs,
// separately from MaxInFlight. Default 20.
MaxRunsInFlight int
// PollInterval is the longest the dispatcher sleeps without checking for
// due tasks, as a fallback for missed wake-ups. Default 1s.
PollInterval time.Duration
// LeaseGrace is added to a task's timeout to form its lease. If the lease
// expires the task is presumed lost and redelivered. A leader never reaps
// its own deliveries, so this only needs to cover a previous leader
// finishing up after losing leadership. Default 10s.
LeaseGrace time.Duration
// ReapInterval is how often expired leases are checked. Default 10s.
ReapInterval time.Duration
// LeaderRetryInterval is how often a standby tries to take over. Default 5s.
LeaderRetryInterval time.Duration
// ShutdownTimeout bounds how long in-flight deliveries may finish after
// shutdown starts; the rest are interrupted and redelivered. Default 25s.
ShutdownTimeout time.Duration
// Retention is how long finished tasks (succeeded, failed or cancelled)
// are kept before they are deleted with their attempts. 0 keeps them
// forever.
Retention time.Duration
// RunRetention is how long finished schedule runs are kept; each
// schedule's latest run is kept regardless. 0 keeps them forever.
RunRetention time.Duration
// WorkspaceRetention, if set, returns the workspaces whose finished
// tasks, groups and runs are kept for their own duration instead (0
// keeps them forever). Called once per sweep; if it fails, those
// workspaces aren't known, so nothing is deleted that sweep.
WorkspaceRetention func(ctx context.Context) (map[workspace.ID]time.Duration, error)
// SweepInterval is how often finished tasks and runs past retention are
// deleted. Default 1m.
SweepInterval time.Duration
// Client defaults to a client that doesn't follow redirects, and whose
// connections DialControl vets.
Client *http.Client
DialControl DialControl
// Metrics defaults to a private set that nothing serves.
Metrics *metrics.Workly
// Alerts, if set, runs while this dispatcher leads, so alerts are sent
// from one process.
Alerts interface{ Run(ctx context.Context) }
// Changed, if set, is told when a workspace's tasks or runs change
// state, so dashboards can refresh. It must not block.
Changed func(workspace.ID)
// Executed, if set, is told of each execution: the first delivery
// attempt of a task or schedule run since it was created or retried by
// hand, once the attempt is done. A schedule run that starts a workflow
// isn't one (its steps are). It must not block for long: it is called
// from the delivery's goroutine.
Executed func(ctx context.Context, ws workspace.ID, kind Kind)
// AutoPaused, if set, is told when a queue pauses itself after failures
// in a row, and when it resumes after a success. It must not block.
AutoPaused func(store.AutoPauseEvent)
// FirstProbeDelay is how long after an auto-pause the first probe goes.
// Each failed probe doubles it, up to MaxProbeDelay. Defaults 30s and
// 10m.
FirstProbeDelay time.Duration
MaxProbeDelay time.Duration
}
type SecretsFunc ¶
SecretsFunc returns the secrets that sign a workspace's deliveries: the current one, and the previous one during a rotation (or nil).
type Signer ¶
type Signer struct {
// contains filtered or unexported fields
}
Signer sets the headers every delivery carries. The API uses it too, to show a task's delivery request exactly as the dispatcher sends it.
func NewSigner ¶
func NewSigner(secret, previousSecret []byte, userAgent string, lookup SecretsFunc) Signer
NewSigner signs with secret and previousSecret, or, if lookup is set, with each workspace's own secrets.
func (Signer) Header ¶
func (s Signer) Header(ctx context.Context, t store.Task, attempt int, now time.Time) (http.Header, error)
Header returns the headers of attempt number of t, sent at now: the task's own, then Workly's.
func (Signer) RunHeader ¶
func (s Signer) RunHeader(ctx context.Context, r store.Run, attempt int, now time.Time) (http.Header, error)
RunHeader returns the headers of attempt number of a schedule run, sent at now: the run's own, then Workly's. Runs are told apart from tasks by Workly-Run-Id, and carry the slot they are for.