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 ¶
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.
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 ¶
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 ¶
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 ¶
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.