Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Sistema de webhooks: melhoria com timeout e retentativas configuráveis #1341

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

guilhermejansen
Copy link

@guilhermejansen guilhermejansen commented Mar 26, 2025

Referência

Resolve #1325

Descrição

Esta PR implementa melhorias significativas no sistema de webhooks da API, adicionando configurabilidade via variáveis de ambiente e corrigindo o bug crítico que causava duplicação de webhooks.

Problemas Resolvidos

  1. Correção de bug crítico: Webhooks estavam sendo enviados várias vezes mesmo quando o servidor respondia com sucesso
  2. Timeout configurável: Agora é possível definir o tempo máximo de espera por uma resposta
  3. Retentativas inteligentes: Implementação de backoff exponencial com jitter para evitar sobrecarga
  4. Detecção de erros permanentes: Não tenta novamente em casos de erros como 400, 401, 403, etc.
  5. Logs aprimorados: Mais detalhes sobre falhas para facilitar diagnóstico

Novas Configurações

Todas estas configurações são opcionais e possuem valores padrão razoáveis:

# Webhook timeout e configuração de retry
WEBHOOK_REQUEST_TIMEOUT_MS=30000
WEBHOOK_RETRY_MAX_ATTEMPTS=10
WEBHOOK_RETRY_INITIAL_DELAY_SECONDS=5
WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF=true
WEBHOOK_RETRY_MAX_DELAY_SECONDS=300
WEBHOOK_RETRY_JITTER_FACTOR=0.2
# Lista de códigos HTTP que não devem gerar retentativas
WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES=400,401,403,404,422

Impacto e Compatibilidade

  • Esta mudança é totalmente compatível com versões anteriores
  • As novas configurações são opcionais, mantendo o comportamento padrão se não forem especificadas
  • Melhora significativa na estabilidade e eficiência para sistemas com alto volume de webhooks

Testes Realizados

  • Testei a implementação com diferentes cenários:
    • Servidor de destino lento (simulando timeout)
    • Servidor respondendo com diferentes códigos de erro (400, 404, 500)
    • Servidor funcionando normalmente (confirmação de que não há mais duplicação)

Alterações Implementadas

  • Adicionada interface para configuração de timeout e retry
  • Adicionadas variáveis de ambiente para webhook
  • Implementado timeout configurável
  • Implementado backoff exponencial e correção de duplicação

Summary by Sourcery

Improves the webhook system by adding configurable options for timeout and retry logic, and fixes a bug that caused duplicate webhooks. It introduces environment variables to configure the webhook system.

Bug Fixes:

  • Fixes a bug that caused webhooks to be sent multiple times even when the server responded successfully.

Enhancements:

  • Adds configurable timeout for webhook requests.
  • Implements exponential backoff with jitter for webhook retries.
  • Adds logic to avoid retrying on permanent errors such as 400, 401, and 403 status codes.
  • Improves logging for webhook failures to facilitate diagnosis.

Summary by Sourcery

Improve the webhook system by adding configurable timeout, retry logic, and preventing duplicate webhook sends

New Features:

  • Add support for business-related features like fetching catalog and collections
  • Introduce NATS integration for event messaging

Bug Fixes:

  • Fix issue with duplicate webhook sends
  • Prevent multiple webhook sends for the same message
  • Improve handling of webhook error scenarios

Enhancements:

  • Implement configurable webhook timeout and retry mechanism
  • Add exponential backoff with jitter for webhook retries
  • Improve message deduplication logic
  • Enhance error handling for webhook requests

Deployment:

  • Update Docker configurations
  • Update GitHub Actions workflows to use new Docker image repository

Documentation:

  • Update README with new contributors
  • Update licensing contact information

…vas inteligente

Resolve EvolutionAPI#1325

- Adiciona configuração de timeout via variáveis de ambiente
- Implementa backoff exponencial com jitter para retentativas
- Detecta erros permanentes para evitar retentativas desnecessárias
- Corrige bug de duplicação de webhooks
- Melhora logs para diagnóstico
Copy link

