mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-02 14:19:56 +00:00
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.
This commit is contained in:
+10
-4
@@ -106,9 +106,11 @@ export class GlmtProxy {
|
|||||||
this.timeout = config.timeout || 120000; // 120s default
|
this.timeout = config.timeout || 120000; // 120s default
|
||||||
|
|
||||||
// Retry configuration for handling 429 rate limit errors
|
// 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 = {
|
this.retryConfig = {
|
||||||
maxRetries: parseInt(process.env.GLMT_MAX_RETRIES || '3', 10),
|
maxRetries: isNaN(maxRetries) || maxRetries < 0 ? 3 : maxRetries,
|
||||||
baseDelay: parseInt(process.env.GLMT_RETRY_BASE_DELAY || '1000', 10),
|
baseDelay: isNaN(baseDelay) || baseDelay < 0 ? 1000 : baseDelay,
|
||||||
enabled: process.env.GLMT_DISABLE_RETRY !== '1',
|
enabled: process.env.GLMT_DISABLE_RETRY !== '1',
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -387,8 +389,12 @@ export class GlmtProxy {
|
|||||||
return retryAfter * 1000; // Convert seconds to ms
|
return retryAfter * 1000; // Convert seconds to ms
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Exponential backoff: 2^attempt * baseDelay + random jitter (0-500ms)
|
// Exponential backoff: 2^attempt * baseDelay + random jitter (0-500ms), capped at 30s
|
||||||
const exponentialDelay = Math.pow(2, attempt) * this.retryConfig.baseDelay;
|
const MAX_DELAY_MS = 30000;
|
||||||
|
const exponentialDelay = Math.min(
|
||||||
|
Math.pow(2, attempt) * this.retryConfig.baseDelay,
|
||||||
|
MAX_DELAY_MS
|
||||||
|
);
|
||||||
const jitter = Math.random() * 500;
|
const jitter = Math.random() * 500;
|
||||||
return exponentialDelay + jitter;
|
return exponentialDelay + jitter;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user