timers

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: AGPL-3.0 Imports: 15 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 timers is the worker of the timer rows (C-09.FR-12, ADR-0006, ADR-0007): on every replica it claims the due timers of each Organization with FOR UPDATE SKIP LOCKED and a lease, fires each in a transaction of its own through the handler of its kind, and waits for the earliest deadline, a notification of a new timer or a move of the development clock. Deadlines are business times and leases real times; a timer overdue after downtime fires once, and a timer whose lease ran out is claimed again by any replica.

Index

Constants

View Source
const (
	Lease          = time.Minute
	Batch          = 100
	MaxWait        = 30 * time.Second
	MinWait        = 100 * time.Millisecond
	FailureBackoff = time.Second
)

The defaults of the worker: the lease of a claimed timer, the rows one claim takes, the longest wait between rounds when no deadline is nearer, which bounds what a missed notification costs, the shortest one, so that a due timer another replica is claiming right now is not polled in a tight loop, and the first wait after a failed round.

View Source
const AckTimeoutNotices = 3

AckTimeoutNotices is the most notices one ack timeout sends; the last makes the Alert Group Unclaimed (C-17.FR-2, FR-3).

View Source
const AutoUnacknowledgeAfter = 2

AutoUnacknowledgeAfter is timers.auto_unacknowledge_after: the unanswered Reminders in a row after which the next one auto-unacknowledges instead, where the Route has auto-unacknowledge on (C-17.FR-5).

Variables

This section is empty.

Functions

func AckNoticeDue

func AckNoticeDue(start time.Time, first time.Duration, n int64) time.Time

AckNoticeDue is when notice n (1 to AckTimeoutNotices) of an ack timeout started at start is due, with the first interval first: start + (2ⁿ − 1)·first, that is first, 3·first and 7·first after the start.

func AckNoticeGap

func AckNoticeGap(first time.Duration, n int64) time.Duration

AckNoticeGap is the interval before notice n: 2ⁿ⁻¹·first, that is first, 2·first and 4·first.

func DueAckNotices

func DueAckNotices(start time.Time, first time.Duration, sent int64, now time.Time) int64

DueAckNotices is how many notices of an ack timeout started at start, after the sent ones, are due at now: those whose time came, at most the notices left.

func DueReminders

func DueReminders(deadline time.Time, next int64, first, limit time.Duration, now time.Time) (int64, time.Time)

DueReminders counts the Reminders due at now when Reminder next is due at deadline: deadline itself, then each one interval of the following step after the one before, as long as it is not after now. It returns the count, zero when deadline is after now, and when the last of them was due. Once the interval reaches the cap the rest are counted at once, so that days of downtime are not walked one step at a time.

func ReminderInterval

func ReminderInterval(k int64, first, limit time.Duration) time.Duration

ReminderInterval is the interval before Reminder k (1, 2, …) after the one before it, or after the acknowledgement for the first: min(first·2ᵏ⁻¹, limit), limit being the cap of route.reminders. It stops doubling once the cap is reached, so a large k never overflows; a k below 1 is the first.

Types

type Handler

type Handler func(ctx context.Context, tx dbgen.DBTX, orgID int64, t Timer) (func(context.Context), error)

Handler fires one timer of its kind in the transaction tx of the timer, for the Organization orgID, and returns what to run once that transaction committed, or nil. A handler checks the state of its subject itself: a timer that is no longer due for it changes nothing.

type Store

type Store interface {
	// Claim leases at most limit due timers of the kinds of the Organization orgID.
	Claim(ctx context.Context, l db.Lease, orgID int64, kinds []string, limit int32) ([]Timer, error)
	// Fire runs h for a claimed timer in a transaction that still holds its lease, and returns what h returns.
	Fire(ctx context.Context, l db.Lease, orgID int64, t Timer, h Handler) (func(context.Context), error)
	// Next is how long until a timer of the kinds of the Organization can be claimed, at most limit: until the
	// earliest deadline of a free one on the business clock, or the end of the earliest lease on the real clock.
	Next(ctx context.Context, l db.Lease, orgID int64, kinds []string, limit time.Duration) (time.Duration, error)
}

Store claims, fires and schedules the timers.

func NewStore

func NewStore(pool *pgxpool.Pool) Store

NewStore is the Store over the main pool.

type Timer

type Timer struct {
	ID           int64
	AlertGroupID *int64
	StormID      *int64
	Kind         string
	Deadline     time.Time
	Attempts     int64
}

Timer is a claimed timer row.

type Worker

type Worker struct {
	Store Store
	// Lease names this replica in lease_owner and carries the clocks: due times on the business clock, leases on
	// the real clock.
	Lease db.Lease
	// Organizations lists the Organizations whose timers it fires: one in L1.
	Organizations func(ctx context.Context) ([]int64, error)
	// Handlers fire each kind; the worker claims only these kinds.
	Handlers map[string]Handler
	Log      *logging.Logger
	// MaxWait bounds the wait between rounds, and Batch the timers one claim takes; zero is the default.
	MaxWait time.Duration
	Batch   int32
	// Wait waits for d or until a wake arrives; nil waits on the real clock.
	Wait func(ctx context.Context, d time.Duration, wake <-chan struct{})
	// contains filtered or unexported fields
}

Worker fires the due timers of this replica.

func (*Worker) Round

func (w *Worker) Round(ctx context.Context) (time.Duration, error)

Round fires every due timer of the kinds it has handlers for in every Organization, claiming Batch at a time, and returns how long until the next one can be claimed, at most MaxWait. A timer that fails is logged and fires again once its lease runs out; the round goes on. Timers of other kinds wait for the release that fires them.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context)

Run fires timers until ctx ends: a round, then a wait until the next timer can be claimed — its deadline on the business clock or the end of its lease on the real clock — between MinWait and MaxWait, or until woken; after a failed round, a backoff.

func (*Worker) Wake

func (w *Worker) Wake()

Wake makes the worker run a round at once: a timer was set, the development clock moved, or the LISTEN is back.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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