Repository navigation
FX-047: keep events on temporary errors, honour Retry-After, count drops, bound flush() - #3
Merged
Merged
Conversation
…ops, bound flush()
- 5xx/503/429/408/timeout/network: the event is kept and retried with backoff
(up to 60 s between cycles) for maxRetryDuration (default 1 h) instead of being
deleted after 10 failed flush cycles (~2 minutes). Same event object, same eventId.
- Retry-After (seconds or HTTP date, capped at 5 min) is honoured inline and between cycles.
- 401/403 kept and retried for 10 minutes, then dropped; other 4xx stay final.
- getStats(): queued, in-flight and dropped counts by reason; drops logged as one
summary line per flush cycle.
- flush() bounded by flushTimeout (default 30 s); flush() and close() resolve with { unsent }.
- Event id builder unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…03 final, jitter on Retry-After - maxQueueSize now bounds queued + in flight; a re-queued failure counts against it and the oldest queued event is evicted as queue_overflow (counted). Queue kept in arrival order. - 401/403 are final again (the server answers 503 for its own faults), counted as the new drop reason auth_refused and in the summary line. - Retry-After waits get up to 20% jitter, between cycles and in the quick retries. - Tests: steady sender through a 30-minute outage never exceeds the cap; drop after maxRetryDuration; malformed Retry-After; close() waits out a backoff. - eventId doc comment: 7-day dedup window. Changelog: ships as 1.4.0 (flush/close return type). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts resolved only: src/index.ts keeps this PR's code exactly (comment-stripped compare identical) with internal ticket ids removed from comments, as on main; README.md takes main's new short README (the full reference lives on the docs site). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
czypnt
added a commit
that referenced
this pull request
Nov 22, 2026
Source resolutions: - src/index.ts TrackOptions.externalIds JSDoc: this PR's rewrite. - src/index.ts fields: main's retry/inFlight/drop-count fields kept; this PR's warnedKinds, exitFlushed and shared liveClients/exitListener kept; flushAttempts (gone in main) and exitHook (gone here) dropped. - src/index.ts registerExitHook: this PR's shared beforeExit listener, calling main's startDrain() instead of flush(), as main's per-client hook did. - src/index.ts _flush failure path: main's handleFailure; this PR's once-only "failed flush attempts" warning dropped in favour of main's per-cycle drop summary. - tests/test.ts W3: the 10-cycle drop no longer exists; now two final-400 drops in one flush must give one summary line. W14 network-recovery test waits out the first backoff before re-flushing. - CHANGELOG: one Unreleased; this PR's entries under main's Changed/Added, the warning entry no longer mentions failed-attempt drops. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
The SDK deleted events during a server outage. In 1.3.4 a temporary failure (
5xx,503,429,408, a timeout or a network error) gotretryLimitquick retries, then went back to the queue;_flush()counted failed flush cycles per event and, atMAX_FLUSH_ATTEMPTS = 10, calledonDrop(..., 'max_flush_attempts')and threw the event away. With the 10 s flush timer that is about two minutes, so a two-minute outage lost events for good. Also:Retry-Afterwas never read.flush()had no time bound.What the ingest worker answers on
POST /trackTaken from
infra/cloudflare/ingest/index.jshandleServerTrackon monorepo mainef756fd42, and pinned byinfra/cloudflare/tests/ingest-answer-contract.test.js:200(stored, or a duplicateeventId)400,405,413,415permanent_client_error401503becauseINGEST_OWN_FAULT_503 = "on"in production.auth_refused(new reason), as in 1.3.4429+Retry-After: 1Retry-After500503+Retry-After: 5Retry-AfterThe server deduplicates on
eventIdfor 7 days, so retrying with the same id is safe.What changes
maxRetryDuration, a new option that defaults to 1 hour (allowed range 1 minute to 24 hours). After that it is dropped asmax_flush_attempts; the name is kept.Retry-Afteris honoured, as seconds or as an HTTP date, capped at 5 minutes, plus up to 20% jitter so clients do not all retry at the same instant. A malformed value is ignored. A short one is waited out inside the quick retries. One over 10 s sends the event back to the queue, which does not send it before that time.401/403are final, as in 1.3.4. They are counted under the new drop reasonauth_refused, with the loud authentication line once and in the summary line.maxQueueSizenow bounds queued plus in-flight events, including re-queued failures, so memory stays bounded through an outage. The queue stays in arrival order. The oldest queued event is evicted asqueue_overflowand counted. When every held event is in flight, the new one is dropped.eventIdis the same. The event id builder is untouched; only its doc comment now says 7-day window instead of 6h.getStats()(new) returns{ queued, inFlight, dropped, droppedByReason }. Every drop is counted. Drops from the cap, final answers, auth refusals and expired retries are logged as one summary line per flush cycle, never per event.flush()waits at mostflushTimeout, a new option that defaults to 30 s.flush()andclose()resolve with{ unsent }and log one line when it is above zero.close()waits out backoff andRetry-AfterwithincloseTimeoutand never sends early.How it was tested
The tests were written first; each failed on the earlier code and passes now.
tests/retry.tsmocksfetchand movesDate.nowforward. It covers:503,500,429, timeout, network error and502: the event is delivered once, nothing is dropped, every attempt carries the same id, and the event is never re-sent after its2xx503outage with a cap of 100: queued plus in-flight never goes above 100 (the reviewer measured 2,790 before this fix; this test measured 575 before it), and every event is either held or counted asqueue_overflowmax_flush_attemptsaftermaxRetryDurationRetry-Afteras seconds, as an HTTP date, malformed, and long (120 s, not sent early)close()waiting out a 2 s backoff, then delivering401/403sent once, dropped asauth_refused, counted, with one summary line;400/413finalflush()/close()returning inside their bounds withunsenttests/test.tskeeps every earlier check; the401section only expects the new reason name.npm testruns both files.tsc --noEmitandnpm run buildpass locally on Node 26, and I ran the suite twice. CI runs Node 22 and 24.What an integrator sees
maxQueueSizeincluding in-flight events, and arrive when the server recovers.flush()andclose()returnPromise<FlushResult>({ unsent }) instead ofPromise<void>. Code that ignores the value is unaffected. A subclass overridingflush(): Promise<void>, or code typed to expectPromise<void>, no longer type-checks.401/403reachonDropasauth_refused, notpermanent_client_error.max_flush_attemptsfires only aftermaxRetryDuration.Queue full (N) — dropping oldest events. ...andDropping event after N failed flush attempts: <event>N event(s) dropped since the last report (<reason>=<n>, ...); getStats() has the totals.(at most once per flush cycle) andflush(): N event(s) not delivered yet; kept in memory and retried later.close(): N event(s) undelivered ...line now counts in-flight events and ends with: Q queued, F still in flight.Known limits
Events still in memory when a process exits or a serverless function is frozen are lost without being counted. During a
503outage a flush can wait 15 to 20 seconds, where 1.3.4 waited 4 to 7.Release
This ships as a minor release, 1.4.0, because the return type of
flush()andclose()changed. The version is not bumped; the changelog entry stays under Unreleased. Publishing is a separate decision. Nothing is needed on the server first.🤖 Generated with Claude Code