perf(worker): harden chat queue dispatch (concurrency, dedup, retries) - #1195
Open
realcodesiman wants to merge 4 commits into
Open
realcodesiman wants to merge 4 commits into
realcodesiman wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements P0 items B1, B2, B5 from the system-design-review audit.
B1 — chat queue concurrency + rate limit
apps/worker/src/chat/worker.tshardcodedconcurrency: 5with no limiter — the first bottleneck for broadcast/flow/template sends. AddedCHAT_WORKER_CONCURRENCY(default 20),CHAT_WORKER_RATE_LIMIT_MAX/_DURATION_MS(default 80/1000ms) env vars inapps/worker/src/env.ts, matching the existing per-queue concurrency-var convention (WEBHOOK_WORKER_CONCURRENCYetc.). Note: BullMQ OSS'slimiteris queue-wide, not per-channel/page — a coarse throughput cap, not a per-inbox token bucket (that would be a separate, bigger feature, out of scope here).B2 — deterministic jobId for
incomingMessageMessenger, Instagram, WhatsApp, Telegram, TikTok, and Zalo webhook handlers enqueued
incomingMessagewith nojobId, so a provider retry storm created duplicate jobs (dedup only happened later, in the DB). AddedtoBullMqSafeIdSegmentto@chatbotx.io/utils(removed the duplicate private copy in the whatsapp package) and added a deterministicjobIdper channel (incoming-<channel>-<sourceId>), built only when a source id is available. TikTok's existingdelay: 2000echo-race option is preserved (merged into the same options object, not replaced).B5 — stronger retry policy for chat/integration queues
Added
resilientJobOptions(attempts: 5, exponential 10s backoff) inpackages/worker-config/src/lib/connection.ts, used only by thechatandintegrationqueues (every other queue keepsdefaultJobOptions, attempts: 2). No custom jitter — BullMQ's built-in exponential backoff has none; documenting rather than fabricating one.Verification
pnpm --filter @chatbotx.io/worker-config check-types && test— 46/46 passpnpm --filter worker check-types && test— 2339/2339 pass