mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 17:38:40 -06:00
fix(webhook): implementar timeout configurável e sistema de retentativas inteligente
This commit is contained in:
parent
e37a3cc2d6
commit
0597993c5d
@ -157,7 +157,7 @@ export class WebhookController extends EventController implements EventControlle
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (isURL(globalURL)) {
|
if (isURL(globalURL)) {
|
||||||
const httpService = axios.create({
|
const httpService = axios.create({
|
||||||
baseURL: globalURL,
|
baseURL: globalURL,
|
||||||
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
|
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
|
||||||
});
|
});
|
||||||
@ -221,10 +221,10 @@ export class WebhookController extends EventController implements EventControlle
|
|||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
attempts++;
|
attempts++;
|
||||||
|
|
||||||
// Verificar se é um erro de timeout
|
// Verificar se é um erro de timeout
|
||||||
const isTimeout = error.code === 'ECONNABORTED';
|
const isTimeout = error.code === 'ECONNABORTED';
|
||||||
|
|
||||||
// Verificar se o erro não deve gerar retry com base no status code
|
// Verificar se o erro não deve gerar retry com base no status code
|
||||||
if (error?.response?.status && nonRetryableStatusCodes.includes(error.response.status)) {
|
if (error?.response?.status && nonRetryableStatusCodes.includes(error.response.status)) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
@ -261,7 +261,7 @@ export class WebhookController extends EventController implements EventControlle
|
|||||||
if (useExponentialBackoff) {
|
if (useExponentialBackoff) {
|
||||||
// Fórmula: initialDelay * (2^attempts) com limite máximo
|
// Fórmula: initialDelay * (2^attempts) com limite máximo
|
||||||
nextDelay = Math.min(initialDelay * Math.pow(2, attempts - 1), maxDelay);
|
nextDelay = Math.min(initialDelay * Math.pow(2, attempts - 1), maxDelay);
|
||||||
|
|
||||||
// Adicionar jitter para evitar "thundering herd"
|
// Adicionar jitter para evitar "thundering herd"
|
||||||
const jitter = nextDelay * jitterFactor * (Math.random() * 2 - 1);
|
const jitter = nextDelay * jitterFactor * (Math.random() * 2 - 1);
|
||||||
nextDelay = Math.max(initialDelay, nextDelay + jitter);
|
nextDelay = Math.max(initialDelay, nextDelay + jitter);
|
||||||
|
@ -220,8 +220,8 @@ export type CacheConfLocal = {
|
|||||||
TTL: number;
|
TTL: number;
|
||||||
};
|
};
|
||||||
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
|
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
|
||||||
export type Webhook = {
|
export type Webhook = {
|
||||||
GLOBAL?: GlobalWebhook;
|
GLOBAL?: GlobalWebhook;
|
||||||
EVENTS: EventsWebhook;
|
EVENTS: EventsWebhook;
|
||||||
REQUEST?: {
|
REQUEST?: {
|
||||||
TIMEOUT_MS?: number;
|
TIMEOUT_MS?: number;
|
||||||
@ -520,7 +520,9 @@ export class ConfigService {
|
|||||||
USE_EXPONENTIAL_BACKOFF: process.env?.WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF !== 'false',
|
USE_EXPONENTIAL_BACKOFF: process.env?.WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF !== 'false',
|
||||||
MAX_DELAY_SECONDS: Number.parseInt(process.env?.WEBHOOK_RETRY_MAX_DELAY_SECONDS) || 300,
|
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,
|
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: {
|
CONFIG_SESSION_PHONE: {
|
||||||
|
Loading…
Reference in New Issue
Block a user