Skip to content

Commit 0597993

Browse files
fix(webhook): implementar timeout configurável e sistema de retentativas inteligente
1 parent e37a3cc commit 0597993

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/api/integrations/event/webhook/webhook.controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class WebhookController extends EventController implements EventControlle
157157

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

src/config/env.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ export type CacheConfLocal = {
220220
TTL: number;
221221
};
222222
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
223-
export type Webhook = {
224-
GLOBAL?: GlobalWebhook;
223+
export type Webhook = {
224+
GLOBAL?: GlobalWebhook;
225225
EVENTS: EventsWebhook;
226226
REQUEST?: {
227227
TIMEOUT_MS?: number;
@@ -520,7 +520,9 @@ export class ConfigService {
520520
USE_EXPONENTIAL_BACKOFF: process.env?.WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF !== 'false',
521521
MAX_DELAY_SECONDS: Number.parseInt(process.env?.WEBHOOK_RETRY_MAX_DELAY_SECONDS) || 300,
522522
JITTER_FACTOR: Number.parseFloat(process.env?.WEBHOOK_RETRY_JITTER_FACTOR) || 0.2,
523-
NON_RETRYABLE_STATUS_CODES: process.env?.WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES?.split(',').map(Number) || [400, 401, 403, 404, 422],
523+
NON_RETRYABLE_STATUS_CODES: process.env?.WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES?.split(',').map(Number) || [
524+
400, 401, 403, 404, 422,
525+
],
524526
},
525527
},
526528
CONFIG_SESSION_PHONE: {

0 commit comments

Comments
 (0)