diff --git a/modules/deliveries/access_test.go b/modules/deliveries/access_test.go index 4b7f7ef43..cf7cad293 100644 --- a/modules/deliveries/access_test.go +++ b/modules/deliveries/access_test.go @@ -63,8 +63,8 @@ func (c *channelLookups) Get(string) (*connectors.Connector, bool) { // TestDeliveryIsAuthorizedOnEveryAttempt guards that a queued message is sent // only while the workload principal may still deliver on its channel. Each // attempt, including a retry after a backoff or a restart, asks again: a -// revoked grant ends the message as failed, and a chain that can't decide -// leaves it queued for its next attempt instead of dropping it. +// revoked grant marks the message failed, and a chain that can't decide leaves +// it queued for its next attempt instead of dropping it. func TestDeliveryIsAuthorizedOnEveryAttempt(t *testing.T) { allowed, revoked, unavailable := governance.Allowed(), governance.DeniedAt(governance.LevelOrg, []string{"org-workload"}), governance.Unavailable() tests := []struct { @@ -76,7 +76,7 @@ func TestDeliveryIsAuthorizedOnEveryAttempt(t *testing.T) { wantOutcomes []string }{ {name: "an allowed attempt is sent", decisions: []governance.Decision{allowed}, attempts: 1, wantSends: 1, wantOutcomes: []string{"failed: connector \"slack\" isn't running"}}, - {name: "a revoked grant ends the message", decisions: []governance.Decision{revoked}, attempts: 1, wantOutcomes: []string{"gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}}, + {name: "a revoked grant fails the message", decisions: []governance.Decision{revoked}, attempts: 1, wantOutcomes: []string{"gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}}, {name: "a retry after revocation is refused", decisions: []governance.Decision{allowed, revoked}, attempts: 2, wantSends: 1, wantOutcomes: []string{"failed: connector \"slack\" isn't running", "gave up: " + connectors.ErrRejected.Error() + ": " + governance.ErrDenied.Error() + ": deliver delivery slack"}}, {name: "an undecidable chain leaves the message queued", decisions: []governance.Decision{unavailable}, attempts: 3}, {name: "no policy wired leaves the message queued", unwired: true, attempts: 1}, diff --git a/modules/deliveries/deliveries.go b/modules/deliveries/deliveries.go index aeca627e3..b1ca514e4 100644 --- a/modules/deliveries/deliveries.go +++ b/modules/deliveries/deliveries.go @@ -1,11 +1,11 @@ -// Package deliveries is the transport for what Archie tells people, on each -// channel. It holds no approval state: approvals own the request and its -// answer, and use deliveries to carry them. The composer, the chat box in the -// dashboard, is the first channel: its messages are rows a client reads from a -// cursor, with a live stream that only says when to read. Every accepted -// connector that delivers messages is another: each gets its own row, retried -// until the connector confirms it, rejects it permanently, or the retry limit -// is reached. +// Package deliveries carries what Archie tells people on each channel. It +// holds no approval state: approvals own the request and its answer, and use +// deliveries to carry them. The composer, the chat box in the dashboard, is +// the first channel: its messages are rows a client reads from a cursor, with +// a live stream that only says when to read. Every accepted connector that +// delivers messages is another channel: each gets its own row, retried until +// the connector confirms it, rejects it permanently, or the retry limit is +// reached. package deliveries import ( @@ -64,8 +64,8 @@ type Notification struct { } // ApprovalRequest is one judged action waiting for a person's answer. It is -// carried like a notification, with the request an answer names and the time -// it runs out. +// carried like a notification, and adds the approval request an answer refers +// to and when the request expires. type ApprovalRequest struct { RunID uuid.UUID RequestID uuid.UUID @@ -77,7 +77,8 @@ type ApprovalRequest struct { } // Message is a composer message as the API shows it. Attempts are the delivery -// attempts of the connector rows that carry the same message, one per channel. +// attempts recorded on the connector rows that carry the same message, one per +// channel. type Message struct { Seq int64 `json:"seq"` ID uuid.UUID `json:"id"` @@ -108,7 +109,8 @@ type Channels interface { Get(name string) (*connectors.Connector, bool) } -// Module delivers one organisation's notifications. +// Module delivers one organisation's notifications, approval requests and +// answers. type Module struct { orgID string store *store.Store @@ -214,9 +216,10 @@ func (m *Module) send(ctx context.Context, msg store.Message) error { return nil } -// SentBy reports whether channel confirmed a message under one of ids, the id -// a source's event carries when it echoes what Archie sent, and the run that -// sent it (nil when the message belonged to none). +// SentBy reports whether the channel confirmed a message under any of ids, and +// returns the run that sent it. An id is the identifier a source's event +// carries when it echoes back what Archie sent. The run is empty when no +// message matched. func (m *Module) SentBy(ctx context.Context, channel string, ids ...string) (uuid.UUID, bool, error) { ids = slices.DeleteFunc(slices.Clone(ids), func(id string) bool { return id == "" }) if len(ids) == 0 { @@ -249,9 +252,9 @@ func (m *Module) Work(ctx context.Context) { // deliver sends a claimed message when the workload principal may still deliver // on its channel. Each attempt is decided afresh, so a retry after a backoff or -// a restart honours a revocation made meanwhile. A refusal ends the message as -// failed. When the policy chain can't decide, the message stays queued for its -// next attempt. +// a restart honours a revocation made in the meantime. A refusal marks the +// message failed. When the policy chain can't decide, the message stays queued +// for its next attempt. func (m *Module) deliver(ctx context.Context, msg store.Message) { err := governance.RequireWorkload(ctx, m.access, governance.ActionDeliver, governance.Resource{ Kind: governance.KindDelivery, ID: msg.Channel, @@ -313,7 +316,8 @@ func (m *Module) sendTo(ctx context.Context, msg store.Message) (string, error) return c.Deliver(ctx, connectors.DeliveryMessage, raw, msg.Key) } -// backoff is how long to wait after the attempts so far before the next. +// backoff returns the wait before the next attempt, given the number of +// attempts made so far. func backoff(attempts int) time.Duration { if attempts >= 10 { return maxBackoff diff --git a/modules/deliveries/internal/store/store.go b/modules/deliveries/internal/store/store.go index af2134838..f4f52de91 100644 --- a/modules/deliveries/internal/store/store.go +++ b/modules/deliveries/internal/store/store.go @@ -29,8 +29,8 @@ type Message struct { RequestID uuid.NullUUID `db:"request_id"` ExpiresAt sql.NullTime `db:"expires_at"` CreatedAt time.Time `db:"created_at"` - // Sent is set on a message its channel has confirmed; the composer's are - // sent when they are stored. + // Sent is set on a message its channel has confirmed. Composer messages + // count as sent as soon as they are stored. Sent bool `db:"-"` Attempts int `db:"attempts"` } @@ -93,9 +93,9 @@ func (s *Store) Latest(ctx context.Context, orgID, channel string, limit int) ([ return out, nil } -// Claim takes the oldest message a channel hasn't confirmed whose next attempt -// is due, and pushes that attempt out by backoff first: the push is the lease, -// so a crash mid-delivery retries after it rather than never. +// Claim takes the oldest message its channel hasn't confirmed whose next +// attempt is due, and first pushes that attempt out by the backoff. The push +// is the lease, so a crash mid-delivery retries after it rather than never. func (s *Store) Claim(ctx context.Context, backoff func(attempts int) time.Duration) (Message, bool, error) { tx, err := s.db.BeginTxx(ctx, nil) if err != nil { @@ -148,8 +148,8 @@ func (s *Store) Sent(ctx context.Context, seq int64, attempt int, externalID str `UPDATE deliveries.messages SET sent_at = now(), external_id = $2, last_error = NULL WHERE seq = $1`, externalID) } -// SentBy finds a message its channel confirmed under one of ids. The run is -// the one that sent it, nil when the message belonged to none. +// SentBy finds a message its channel confirmed under any of ids, and returns +// the run that sent it. The run is invalid when the message belonged to none. func (s *Store) SentBy(ctx context.Context, orgID, channel string, ids []string) (uuid.NullUUID, bool, error) { var run uuid.NullUUID err := s.db.GetContext(ctx, &run, ` @@ -164,8 +164,8 @@ func (s *Store) SentBy(ctx context.Context, orgID, channel string, ids []string) return run, true, nil } -// Failed records why an attempt failed, with the attempt that failed; the -// lease already set when it is tried again. +// Failed records why an attempt failed, along with the attempt that failed. +// The lease that Claim set still decides when the next attempt is due. func (s *Store) Failed(ctx context.Context, seq int64, attempt int, reason string, took time.Duration) error { return s.attempt(ctx, seq, attempt, attemptOutcome{failure: reason, took: took}, `UPDATE deliveries.messages SET last_error = $2 WHERE seq = $1`, reason)