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
- Variables
- func Clip(kind, v string) string
- func Guard(a netip.Addr) error
- func ID(subscriber, room string, seq int64, typ string) string
- func Limits() []string
- func ReadKeys(path string) ([][]byte, error)
- func Reasons() []string
- func SafeClient(timeout time.Duration) *http.Client
- func Sign(keys [][]byte, id string, ts time.Time, body []byte) http.Header
- func States() []string
- func Types() []string
- func Verify(keys [][]byte, h http.Header, body []byte, now time.Time) (string, error)
- type Emitter
- type Event
- type LogReader
- type Recorder
- type RoomRepo
- type StateStore
- type Subscriber
- type Tracker
Constants ¶
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 ¶
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 ¶
Clip is v when kind's vocabulary ("reason", "limit", "state" or "verdict") holds it, "" for "", else "other": no value outside a closed list leaves the broker or reaches a Slack thread.
func Guard ¶
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 ¶
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 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 ¶
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.
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 ¶
NeedLeaderElection makes the emitter the leader's: two emitters would deliver everything twice.
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"`
// Verdict is the post-merge monitor's verdict on room.monitor_verdict: a closed code, no text.
Verdict string `json:"verdict,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 ¶
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.