dispatch

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

This section is empty.

Types

type DialControl

type DialControl = func(network, address string, c syscall.RawConn) error

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

func (*Dispatcher) Run

func (d *Dispatcher) Run(ctx context.Context) error

Run dispatches while leader, and stands by otherwise, until ctx is cancelled. It then lets in-flight deliveries finish (up to ShutdownTimeout) before returning.

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
	// 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)
}

type SecretsFunc

type SecretsFunc = func(ctx context.Context, ws workspace.ID) (current, previous []byte, err error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL