WebhookBatchBuffer
class WebhookBatchBuffer
Methods
Append an event to the buffer and check if a flush should be scheduled.
Atomically claim events from the buffer into a processing hash.
Recover stale processing keys from crashed flush jobs.
Acknowledge successful processing — delete the processing key.
Check if the buffer has remaining items.
Clear the debounce lock so a new flush can be scheduled.
Lua script: RPUSH event data + SET NX debounce lock.
Lua script: atomically claim, trim by byte budget, and store in processing hash.
Lua script: atomically recover stale processing events to the buffer.
Details
at line 11
__construct(RedisProxy $redis)
No description
at line 23
bool
appendAndCheckSchedule(string $appId, array $eventData)
Append an event to the buffer and check if a flush should be scheduled.
Uses a single Lua script for one Redis round-trip: RPUSH the event data and SET NX the debounce lock. Returns true if the lock was newly acquired (caller should schedule a flush job).
at line 49
array
claim(string $appId, int $maxEvents, int $maxPayloadBytes)
Atomically claim events from the buffer into a processing hash.
A single Lua script handles everything in one Redis round-trip:
- Guards against concurrent flushes (returns empty if processing hash exists)
- Claims up to $maxEvents from the buffer
- Applies $maxPayloadBytes limit, pushing overflow back to the buffer
- Stores only the retained events + timestamp in the processing hash
Returns the retained events as decoded arrays. Empty return means either the buffer is empty or another flush is in-flight.
at line 79
bool
recoverStaleProcessingKeys(string $appId, int $maxAgeSeconds = 60)
Recover stale processing keys from crashed flush jobs.
Uses an atomic Lua script so multiple workers calling this concurrently don't duplicate events — the first worker recovers the events, the rest no-op because the key is already deleted.
Returns true if events were recovered (caller should schedule a flush).
at line 94
void
acknowledge(string $appId)
Acknowledge successful processing — delete the processing key.
at line 102
bool
hasRemaining(string $appId)
Check if the buffer has remaining items.
at line 110
void
clearFlushLock(string $appId)
Clear the debounce lock so a new flush can be scheduled.
at line 125
protected string
appendAndLockScript()
Lua script: RPUSH event data + SET NX debounce lock.
KEYS[1] = buffer list key KEYS[2] = lock key ARGV[1] = JSON event data ARGV[2] = lock TTL in milliseconds
Returns 1 if lock was newly acquired, 0 if already held.
at line 150
protected string
claimScript()
Lua script: atomically claim, trim by byte budget, and store in processing hash.
KEYS[1] = buffer list key KEYS[2] = processing hash key ARGV[1] = max events to claim ARGV[2] = max payload bytes ARGV[3] = envelope overhead bytes
Uses redis.call('TIME') for claimed_at so all nodes sharing Redis use the same clock source for staleness detection.
Guards against concurrent flushes: returns empty if processing hash exists. Claims up to max events, applies byte budget, pushes overflow back to the buffer, and stores only the retained events in the processing hash. Returns the retained event strings.
at line 211
protected string
recoverScript()
Lua script: atomically recover stale processing events to the buffer.
KEYS[1] = processing hash key KEYS[2] = buffer list key ARGV[1] = max age in seconds
Uses redis.call('TIME') for the current timestamp so all nodes sharing Redis use the same clock source as the claim script.
If the processing hash exists and claimed_at is older than max age, pushes all events back to the front of the buffer and deletes the hash. Returns 1 if recovered, 0 if no-op. Safe for concurrent calls.