Skip to content

Commit cf284c6

Browse files
committed
merge with develop branch
1 parent a27a7d7 commit cf284c6

3 files changed

Lines changed: 8 additions & 92 deletions

File tree

.env.example

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,6 @@ WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS=false
176176
WEBHOOK_EVENTS_ERRORS=false
177177
WEBHOOK_EVENTS_ERRORS_WEBHOOK=
178178

179-
# Webhook timeout and retry configuration
180-
WEBHOOK_REQUEST_TIMEOUT_MS=60000
181-
WEBHOOK_RETRY_MAX_ATTEMPTS=10
182-
WEBHOOK_RETRY_INITIAL_DELAY_SECONDS=5
183-
WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF=true
184-
WEBHOOK_RETRY_MAX_DELAY_SECONDS=300
185-
WEBHOOK_RETRY_JITTER_FACTOR=0.2
186-
# Comma separated list of HTTP status codes that should not trigger retries
187-
WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES=400,401,403,404,422
188-
189179
# Name that will be displayed on smartphone connection
190180
CONFIG_SESSION_PHONE_CLIENT=Evolution API
191181
# Browser Name = Chrome | Firefox | Edge | Opera | Safari

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

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export class WebhookController extends EventController implements EventControlle
115115
const httpService = axios.create({
116116
baseURL,
117117
headers: webhookHeaders as Record<string, string> | undefined,
118-
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
119118
});
120119

121120
await this.retryWebhookRequest(httpService, webhookData, `${origin}.sendData-Webhook`, baseURL, serverUrl);
@@ -157,10 +156,7 @@ export class WebhookController extends EventController implements EventControlle
157156

