Repository navigation
AI-3513: count a forward's attempt at its claim, leave an unopenable secret to another router #2269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
98735f3
d6d8aeb
d903812
d7ab375
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,9 +157,12 @@ type Forwarder struct { | |
| // sending is how many sends to each destination are running, by destination id. | ||
| mu sync.Mutex | ||
| sending map[string]int | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| working sync.WaitGroup | ||
| // unopenedAt is when this router last logged that it cannot open a destination's signing | ||
| // secret, by destination id (unopened). | ||
| unopenedAt map[string]time.Time | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| working sync.WaitGroup | ||
| } | ||
|
|
||
| // Event is one verified provider delivery of one customer's provider app. | ||
|
|
@@ -224,20 +227,21 @@ func New(options Options) (*Forwarder, error) { | |
| } | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| return &Forwarder{ | ||
| store: options.Store, | ||
| secrets: options.Secrets, | ||
| http: &unredirected, | ||
| publicURL: publicURL, | ||
| retries: retries, | ||
| poll: poll, | ||
| lease: leaseFor, | ||
| activity: options.Activity, | ||
| logger: logger, | ||
| kick: make(chan struct{}, 1), | ||
| slots: make(chan struct{}, inFlight), | ||
| sending: map[string]int{}, | ||
| ctx: ctx, | ||
| cancel: cancel, | ||
| store: options.Store, | ||
| secrets: options.Secrets, | ||
| http: &unredirected, | ||
| publicURL: publicURL, | ||
| retries: retries, | ||
| poll: poll, | ||
| lease: leaseFor, | ||
| activity: options.Activity, | ||
| logger: logger, | ||
| kick: make(chan struct{}, 1), | ||
| slots: make(chan struct{}, inFlight), | ||
| sending: map[string]int{}, | ||
| unopenedAt: map[string]time.Time{}, | ||
| ctx: ctx, | ||
| cancel: cancel, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
@@ -267,8 +271,9 @@ func (f *Forwarder) Start() { | |
| }() | ||
| } | ||
|
|
||
| // Close stops the worker and cancels the sends in flight. A send cut short is not counted as | ||
| // an attempt: its lease runs out and a router sends it again. | ||
| // Close stops the worker and cancels the sends in flight. A send cut short keeps the attempt | ||
| // its claim counted, as a router that died mid-send does, since the destination may have had | ||
| // it: its lease runs out and a router sends it again while attempts remain. | ||
| func (f *Forwarder) Close() { | ||
| f.cancel() | ||
| f.working.Wait() | ||
|
|
@@ -504,26 +509,44 @@ func (f *Forwarder) sendDue() (wait time.Duration, again bool) { | |
| // it or refused it for good, else due again after the next wait, or gone when the waits ran | ||
| // out. | ||
| func (f *Forwarder) attempt(delivery store.ClaimedEventDelivery) { | ||
| if delivery.Attempts > len(f.retries)+1 { | ||
| f.giveUpUnsent(delivery) | ||
| return | ||
| } | ||
| ctx, cancel := context.WithTimeout(f.ctx, attemptTimeout) | ||
| err := f.send(ctx, delivery) | ||
| cancel() | ||
| if f.ctx.Err() != nil { | ||
| // Close cut it short: the lease runs out and a router sends it again. | ||
| // Close cut it short: the attempt its claim counted stays, the lease runs out and a | ||
| // router sends it again. | ||
| return | ||
| } | ||
| writing, done := context.WithTimeout(context.WithoutCancel(f.ctx), attemptTimeout) | ||
| defer done() | ||
| log := f.logger.With("connector", delivery.ConnectorID, "customer", delivery.CustomerID, | ||
| "destination", delivery.DestinationID, "webhook_id", delivery.ID) | ||
| var again retryable | ||
| attempts := delivery.Attempts + 1 | ||
| // The claim counted this attempt. | ||
| attempts := delivery.Attempts | ||
| switch { | ||
| case errors.Is(err, auth.ErrKeyVersionUnavailable) && time.Since(delivery.CreatedAt) > f.unsignedWait(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Should Fix] Give-up by row age drops a forward still on its retries Evidence
|
||
| // No router opened it for longer than its retries would have run: none holds the key. | ||
| log.Warn("gave up an event forward no router could sign", "error", err) | ||
| f.count(delivery, codeNotSent) | ||
| err = f.store.EventDeliveries.Finish(writing, delivery.DestinationID, delivery.ID) | ||
| case errors.Is(err, auth.ErrKeyVersionUnavailable): | ||
| // Another router may hold the key version the secret was sealed with: this one sent | ||
| // nothing, so the delivery goes back to the queue with no attempt counted. It is due | ||
| // again a lease later, as if its claim had run out, not at once: this router, woken by | ||
| // its own finished send, would take the destination's backlog straight back every poll. | ||
| f.unopened(log, delivery.DestinationID, err) | ||
| err = f.store.EventDeliveries.Release(writing, delivery, f.lease) | ||
| case err == nil: | ||
| f.count(delivery, "") | ||
| err = f.store.EventDeliveries.Finish(writing, delivery.DestinationID, delivery.ID) | ||
| case errors.As(err, &again) && attempts <= len(f.retries): | ||
| log.Info("an event forward failed and is sent again", "attempts", attempts, "error", err) | ||
| err = f.store.EventDeliveries.Retry(writing, delivery.DestinationID, delivery.ID, attempts, time.Now().Add(f.retries[attempts-1])) | ||
| err = f.store.EventDeliveries.Retry(writing, delivery.DestinationID, delivery.ID, time.Now().Add(f.retries[attempts-1])) | ||
| default: | ||
| log.Warn("gave up an event forward", "attempts", attempts, "error", err) | ||
| f.count(delivery, givenUpCode(err)) | ||
|
|
@@ -534,6 +557,47 @@ func (f *Forwarder) attempt(delivery store.ClaimedEventDelivery) { | |
| } | ||
| } | ||
|
|
||
| // unsignedWait is how long a forward no router can sign is left for one that can: its whole | ||
| // retry schedule, and a lease for the release that ends it. | ||
| func (f *Forwarder) unsignedWait() time.Duration { | ||
| wait := f.lease | ||
| for _, retry := range f.retries { | ||
| wait += retry | ||
| } | ||
| return wait | ||
| } | ||
|
|
||
| // unopened logs that this router cannot open a destination's signing secret, once a lease for | ||
| // each destination: a destination's whole backlog fails alike, and one line says it. | ||
| func (f *Forwarder) unopened(log *slog.Logger, destinationID string, err error) { | ||
| f.mu.Lock() | ||
| last, seen := f.unopenedAt[destinationID] | ||
| due := !seen || time.Since(last) >= f.lease | ||
| if due { | ||
| f.unopenedAt[destinationID] = time.Now() | ||
| } | ||
| f.mu.Unlock() | ||
| if !due { | ||
| return | ||
| } | ||
| log.Error("this router cannot open an event destination's signing secret; its forwards are left to another", "error", err) | ||
| } | ||
|
|
||
| // giveUpUnsent drops a delivery claimed more times than it may be tried: each claim before | ||
| // this one counted its attempt, and a forwarder that died between its claim and its send, every | ||
| // time, recorded nothing else. | ||
| func (f *Forwarder) giveUpUnsent(delivery store.ClaimedEventDelivery) { | ||
| writing, done := context.WithTimeout(context.WithoutCancel(f.ctx), attemptTimeout) | ||
| defer done() | ||
| log := f.logger.With("connector", delivery.ConnectorID, "customer", delivery.CustomerID, | ||
| "destination", delivery.DestinationID, "webhook_id", delivery.ID) | ||
| log.Warn("gave up an event forward its earlier claims never finished", "attempts", delivery.Attempts) | ||
| f.count(delivery, codeNotSent) | ||
| if err := f.store.EventDeliveries.Finish(writing, delivery.DestinationID, delivery.ID); err != nil { | ||
| log.Error("could not record an event forward's attempt", "error", err) | ||
| } | ||
| } | ||
|
|
||
| // count counts a forward that ended in the activity record, once and not per attempt: sent, or | ||
| // refused with code when it was given up. | ||
| func (f *Forwarder) count(delivery store.ClaimedEventDelivery, code string) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.