From 67a8e2cefcedad2f5a26f0d43952219379be5cc0 Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Fri, 30 Jan 2026 08:05:38 -0500 Subject: [PATCH] fix(glmt): add env var validation and max delay cap - Validate GLMT_MAX_RETRIES and GLMT_RETRY_BASE_DELAY for NaN/negative - Add 30s max delay cap to prevent excessively long waits Addresses PR review feedback. --- src/glmt/glmt-proxy.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/glmt/glmt-proxy.ts b/src/glmt/glmt-proxy.ts index 1a07d163..73a24315 100644 --- a/src/glmt/glmt-proxy.ts +++ b/src/glmt/glmt-proxy.ts @@ -106,9 +106,11 @@ export class GlmtProxy { this.timeout = config.timeout || 120000; // 120s default // Retry configuration for handling 429 rate limit errors + const maxRetries = parseInt(process.env.GLMT_MAX_RETRIES || '3', 10); + const baseDelay = parseInt(process.env.GLMT_RETRY_BASE_DELAY || '1000', 10); this.retryConfig = { - maxRetries: parseInt(process.env.GLMT_MAX_RETRIES || '3', 10), - baseDelay: parseInt(process.env.GLMT_RETRY_BASE_DELAY || '1000', 10), + maxRetries: isNaN(maxRetries) || maxRetries < 0 ? 3 : maxRetries, + baseDelay: isNaN(baseDelay) || baseDelay < 0 ? 1000 : baseDelay, enabled: process.env.GLMT_DISABLE_RETRY !== '1', }; @@ -387,8 +389,12 @@ export class GlmtProxy { return retryAfter * 1000; // Convert seconds to ms } } - // Exponential backoff: 2^attempt * baseDelay + random jitter (0-500ms) - const exponentialDelay = Math.pow(2, attempt) * this.retryConfig.baseDelay; + // Exponential backoff: 2^attempt * baseDelay + random jitter (0-500ms), capped at 30s + const MAX_DELAY_MS = 30000; + const exponentialDelay = Math.min( + Math.pow(2, attempt) * this.retryConfig.baseDelay, + MAX_DELAY_MS + ); const jitter = Math.random() * 500; return exponentialDelay + jitter; }