Skip to content

feat(daemon): adaptive pacing for history sync - #137

Merged
neoromantic merged 3 commits into
mainfrom
feat/adaptive-sync-pacing
Oct 31, 2026
Merged

neoromantic merged 3 commits into
mainfrom
feat/adaptive-sync-pacing

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

Fixes #133

Problem

A first sync made one messages.getHistory call about every 4 seconds: the fixed 3 s pause between jobs counted from the job's end and waited for the main loop's next 1 s tick, and about 92% of the active time was that pause. A probe showed Telegram allows about 0.7 calls/s without a FLOOD_WAIT and 0.85–0.95 through short ones.

Change

  • Each account has a pacer (src/daemon/sync-pacing.ts). The interval counts from each job's start. While first or history pages are due it starts at 0.7 calls/s, speeds up by 0.05 after every 30 calls without a FLOOD_WAIT up to sync.pacing.maxCallsPerSecond (0.9), and halves on any FLOOD_WAIT (never below the calm pace), starting nothing until the wait and one interval at the new pace are over. With no history due it runs one job every sync.pacing.calmInterval (3s). Catch-up keeps its lane turns.
  • The main loop wakes inside its 1 s tick when an account's next job comes due (waitForNextTick), and a job that ends after the next one was due starts it at once. A stop still ends the wait at once.
  • The daemon's activity middleware (inside mtcute's flood waiter) reports every FLOOD_WAIT, short or long, to the account's pacer, and the daemon logs the new pace.
  • config.json: sync.pacing.maxCallsPerSecond (number above 0, at most 2) and sync.pacing.calmInterval (duration above 0), validated like cache.retention.*; the daemon reads them once at start and warns on bad values.
  • tg status: progress.history.calls_per_second (getHistory calls of the last 5 minutes from api_activity) and progress.history.pacing ({ mode, target_calls_per_second, flood_waits } from the live daemon), per account and summed; pretty output adds a History Pace row. New daemon_status keys pacing_*.
  • Docs: rate-limiting (new Pacing section), sync-strategy, daemon, configuration, cli-commands, database-schema, testing, CHANGELOG.

Tests

  • sync-pacing.test.ts: start at 0.7, speed-up after clean runs, cap, halving and waiting out a FLOOD_WAIT, floor at the calm pace, back to calm.
  • daemon-scheduler.test.ts: history at 0.7 calls/s counted from each start, the next job right after one that outlasted its interval, back to calm 3 s, no job until a FLOOD_WAIT and one interval are over (injected clock).
  • daemon-loop.test.ts: the tick wakes for a job due inside it; a stop during that wait starts nothing (fake clock and sleep).
  • daemon-accounts.test.ts: a FLOOD_WAIT through the daemon's middlewares halves the pace and logs it.
  • config, daemon-status, history-progress unit tests; status and multi-account e2e for the new fields.
  • bun run check, bun run test (2175 pass), status/multi-account/config/daemon e2e pass locally.

🤖 Generated with Claude Code

neoromantic and others added 2 commits October 31, 2026 04:10
While first pages or history are due, each account starts at 0.7
messages.getHistory calls per second, speeds up by 0.05 after every 30
calls without a FLOOD_WAIT up to sync.pacing.maxCallsPerSecond (0.9),
halves the pace on any FLOOD_WAIT after waiting it out, and goes back to
one job every sync.pacing.calmInterval (3s) once no history is due.

The pause counts from each job's start; the main loop wakes inside its
1 s tick when an account's next job comes due, and a job that ends after
the next one was due starts it at once. The daemon's activity middleware
reports every FLOOD_WAIT to the account's pacer.

tg status shows progress.history.calls_per_second (getHistory calls of
the last 5 minutes) and progress.history.pacing (the live daemon's mode,
target pace and FLOOD_WAITs), per account and summed, and a History Pace
row in pretty output.

Fixes #133

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A live run on a large account got a FLOOD_WAIT on messages.getDialogs in
the dialog pass at start, which halved the history pace to 0.35 calls/s
before the first page; climbing back to 0.9 took over ten minutes. Only a
FLOOD_WAIT on messages.getHistory, the sync jobs' own method, now slows
the pace.

Refs #133

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic force-pushed the feat/adaptive-sync-pacing branch from 7890961 to 79208dc Compare October 31, 2026 00:11
…rval

With calmInterval 1s the floor of the halving was 1 call/s capped at 0.9,
so a FLOOD_WAIT sped history up from 0.7 to 0.9. The floor is now a fixed
0.2 calls/s (or the cap, if lower), and history intervals round up, so the
pace never goes above the cap.

Refs #133

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit febc013 into main Oct 31, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/adaptive-sync-pacing branch October 31, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

First sync: pace history calls to what Telegram allows, not a fixed 3 s

1 participant