fix(telegram): bound startup request timeouts (#61601) (thanks @neeravmakwana)

This commit is contained in:
Neerav Makwana
2026-04-06 08:20:15 -04:00
committed by GitHub
parent 6b53a9aadb
commit 177e23801b
4 changed files with 37 additions and 4 deletions

View File

@@ -36,6 +36,7 @@ import { apiThrottler, Bot, sequentialize, type ApiClientOptions } from "./bot.r
import { buildTelegramGroupPeerId, resolveTelegramStreamMode } from "./bot/helpers.js";
import { resolveTelegramTransport, type TelegramTransport } from "./fetch.js";
import { tagTelegramNetworkError } from "./network-errors.js";
import { resolveTelegramRequestTimeoutMs } from "./request-timeouts.js";
import { createTelegramSendChatActionHandler } from "./sendchataction-401-backoff.js";
import { getTelegramSequentialKey } from "./sequential-key.js";
import { createTelegramThreadBindingManager } from "./thread-bindings.js";
@@ -81,8 +82,6 @@ const DEFAULT_TELEGRAM_BOT_RUNTIME: TelegramBotRuntime = {
apiThrottler,
};
const TELEGRAM_GET_UPDATES_REQUEST_TIMEOUT_MS = 45_000;
let telegramBotRuntimeForTest: TelegramBotRuntime | undefined;
export function setTelegramBotRuntimeForTest(runtime?: TelegramBotRuntime): void {
@@ -200,8 +199,7 @@ export function createTelegramBot(opts: TelegramBotOptions): TelegramBotInstance
}
};
const method = extractTelegramApiMethod(input);
const requestTimeoutMs =
method === "getupdates" ? TELEGRAM_GET_UPDATES_REQUEST_TIMEOUT_MS : undefined;
const requestTimeoutMs = resolveTelegramRequestTimeoutMs(method);
let requestTimeout: ReturnType<typeof setTimeout> | undefined;
let onRequestAbort: (() => void) | undefined;
const requestSignal = init?.signal;

View File

@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { resolveTelegramRequestTimeoutMs } from "./request-timeouts.js";
describe("resolveTelegramRequestTimeoutMs", () => {
it("bounds Telegram startup control-plane methods", () => {
expect(resolveTelegramRequestTimeoutMs("deletewebhook")).toBe(15_000);
expect(resolveTelegramRequestTimeoutMs("getme")).toBe(15_000);
expect(resolveTelegramRequestTimeoutMs("setwebhook")).toBe(15_000);
});
it("keeps the longer polling timeout for getUpdates", () => {
expect(resolveTelegramRequestTimeoutMs("getupdates")).toBe(45_000);
});
it("does not assign hard timeouts to unrelated Telegram methods", () => {
expect(resolveTelegramRequestTimeoutMs("sendmessage")).toBeUndefined();
expect(resolveTelegramRequestTimeoutMs(null)).toBeUndefined();
});
});

View File

@@ -0,0 +1,15 @@
const TELEGRAM_REQUEST_TIMEOUTS_MS = {
// Bound startup/control-plane calls so the gateway cannot report Telegram as
// healthy while provider startup is still hung on Bot API setup.
deletewebhook: 15_000,
getme: 15_000,
getupdates: 45_000,
setwebhook: 15_000,
} as const;
export function resolveTelegramRequestTimeoutMs(method: string | null): number | undefined {
if (!method) {
return undefined;
}
return TELEGRAM_REQUEST_TIMEOUTS_MS[method as keyof typeof TELEGRAM_REQUEST_TIMEOUTS_MS];
}