webhook

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 27 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 webhook is the broker's outbound events (factory interfaces F6): id-only payloads, signed as Standard Webhooks sign them, so a subscriber verifies, rejects a stale timestamp and dedupes on webhook-id. Both the broker and the factory's Slack App use it.

Index

Constants

View Source
const Tolerance = 5 * time.Minute

Tolerance is how far a delivery's timestamp may stand from the receiver's clock (the design's five minutes), either way.

Variables

View Source
var ErrInvalid = errors.New("webhook: invalid delivery")

ErrInvalid is any delivery that does not verify; its text never says which check failed.

Functions

func Clip

func Clip(kind, v string) string

Clip is v when kind's vocabulary ("reason", "limit" or "state") holds it, "" for "", else "other": no value outside a closed list leaves the broker or reaches a Slack thread.

func Guard

func Guard(a netip.Addr) error

Guard refuses an address an internet subscriber may never resolve to (R20): loopback, private, link-local (cloud metadata included), CGNAT (the tailnet), unique-local and unspecified.

func ID

func ID(subscriber, room string, seq int64, typ string) string

ID is a delivery's webhook-id: the same for every retry and every leader, distinct per subscriber, so each subscriber dedupes its own stream (R15).

func Limits

func Limits() []string

Limits lists the values an event's limit may hold.

func ReadKeys

func ReadKeys(path string) ([][]byte, error)

ReadKeys reads one or two base64 keys, one per line (R16).

func Reasons

func Reasons() []string

Reasons lists the values an event's reason may hold: the wait reasons the summary has prose for, the run ends that escalate a task, the broker's approval, and the three escalations the factory raises on a room it cannot use. They repeat the factory's strings: this package imports nothing of the factory's reconciler, so a new factory reason needs a line here.

func SafeClient

func SafeClient(timeout time.Duration) *http.Client

SafeClient is an internet subscriber's client: every dialled address passes Guard after resolution, so a name that rebinds to a private address is refused at connect; no redirect is followed, since the address it names was never declared.

func Sign

func Sign(keys [][]byte, id string, ts time.Time, body []byte) http.Header

Sign is a delivery's three headers: one v1 signature per key, space-separated (R16).

func States

func States() []string

States lists the values an event's state may hold: the six coarse states.

func Types

func Types() []string

Types lists the events a subscriber may ask for.

func Verify

func Verify(keys [][]byte, h http.Header, body []byte, now time.Time) (string, error)

Verify checks a delivery against any of keys: the timestamp within Tolerance of now, then one v1 signature over id, timestamp and body, compared in constant time. It returns the webhook-id.

Types

type Emitter

type Emitter struct {
	Subs    []Subscriber
	Events  LogReader
	Rooms   RoomRepo
	State   StateStore
	Metrics Recorder
	Logger  *slog.Logger
	Now     func() time.Time
	// Sleep waits between retries; nil waits on a timer or ctx. A test steps it.
	Sleep func(ctx context.Context, d time.Duration) error
	// SweepEvery is how often the dirty rooms are retried; 0 means 10 s.
	SweepEvery time.Duration
	// MinBackoff is the first wait before retrying a failed registration or reconnect; it
	// doubles to 30 s. 0 means 1 s.
	MinBackoff time.Duration
	// FactoryPrincipal is the system principal whose task facts name a room's repository.
	FactoryPrincipal string
	// LaggingLimit is how many rooms one catch-up asks for; 0 means 1,000.
	LaggingLimit int
	// contains filtered or unexported fields
}

Emitter posts each subscriber the events of its repositories' rooms, from the log (F6).

func (*Emitter) NeedLeaderElection

func (e *Emitter) NeedLeaderElection() bool

NeedLeaderElection makes the emitter the leader's: two emitters would deliver everything twice.

func (*Emitter) Start

func (e *Emitter) Start(ctx context.Context) error

Start registers the subscribers, then delivers until ctx ends.

type Event

type Event struct {
	Type   string `json:"type"`
	Room   string `json:"room"`
	Repo   string `json:"repo,omitempty"`
	Run    string `json:"run,omitempty"`
	PR     int    `json:"pr,omitempty"`
	Reason string `json:"reason,omitempty"`
	Limit  string `json:"limit,omitempty"`
	State  string `json:"state,omitempty"`
	// Origin is the room of the task this room's task was started from (RunLore agent-to-agent
	// RA10), on every event of the room once its facts name one, so a subscriber correlates an
	// event it missed the start of.
	Origin string `json:"origin,omitempty"`
}

Event is one delivery's payload: ids, states and reason codes only (R19). No room text.

type LogReader

type LogReader interface {
	Range(ctx context.Context, roomID string, afterSeq int64, limit int) ([]envelope.Event, error)
	Listen(ctx context.Context, ready func(), fn func(roomID string, seq int64)) error
}

LogReader is the room log as the emitter reads it.

type Recorder

type Recorder interface {
	WebhookDelivery(ctx context.Context, subscriber, outcome string)
	WebhookDisabled(ctx context.Context, subscriber string, disabled bool)
}

Recorder counts deliveries and holds the disabled gauge.

type RoomRepo

type RoomRepo func(ctx context.Context, room string) (string, error)

RoomRepo is a room's repository: "" when the Room is not known (yet) or names none (M5).

type StateStore

type StateStore interface {
	Subscriber(ctx context.Context, sub string, generation int) (failures int, disabled bool, err error)
	WebhookCursor(ctx context.Context, sub, room string) (int64, error)
	SetWebhookCursor(ctx context.Context, sub, room string, seq int64) error
	Lagging(ctx context.Context, sub string, limit int) ([]string, error)
	Failed(ctx context.Context, sub string) (int, error)
	Succeeded(ctx context.Context, sub string) error
	Disable(ctx context.Context, sub string) error
}

StateStore is where each subscriber stands (R18) and whether it is disabled (R17).

type Subscriber

type Subscriber struct {
	Name, Network, URL, SecretFile string
	Events, Repositories           []string
	Generation                     int
}

Subscriber is one webhooks[] entry as the emitter needs it; internal/app maps the config's, since this package never imports config.

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker turns one room's log into one subscriber's events (R19). It is per subscriber and room (M1): what it has sent is that subscriber's, so two subscribers at different cursors never share it. The emitter warms it by replaying the room up to the subscriber's cursor.

func (*Tracker) Events

func (t *Tracker) Events(room, repo string, ev envelope.Event) []Event

Events is what ev means to a subscriber of room, whose repository is repo: none, one or a few events, each carrying ids, states and codes only. A message is never decoded (M3).

Jump to

Keyboard shortcuts

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