158157
try {
159158
if (regex.test(globalURL)) {
160-
const httpService = axios.create({
161-
baseURL: globalURL,
162-
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
163-
});
159+
const httpService = axios.create({ baseURL: globalURL });
164160

165161
await this.retryWebhookRequest(
166162
httpService,
@@ -194,21 +190,12 @@ export class WebhookController extends EventController implements EventControlle
194190
origin: string,
195191
baseURL: string,
196192
serverUrl: string,
197-
maxRetries?: number,
198-
delaySeconds?: number,
193+
maxRetries = 10,
194+
delaySeconds = 30,
199195
): Promise<void> {
200-
// Obter configurações de retry das variáveis de ambiente
201-
const webhookConfig = configService.get<Webhook>('WEBHOOK');
202-
const maxRetryAttempts = maxRetries ?? webhookConfig.RETRY?.MAX_ATTEMPTS ?? 10;
203-
const initialDelay = delaySeconds ?? webhookConfig.RETRY?.INITIAL_DELAY_SECONDS ?? 5;
204-
const useExponentialBackoff = webhookConfig.RETRY?.USE_EXPONENTIAL_BACKOFF ?? true;
205-
const maxDelay = webhookConfig.RETRY?.MAX_DELAY_SECONDS ?? 300;
206-
const jitterFactor = webhookConfig.RETRY?.JITTER_FACTOR ?? 0.2;
207-
const nonRetryableStatusCodes = webhookConfig.RETRY?.NON_RETRYABLE_STATUS_CODES ?? [400, 401, 403, 404, 422];
208-
209196
let attempts = 0;
210197

211-
while (attempts < maxRetryAttempts) {
198+
while (attempts < maxRetries) {
212199
try {
213200
await httpService.post('', webhookData);
214201
if (attempts > 0) {
@@ -222,58 +209,24 @@ export class WebhookController extends EventController implements EventControlle
222209
} catch (error) {
223210
attempts++;
224211

225-
// Verificar se é um erro de timeout
226-
const isTimeout = error.code === 'ECONNABORTED';
227-
228-
// Verificar se o erro não deve gerar retry com base no status code
229-
if (error?.response?.status && nonRetryableStatusCodes.includes(error.response.status)) {
230-
this.logger.error({
231-
local: `${origin}`,
232-
message: `Erro não recuperável (${error.response.status}): ${error?.message}. Cancelando retentativas.`,
233-
statusCode: error?.response?.status,
234-
url: baseURL,
235-
server_url: serverUrl,
236-
});
237-
throw error;
238-
}
239-
240212
this.logger.error({
241213
local: `${origin}`,
242-
message: `Tentativa ${attempts}/${maxRetryAttempts} falhou: ${isTimeout ? 'Timeout da requisição' : error?.message}`,
214+
message: `Tentativa ${attempts}/${maxRetries} falhou: ${error?.message}`,
243215
hostName: error?.hostname,
244216
syscall: error?.syscall,
245217
code: error?.code,
246-
isTimeout,
247-
statusCode: error?.response?.status,
248218
error: error?.errno,
249219
stack: error?.stack,
250220
name: error?.name,
251221
url: baseURL,
252222
server_url: serverUrl,
253223
});
254224

255-
if (attempts === maxRetryAttempts) {
225+
if (attempts === maxRetries) {
256226
throw error;
257227
}
258228

259-
// Cálculo do delay com backoff exponencial e jitter
260-
let nextDelay = initialDelay;
261-
if (useExponentialBackoff) {
262-
// Fórmula: initialDelay * (2^attempts) com limite máximo
263-
nextDelay = Math.min(initialDelay * Math.pow(2, attempts - 1), maxDelay);
264-
265-
// Adicionar jitter para evitar "thundering herd"
266-
const jitter = nextDelay * jitterFactor * (Math.random() * 2 - 1);
267-
nextDelay = Math.max(initialDelay, nextDelay + jitter);
268-
}
269-
270-
this.logger.log({
271-
local: `${origin}`,
272-
message: `Aguardando ${nextDelay.toFixed(1)} segundos antes da próxima tentativa`,
273-
url: baseURL,
274-
});
275-
276-
await new Promise((resolve) => setTimeout(resolve, nextDelay * 1000));
229+
await new Promise((resolve) => setTimeout(resolve, delaySeconds * 1000));
277230
}
278231
}
279232
}

src/config/env.config.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,7 @@ export type CacheConfLocal = {
232232
TTL: number;
233233
};
234234
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
235-
export type Webhook = {
236-
GLOBAL?: GlobalWebhook;
237-
EVENTS: EventsWebhook;
238-
REQUEST?: {
239-
TIMEOUT_MS?: number;
240-
};
241-
RETRY?: {
242-
MAX_ATTEMPTS?: number;
243-
INITIAL_DELAY_SECONDS?: number;
244-
USE_EXPONENTIAL_BACKOFF?: boolean;
245-
MAX_DELAY_SECONDS?: number;
246-
JITTER_FACTOR?: number;
247-
NON_RETRYABLE_STATUS_CODES?: number[];
248-
};
249-
};
235+
export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook };
250236
export type Pusher = { ENABLED: boolean; GLOBAL?: GlobalPusher; EVENTS: EventsPusher };
251237
export type ConfigSessionPhone = { CLIENT: string; NAME: string; VERSION: string };
252238
export type QrCode = { LIMIT: number; COLOR: string };
@@ -565,19 +551,6 @@ export class ConfigService {
565551
ERRORS: process.env?.WEBHOOK_EVENTS_ERRORS === 'true',
566552
ERRORS_WEBHOOK: process.env?.WEBHOOK_EVENTS_ERRORS_WEBHOOK || '',
567553
},
568-
REQUEST: {
569-
TIMEOUT_MS: Number.parseInt(process.env?.WEBHOOK_REQUEST_TIMEOUT_MS) || 30000,
570-
},
571-
RETRY: {
572-
MAX_ATTEMPTS: Number.parseInt(process.env?.WEBHOOK_RETRY_MAX_ATTEMPTS) || 10,
573-
INITIAL_DELAY_SECONDS: Number.parseInt(process.env?.WEBHOOK_RETRY_INITIAL_DELAY_SECONDS) || 5,
574-
USE_EXPONENTIAL_BACKOFF: process.env?.WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF !== 'false',
575-
MAX_DELAY_SECONDS: Number.parseInt(process.env?.WEBHOOK_RETRY_MAX_DELAY_SECONDS) || 300,
576-
JITTER_FACTOR: Number.parseFloat(process.env?.WEBHOOK_RETRY_JITTER_FACTOR) || 0.2,
577-
NON_RETRYABLE_STATUS_CODES: process.env?.WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES?.split(',').map(Number) || [
578-
400, 401, 403, 404, 422,
579-
],
580-
},
581554
},
582555
CONFIG_SESSION_PHONE: {
583556
CLIENT: process.env?.CONFIG_SESSION_PHONE_CLIENT || 'Evolution API',

0 commit comments

Comments
 (0)