fix(history): batch getHistory requests instead of one RPC per message#249
Open
yagop wants to merge 1 commit into
Open
fix(history): batch getHistory requests instead of one RPC per message#249yagop wants to merge 1 commit into
yagop wants to merge 1 commit into
Conversation
The messages iterator's default batch size is 1, so `tg history --limit N` issued one messages.getHistory RPC per message. Larger limits became extremely slow (hundreds of round trips for a few hundred messages). Set BatchSize to min(limit, 100): Telegram does not reject a per-request limit above 100 but silently returns fewer/incorrect results, so 100 is the safe maximum. Clamp to at least 1, since a non-positive batch size panics the iterator (it preallocates a slice with that capacity) and a negative --limit would otherwise crash the process. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
9153e42 to
a80a6d4
Compare
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.
Problem
tg history --limit Nis extremely slow for anything beyond a handful of messages — fetching ~350 messages did not finish in 7 minutes.The cause is in
listHistory: the gotdmessagesiterator is created withquery.Messages(api).GetHistory(peer).Iter(), leaving the builder's default batch size of 1. The iterator therefore issues onemessages.getHistoryRPC per message (hundreds of round trips for a few hundred messages).Fix
Set
BatchSize(max(1, min(limit, 100)))on the builder:BatchSizedoc), so 100 is the safe maximum.NewIteratorpreallocates a slice with that capacity), so a negative--limitwould otherwise crash the process.Fetching 350 messages now takes ~3.5s (≈4 RPCs) instead of timing out.
Tests
Adds
TestListHistoryBatchSize, asserting the per-request limit sent is5/100/100for--limit 5/100/350and clamps to1for--limit 0/-1(no panic).🤖 Generated with Claude Code