dispatch

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 3 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 decides which work items a scheduling tick may start (方案 §4.8/§7.4): the ordering, the concurrency gates and the preconditions, as a pure function of the state the caller read.

Nothing here writes and nothing here starts anything: the application service performs recovery, calls Plan, and starts the attempts it returns. Keeping the decision pure is what makes the tick reproducible — the same state yields the same plan, and a test can state the expectation exactly.

Index

Constants

View Source
const (
	ReasonNotDispatchable = "not_dispatchable"
	ReasonLeaseActive     = "lease_active"
	ReasonRetryNotDue     = "retry_not_due"
	ReasonBlockedBy       = "blocked_by"
	ReasonMaxReached      = "max_reached"
	ReasonCapGlobal       = "cap_global"
	ReasonCapStatus       = "cap_status"
)

Skip reasons, in the order the planner checks them.

View Source
const DefaultGlobalCap = 1

DefaultGlobalCap is the concurrency bound a tick enforces when no policy declares one: a policy that says nothing runs one attempt at a time.

Variables

This section is empty.

Functions

This section is empty.

Types

type Caps

type Caps struct {
	Global    int
	PerStatus map[string]int
}

Caps are the concurrency bounds a tick enforces. Global is the number of attempts that may be in flight at once; PerStatus bounds each work item status. A zero or negative bound means "not declared"; the caller resolves the policy defaults before calling Plan.

func ResolveCaps

func ResolveCaps(policies []Caps) Caps

ResolveCaps folds the declared concurrency of the given policies into the bounds one tick enforces: the strictest declaration wins, and a policy that declares nothing counts as the default — so a silent policy pins the queue to one at a time rather than letting a looser neighbour widen it. Each status keeps the strictest bound declared for it.

type Decision

type Decision struct {
	WorkitemID string    `json:"workitem_id"`
	Action     string    `json:"action"` // start | skip
	Reason     string    `json:"reason,omitempty"`
	Priority   int       `json:"priority"`
	Status     string    `json:"status"`
	CreatedAt  time.Time `json:"created_at"`
}

Decision is what the planner decided about one work item.

type Input

type Input struct {
	Now   time.Time
	Items []*domain.WorkItem
	Caps  Caps
	// Max additionally bounds how many attempts this tick starts (0 = only
	// the caps apply).
	Max int
}

Input is everything the plan depends on.

type Report

type Report struct {
	Start []Decision `json:"start"`
	Skip  []Decision `json:"skip"`
	Caps  Caps       `json:"caps"`
	// InFlight and InFlightByStatus describe what the tick found already
	// running; they are not updated by the plan's own decisions.
	InFlight         int            `json:"in_flight"`
	InFlightByStatus map[string]int `json:"in_flight_by_status"`
}

Report is one tick's plan.

func Plan

func Plan(in Input) Report

Plan orders the dispatchable work items and applies the gates.

Order (方案 §7.4): priority descending, then oldest created_at, then the identifier — a total order, so two ticks over the same state decide the same way. Preconditions are checked before the caps: a blocked or not-yet-due item is reported as such rather than as an over-cap item, because that is the reason an operator can act on.

Jump to

Keyboard shortcuts

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