fix(webhook): implementar timeout configurável e sistema de retentativas inteligente

This commit is contained in:
Guilherme Jansen 2025-03-21 14:56:45 -03:00
parent e37a3cc2d6
commit 0597993c5d
2 changed files with 9 additions and 7 deletions

View File

@ -157,7 +157,7 @@ export class WebhookController extends EventController implements EventControlle
try {
if (isURL(globalURL)) {
const httpService = axios.create({
const httpService = axios.create({
baseURL: globalURL,
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
});
@ -221,10 +221,10 @@ export class WebhookController extends EventController implements EventControlle
return;
} catch (error) {
attempts++;
// Verificar se é um erro de timeout
const isTimeout = error.code === 'ECONNABORTED';
// Verificar se o erro não deve gerar retry com base no status code
if (error?.response?.status && nonRetryableStatusCodes.includes(error.response.status)) {
this.logger.error({
@ -261,7 +261,7 @@ export class WebhookController extends EventController implements EventControlle
if (useExponentialBackoff) {
// Fórmula: initialDelay * (2^attempts) com limite máximo
nextDelay = Math.min(initialDelay * Math.pow(2, attempts - 1), maxDelay);
// Adicionar jitter para evitar "thundering herd"
const jitter = nextDelay * jitterFactor * (Math.random() * 2 - 1);
nextDelay = Math.max(initialDelay, nextDelay + jitter);

View File

@ -220,8 +220,8 @@ export type CacheConfLocal = {
TTL: number;
};
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
export type Webhook = {
GLOBAL?: GlobalWebhook;
export type Webhook = {
GLOBAL?: GlobalWebhook;
EVENTS: EventsWebhook;
REQUEST?: {
TIMEOUT_MS?: number;
@ -520,7 +520,9 @@ export class ConfigService {
USE_EXPONENTIAL_BACKOFF: process.env?.WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF !== 'false',
MAX_DELAY_SECONDS: Number.parseInt(process.env?.WEBHOOK_RETRY_MAX_DELAY_SECONDS) || 300,
JITTER_FACTOR: Number.parseFloat(process.env?.WEBHOOK_RETRY_JITTER_FACTOR) || 0.2,
NON_RETRYABLE_STATUS_CODES: process.env?.WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES?.split(',').map(Number) || [400, 401, 403, 404, 422],
NON_RETRYABLE_STATUS_CODES: process.env?.WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES?.split(',').map(Number) || [
400, 401, 403, 404, 422,
],
},
},
CONFIG_SESSION_PHONE: {