coalesce

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-2-Clause Imports: 7 Imported by: 1 module, 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 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

View Source
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

func ValidateHold(ctx context.Context, hold stdtime.Duration) error

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)

	// Release drops the claim MarkEmitted recorded for headSHA, so a head whose
	// record was never published becomes claimable again.
	//
	// It exists because one caller cannot honour MarkEmitted's contract above.
	// The webhook handler claims a head as soon as its command reaches Kafka,
	// which is correct — that claim is what stops the poll path publishing a
	// second record for a head the webhook already dispatched. But the command
	// is consumed downstream, and the consumer may decline it: the
	// mechanical-override deferral does exactly that for every trivial bot
	// bump. The claim then describes a publish that never happened, and
	// because Admit reports Settled for it, the poll path publishes nothing
	// either — the head is stranded until the unreviewed scanner's threshold
	// fires and spends the full LLM review this feature exists to remove.
	//
	// A release that names a head the entry no longer holds is ignored, so a
	// late release can never unclaim a head that was published afterwards.
	Release(key Key, headSHA string)

	// 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
)

func (Decision) String

func (d Decision) String() string

String renders the decision for logs.

type DispatchFunc

type DispatchFunc func(ctx context.Context, url string) error

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

type Key struct {
	Owner  string
	Repo   string
	Number int
}

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.

func (Key) String

func (k Key) String() string

String renders the key the way the pipeline logs a PR elsewhere.

type Scheduler

type Scheduler interface {
	AfterFunc(d stdtime.Duration, f func()) Timer
}

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.

type Timer

type Timer interface {
	// Stop prevents the callback from running if it has not run yet.
	Stop()
}

Timer is the handle a scheduled callback can be cancelled through.

Jump to

Keyboard shortcuts

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