dispatch

package
v0.1.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package dispatch implements the task dispatcher: an in-memory active-queue scheduler (min-heap + token buckets), budgeted claims against the store, HTTP delivery workers, the lease sweeper, and retention cleanup.

Correctness never depends on this process being the only dispatcher — every completion is fenced by the task's lease token. Leadership (when a LeaderElector is configured) is purely an optimization to avoid duplicate claim scans.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff

func Backoff(rc store.RetryConfig, attempts int) time.Duration

Backoff returns the delay before the next attempt, given that `attempts` attempts have completed. GCP Cloud Tasks semantics: the interval starts at MinBackoff, doubles MaxDoublings times, then grows linearly by 2^MaxDoublings × MinBackoff per retry, capped at MaxBackoff.

Example (GCP docs): min=10s max=300s doublings=3 → 10, 20, 40, 80, 160, 240, 300, 300, …

func Exhausted

func Exhausted(rc store.RetryConfig, attempts int, firstAttempt *time.Time, now time.Time) bool

Exhausted decides whether retries stop after `attempts` completed attempts. GCP quirk adopted for parity: when BOTH limits are set, the task keeps retrying until BOTH are reached. An unlimited value (-1 attempts / 0 duration) leaves the other limit in sole control.

Types

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

func New

func New(st store.Store, opts Options, log *slog.Logger, met *metrics.Metrics) *Dispatcher

func (*Dispatcher) Role

func (d *Dispatcher) Role() string

func (*Dispatcher) Start

func (d *Dispatcher) Start(ctx context.Context)

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop resigns leadership, drains in-flight dispatches (up to DrainTimeout), and blocks until the loop exits.

type LeaderElector

type LeaderElector interface {
	// Acquire blocks until leadership is held or ctx ends. The returned
	// channel closes if leadership is later lost.
	Acquire(ctx context.Context) (lost <-chan struct{}, err error)
	// Release gives leadership up (e.g. on graceful shutdown).
	Release()
}

LeaderElector serializes the dispatcher role across replicas (Postgres advisory lock in production).

type Options

type Options struct {
	GlobalConcurrency  int
	PerQueueClaimCap   int
	ReconcileInterval  time.Duration
	SweepInterval      time.Duration
	CleanupInterval    time.Duration
	LeaseGrace         time.Duration
	DrainTimeout       time.Duration
	RetentionSucceeded time.Duration
	RetentionFailed    time.Duration
	DedupWindow        time.Duration

	Clock      clockwork.Clock // nil = real clock
	HTTPClient *http.Client    // nil = tuned default
	Leader     LeaderElector   // nil = always leader (single node)
}

Jump to

Keyboard shortcuts

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