sourcery-ai bot commented Mar 26, 2025

Reviewer's Guide by Sourcery

This pull request enhances the webhook system by introducing configurable timeout and retry logic, and fixes a critical bug that caused duplicate webhooks. It also adds support for NATS integration, improves logging, and updates several components to enhance stability and efficiency.

Updated class diagram for webhook configuration

classDiagram
    class Webhook {
        +GlobalWebhook GLOBAL
        +EventsWebhook EVENTS
        +RequestConfig REQUEST
        +RetryConfig RETRY
    }
    class RequestConfig {
        +int TIMEOUT_MS
    }
    class RetryConfig {
        +int MAX_ATTEMPTS
        +int INITIAL_DELAY_SECONDS
        +bool USE_EXPONENTIAL_BACKOFF
        +int MAX_DELAY_SECONDS
        +float JITTER_FACTOR
        +int[] NON_RETRYABLE_STATUS_CODES
    }
    Webhook --> RequestConfig
    Webhook --> RetryConfig
Loading

File-Level Changes

Change Details Files
Fixes a critical bug causing duplicate webhooks.
  • Implemented caching to avoid duplicate messages.
  • Updated message handling logic to prevent retries on successful responses.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Introduces configurable timeout and retry logic for webhooks.
  • Added environment variables for webhook timeout and retry settings.
  • Implemented exponential backoff with jitter for retries.
  • Added logic to avoid retries on permanent errors like 400, 401, 403 status codes.
src/config/env.config.ts
src/api/integrations/event/webhook/webhook.controller.ts
Adds support for NATS integration.
  • Implemented NATS controller and router.
  • Added NATS configuration to environment settings.
  • Updated event manager to include NATS.
src/api/integrations/event/nats/nats.controller.ts
src/api/integrations/event/nats/nats.router.ts
src/config/env.config.ts
src/api/integrations/event/event.manager.ts
Enhances logging for better diagnostics.
  • Improved logging messages for webhook failures.
  • Added detailed logs for NATS and SQS operations.
src/api/integrations/event/webhook/webhook.controller.ts
src/api/integrations/event/sqs/sqs.controller.ts
src/api/integrations/event/nats/nats.controller.ts
Updates Docker and environment configurations.
  • Changed Docker image references to new repository.
  • Updated contact information in Dockerfile and README.
Dockerfile
README.md
.github/workflows/publish_docker_image.yml
.github/workflows/publish_docker_image_homolog.yml
.github/workflows/publish_docker_image_latest.yml

Assessment against linked issues

Issue Objective Addressed Explanation
#1325 Make the webhook request timeout configurable via environment variables.
#1325 Allow configuration of retry parameters, including maximum attempts, initial interval, and implement exponential backoff with jitter.
#1325 Fix the critical bug where webhooks are sent multiple times even when the server responds successfully.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@guilhermejansen guilhermejansen changed the base branch from main to develop March 26, 2025 22:17
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We encountered an error and are unable to review this PR. We have been notified and are working to fix it.

You can try again by commenting this pull request with @sourcery-ai review, or contact us for help.

@acauaferreira
Copy link

Pelo amor da deusa põe libera o update disso chefinho @DavidsonGomes .

Meu bot do dyfy trava com erros de timeout ou quando da algum erro tipo 401 e nao tenta denovo enviar ou receber uma msg ,e fica pra sempre travado.

Resolveria esse cenário? Pois são "conversas entre webhook" essas integrações de chabot,correto?

Comment on lines +160 to +163
const httpService = axios.create({
baseURL: globalURL,
timeout: webhookConfig.REQUEST?.TIMEOUT_MS ?? 30000,
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acredito que de para criar um método que receba a url e o timeout em milisegundos como parâmetro pra poder usar tanto nessas linhas quanto na linha 115-118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants