























@@ -47,10 +47,6 @@ export type CreateTelegramSendChatActionHandlerParams = {
4747sendChatActionFn: SendChatActionFn;
4848logger: TelegramSendChatActionLogger;
4949maxConsecutive401?: number;
50-/**
51- * Best-effort per-chat/action coalescing window. Kept opt-in so tests and
52- * non-typing callers can preserve exact sendChatAction semantics.
53- */
5450minIntervalMs?: number;
5551now?: () => number;
5652};
@@ -90,22 +86,14 @@ export function createTelegramSendChatActionHandler({
9086}: CreateTelegramSendChatActionHandlerParams): TelegramSendChatActionHandler {
9187let consecutive401Failures = 0;
9288let suspended = false;
93-const pendingKeys = new Set<string>();
94-const lastAttemptAtByKey = new Map<string, number>();
89+const blockedUntilByKey = new Map<string, number>();
95909691const reset = () => {
9792consecutive401Failures = 0;
9893suspended = false;
99-pendingKeys.clear();
100-lastAttemptAtByKey.clear();
94+blockedUntilByKey.clear();
10195};
10296103-const coalesceKey = (chatId: number | string, action: ChatAction) =>
104-// The Telegram API throttler keys group traffic by chat_id, not thread ID.
105-// Coalescing at the same level keeps topic typing cues from filling the
106-// shared outbound lane ahead of real replies.
107-`${String(chatId)}:${action}`;
108-10997const sendChatAction = async (
11098chatId: number | string,
11199action: ChatAction,
@@ -115,19 +103,14 @@ export function createTelegramSendChatActionHandler({
115103return;
116104}
117105118-const shouldCoalesce = Number.isFinite(minIntervalMs) && minIntervalMs > 0;
119-const key = shouldCoalesce ? coalesceKey(chatId, action) : undefined;
106+const key = minIntervalMs > 0 ? `${String(chatId)}:${action}` : undefined;
107+const attemptedAt = key ? now() : 0;
120108if (key) {
121-if (pendingKeys.has(key)) {
122-return;
123-}
124-const currentTime = now();
125-const lastAttemptAt = lastAttemptAtByKey.get(key);
126-if (lastAttemptAt !== undefined && currentTime - lastAttemptAt < minIntervalMs) {
109+const blockedUntil = blockedUntilByKey.get(key);
110+if (blockedUntil !== undefined && attemptedAt < blockedUntil) {
127111return;
128112}
129-pendingKeys.add(key);
130-lastAttemptAtByKey.set(key, currentTime);
113+blockedUntilByKey.set(key, Number.POSITIVE_INFINITY);
131114}
132115133116if (consecutive401Failures > 0) {
@@ -167,7 +150,7 @@ export function createTelegramSendChatActionHandler({
167150throw error;
168151} finally {
169152if (key) {
170-pendingKeys.delete(key);
153+blockedUntilByKey.set(key, attemptedAt + minIntervalMs);
171154}
172155}
173156};
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。