Serialize herald Telegram sends and honor 429 retry_after, with
separate non-429 exponential backoff. Route fallback and invalidated-block
messages through the same retry path.
Details
- Reviewers
Fabien - Group Reviewers
Restricted Project - Commits
- rABC4a057a2fc928: [ecash-herald] Improve Telegram send retries for rate limits
- cd apps/ecash-herald && npm test
Diff Detail
- Repository
- rABC Bitcoin ABC
- Lint
Lint Not Applicable - Unit
Tests Not Applicable
Event Timeline
Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed.
CodeRabbit Review
Diff : committed changes only
Compare : HEAD → master
Directory : work
────────────────────────────────────────
(\(\
(• .•) Don't use manual procedures. A shell script or batch file will execute the same instructions, in the same order, time after time.
────────────────────────────────────────────────────────────────────────
minor [Functional Correctness] → ]8;;vscode://file//work/apps/ecash-herald/src/telegram.ts:13apps/ecash-herald/src/telegram.ts:13-45]8;; Align the maxRetries name and doc with the implemented semantics. deliverWithRetries throws when non429Failures >= opts.maxRetries. With maxRetries: 3, the code performs 3 total attempts and 2 retries. The doc states "Max retries for non-429 errors only (default 3)". Rename the option to maxAttempts, or change the comparison to `non429Failures > opts.maxRetries`.
────────────────────────────────────────────────────────────────────────
major [Stability & Availability] → ]8;;vscode://file//work/apps/ecash-herald/src/telegram.ts:136apps/ecash-herald/src/telegram.ts:136-153]8;; Restrict the 429 message heuristic to reduce false positives. Line 152 matches \b429\b anywhere in the error message. Telegram error strings can embed unrelated numbers, for example a chat id, a message id, or echoed message text. A false positive routes a permanent error into the 429 path, which then retries up to maxApiAttemptsPerMessage times and blocks the shared send queue. Anchor the pattern to the Telegram error format instead. 🛡️ Proposed tighter pattern - return typeof e.message === 'string' && /\b429\b/.test(e.message); + return ( + typeof e.message === 'string' && + /(?:ETELEGRAM:\s*)?\b429\b\s*(?:Too Many Requests|:)/i.test(e.message) + );
────────────────────────────────────────────────────────────────────────
major [Stability & Availability]
→ ]8;;vscode://file//work/apps/ecash-herald/src/telegram.ts:274apps/ecash-herald/src/telegram.ts:274-347]8;;
Bound the total time one message can hold the shared send queue.
heraldSend serializes every send through the module-level sendChain.
deliverWithRetries retries 429 responses up to
maxApiAttemptsPerMessage (default 100). If retry_after is absent, each
wait uses fallback429WaitMs (default 5000) plus jitter. One message can
therefore occupy the queue for about 500 seconds, and every other send,
including block summaries and invalidation alerts, waits behind it. A
hostile or misparsed retry_after value makes this worse, because
parsed is used without an upper bound.
Add a total deadline for one message and clamp the honored retry_after.
🛡️ Proposed deadline and clamp
const deliverWithRetries = async (
deliver: () => Promise<SendMessageResponse>,
opts: Required<HeraldSendRetryOptions>,
): Promise<SendMessageResponse> => {
let apiAttempts = 0;
let non429Failures = 0;
let lastError: unknown;
+ const deadline = Date.now() + opts.maxTotalWaitMs;
while (true) {
await waitUntilSendAllowed();
- if (apiAttempts >= opts.maxApiAttemptsPerMessage) {
+ if (
+ apiAttempts >= opts.maxApiAttemptsPerMessage ||
+ Date.now() >= deadline
+ ) {
throw (
lastError ??
new Error(
`Abandoned Telegram send after ${opts.maxApiAttemptsPerMessage} API attempts`,
)
);
}
@@
if (isTelegram429Error(error)) {
const parsed = extractTelegramRetryAfterMs(error);
const baseWait =
- parsed !== null ? parsed : opts.fallback429WaitMs;
+ parsed !== null
+ ? Math.min(parsed, opts.max429WaitMs)
+ : opts.fallback429WaitMs;
Add maxTotalWaitMs and max429WaitMs to HeraldSendRetryOptions and
DEFAULT_RETRY_OPTIONS.────────────────────────────────────────
Review complete
3 findings ✔
Major 2
Minor 1
5 files reviewed:
- apps/ecash-herald/src/events.ts
- apps/ecash-herald/src/telegram.ts
- apps/ecash-herald/test/chronikWsHandler.test.ts
- apps/ecash-herald/test/events.test.ts
- apps/ecash-herald/test/telegram.test.ts
────────────────────────────────────────
Print all AI prompts: coderabbit review --show-prompts
Address CodeRabbit feedback: rename maxRetries to maxAttempts to match
semantics, tighten the 429 message heuristic to avoid false positives on
unrelated numbers, and bound per-message queue time with maxTotalWaitMs /
max429WaitMs.
Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed.
CodeRabbit Review
Diff : committed changes only
Compare : HEAD → master
Directory : work
────────────────────────────────────────
(\(\
(• .•) Codicem Purgo, Ergo Sum. I debug, therefore I am.
────────────────────────────────────────────────────────────────────────
minor [Stability & Availability]
→ ]8;;vscode://file//work/apps/ecash-herald/src/telegram.ts:359apps/ecash-herald/src/telegram.ts:359-379]8;;
Bound the non-429 backoff sleep by the per-message deadline.
The 429 branch bounds its wait with remainingMs. The non-429 branch does
not. With the defaults, maxDelay is 30000 and maxAttempts is 3, so the
sleep can push a message past maxTotalWaitMs while it still holds
sendChain. The loop then aborts on the next iteration, so the extra
sleep only delays every other queued message.
🔧 Proposed fix
const delay = calculateNon429BackoffDelay(
non429Failures - 1,
opts.baseDelay,
opts.maxDelay,
);
+ const boundedDelay = Math.min(
+ delay,
+ Math.max(0, deadline - Date.now()),
+ );
const errorMessage =
error instanceof Error ? error.message : 'Unknown error';
console.log(
`Telegram send error, retrying in ${Math.round(
- delay,
+ boundedDelay,
)}ms (non-429 ${non429Failures}/${opts.maxAttempts}):`,
errorMessage,
);
- if (delay > 0) {
- await sleep(delay);
- }
+ if (boundedDelay > 0) {
+ await sleep(boundedDelay);
+ }────────────────────────────────────────────────────────────────────────
minor [Functional Correctness] → ]8;;vscode://file//work/apps/ecash-herald/src/telegram.ts:331apps/ecash-herald/src/telegram.ts:331-342]8;; Clamp the jitter inside max429WaitMs. The code adds up to 500 ms of jitter after the clamp. A single honored wait can therefore reach max429WaitMs + 500. The doc comment on Line 40 states max429WaitMs is the upper bound on one honored retry_after wait. Apply the clamp after the jitter to match the documented contract. 🔧 Proposed fix - const baseWait = - parsed !== null - ? Math.min(parsed, opts.max429WaitMs) - : opts.fallback429WaitMs; - const waitMs = baseWait + Math.random() * 500; + const baseWait = + parsed !== null ? parsed : opts.fallback429WaitMs; + const waitMs = Math.min( + baseWait + Math.random() * 500, + opts.max429WaitMs, + );
────────────────────────────────────────
Review complete
2 findings ✔
Minor 2
5 files reviewed:
- apps/ecash-herald/src/events.ts
- apps/ecash-herald/src/telegram.ts
- apps/ecash-herald/test/chronikWsHandler.test.ts
- apps/ecash-herald/test/events.test.ts
- apps/ecash-herald/test/telegram.test.ts
────────────────────────────────────────
Print all AI prompts: coderabbit review --show-prompts
Bound non-429 backoff by the per-message deadline, and clamp 429 jitter
inside max429WaitMs so the documented upper bound is honored.
Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed.
CodeRabbit Review
Diff : committed changes only
Compare : HEAD → master
Directory : work
────────────────────────────────────────
(\(\
(• .•) Ah yes, the classic pattern: 'parse first, ask questions never'.
────────────────────────────────────────
Review complete
No findings ✔
5 files reviewed:
- apps/ecash-herald/src/events.ts
- apps/ecash-herald/src/telegram.ts
- apps/ecash-herald/test/chronikWsHandler.test.ts
- apps/ecash-herald/test/events.test.ts
- apps/ecash-herald/test/telegram.test.ts
────────────────────────────────────────
| apps/ecash-herald/src/telegram.ts | ||
|---|---|---|
| 150 ↗ | (On Diff #60838) | Is there any reason to not retry on other errors ? If anything the 429 might be the only reason for not retrying since you're already over the rate limit (it's probably still a good idea because there is a delay before the retry) |
Replied on the inline: we already retry transient non-429 errors; only permanent 4xx fail fast. Requesting review.
| apps/ecash-herald/src/telegram.ts | ||
|---|---|---|
| 150 ↗ | (On Diff #60838) | We do retry other errors. Flow today:
So isTelegram429Error is not the only thing we retry; it is the case that needs different wait timing. Happy to adjust if you want permanent 4xx retried as well. |