feat: add debounce — distributed first-signal-fires debounce primitive#62
Closed
freshlogic wants to merge 1 commit intomainfrom
Closed
feat: add debounce — distributed first-signal-fires debounce primitive#62freshlogic wants to merge 1 commit intomainfrom
freshlogic wants to merge 1 commit intomainfrom
Conversation
Coverage Report for CI Build 24942513116Coverage decreased (-0.2%) to 99.751%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
316541d to
560936d
Compare
Coalesces calls for the same key across multiple processes via Redis so that the callback runs at most once per `wait` window. Uses a single SETNX with PX TTL for atomicity. Cleans up state on callback throw so the next caller can retry immediately. Reference: imitates BullMQ's job-deduplication mechanic (SET NX PX). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
560936d to
6c45b5a
Compare
6 tasks
Member
Author
|
Closing — pettyCache.mutex.lock already provides everything we need (SETNX with TTL). The debounce primitive added complexity (UUID stamps, in-process setTimeout, mutex coordination) without enough benefit for the use cases we've actually identified. Will reopen if a real need surfaces. |
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.
Summary
Adds
pettyCache.debounce(key, { wait }, fn)— a distributed debounce primitive backed by Redis. Async/await only.The pattern: first signal in a quiet
waitwindow invokes the callback; subsequent signals during the window are absorbed. After TTL expires, the next signal can invoke again. If the callback throws, the state is cleared so the next caller can retry immediately.Why
The first concrete use case is Stores.com's segmentation-service rollup pipeline — 5 source services emit events that all signal "refresh accountId X." We want one refresh per ~5-min burst, not 50. Inline implementation gets fiddly fast (race conditions on check+set, partial failure cleanup, etc.). Centralizing it here keeps the convention enforceable across services.
Other expected callers: catalog reindexing on item updates, dashboard recomputes, webhook re-fires.
Reference
Imitates BullMQ's job deduplication mechanic — single SET NX PX for atomicity. Differences from BullMQ:
refreshDate(the scheduled fire time) — useful forscheduledEnqueueTimeUtc, idempotency keysAPI
Test plan
What was considered and rejected
🤖 Generated with Claude Code