Documentation
¶
Overview ¶
Package coalesce bounds how many review records a burst of pushes to one pull request can create.
The mechanism is a trailing coalesce: the first push of a burst is dispatched immediately, so a lone push pays no added latency at all; every push behind it collapses into a single trailing dispatch that fires once the PR has been quiet for the hold window. A six-push burst therefore yields at most two records — the leading one and the trailing one — against the five the pipeline produced before this package existed (bborbe-nuke#815, 2026-10-27).
A full debounce would yield exactly one record, but it must hold the first push too, which adds the hold window to every PR including a lone one; a 60 s hold roughly doubles the measured p50 of 36 s and pushes the goal's p90 past its 10-minute bar. That trade is why the leading edge is unconditional.
Nothing is recorded as emitted until the caller confirms the publish with MarkEmitted. A decision is therefore a decision, not a commitment: a failed send leaves the head unclaimed so the next webhook retry or the next poll re-admits it, instead of a lost record being silently recorded as done.
Index ¶
Constants ¶
const MaxHold = stdtime.Hour
MaxHold is the largest accepted hold window. Beyond it a misconfiguration silently starves every burst of its trailing record rather than failing.
Variables ¶
This section is empty.
Functions ¶
func ValidateHold ¶
ValidateHold rejects a hold window that cannot bound a burst, or one so large that a misconfiguration would silently starve every burst of its trailing record: a non-positive hold would admit every push, which is the behaviour this package exists to remove.
Types ¶
type Coalescer ¶
type Coalescer interface {
// Push is the webhook-side entry point for a push delivery. It returns true
// when the caller must publish now (the leading edge) and false when the
// push was coalesced and a single trailing dispatch is scheduled for hold
// after the last push seen for this PR.
Push(key Key, headSHA string, url string, now libtime.DateTime) bool
// Admit is the poll-side entry point. pushAt is the time of the push that
// produced headSHA — GitHub's updated_at — so the poll can tell a quiet head
// from one still inside a burst.
Admit(key Key, headSHA string, pushAt libtime.DateTime, now libtime.DateTime) Decision
// MarkEmitted records that a record for headSHA was actually published.
// Callers invoke it only after a successful publish, so a failed send
// leaves the head claimable.
MarkEmitted(key Key, headSHA string, now libtime.DateTime)
// Close stops every scheduled trailing dispatch. Call it on shutdown.
Close()
}
Coalescer decides whether a review record may be emitted for a PR's head.
Both emit paths consult the same instance, because a gate on either alone still lets a burst produce a third record through the other: the webhook receiver emits per push, and the poll loop emits per (PR, SHA) it has not seen before.
func NewCoalescer ¶
func NewCoalescer( ctx context.Context, hold stdtime.Duration, scheduler Scheduler, dispatch DispatchFunc, clock libtime.CurrentDateTimeGetter, ) Coalescer
NewCoalescer returns a Coalescer with the given hold window.
ctx is the process lifetime the trailing dispatch runs under; the dispatch never runs on context.Background(), so shutting the watcher down cancels a pending trailing publish rather than leaving it in flight. hold is how long a PR must stay quiet before its trailing dispatch fires; it should exceed the gap between pushes inside a burst and no more. dispatch is invoked from a timer goroutine, never from the caller's goroutine.
type Decision ¶
type Decision int
Decision is what a Coalescer tells a caller to do with one observation.
It is deliberately three-valued. A boolean conflates "hold this head for the trailing dispatch" with "this head was already published", and the two demand opposite cursor handling: a held head must not advance the poll cursor (it is still owed a record), while an already-published head must, or the search window pins to that PR for the process lifetime.
const ( // Emit means publish a record for this head now, then call MarkEmitted. Emit Decision = iota // Hold means the head is being coalesced into the trailing dispatch: // publish nothing, retry on the next cycle, and do not advance the cursor // past this PR. Hold // Settled means a record for this head was already published: publish // nothing, and treat the PR as handled so the cursor advances. Settled )
type DispatchFunc ¶
DispatchFunc publishes a review request for one PR URL. It is the webhook receiver's own dispatch path, injected so the trailing edge reuses it rather than duplicating the command construction.
type Key ¶
Key identifies one pull request across every emit path. The poll loop and the webhook receiver build it independently, so both must agree on the fields.
type Scheduler ¶
Scheduler schedules a one-shot callback. The production implementation is stdtime.AfterFunc; tests inject a manual scheduler so no test sleeps.
func NewTimerScheduler ¶
func NewTimerScheduler() Scheduler
NewTimerScheduler returns the production Scheduler, backed by time.AfterFunc.