WebhookBatchBuffer
class WebhookBatchBuffer
Constants
| protected CLAIM_TIMEOUT_SECONDS |
The age in seconds after which a claimed batch is treated as abandoned. |
| protected FLUSH_LOCK_MILLISECONDS |
The lifetime in milliseconds of the key that marks a flush as scheduled. |
Methods
Append an event to the buffer and check if a flush should be scheduled.
Atomically claim a batch of events from the buffer into a processing hash.
Determine whether a flush job should be scheduled for recovery.
Acknowledge a sent batch by deleting its processing hash.
Check if the buffer has remaining items.
Clear the debounce lock so a new flush can be scheduled.
Build the Redis Cluster hash tag for an application.
Lua script: RPUSH event data + SET NX debounce lock.
Lua script: atomically claim a batch, or take over an abandoned one.
Lua script: decide whether recovery should schedule a flush.
Lua script: delete the processing hash if the token still owns it.
Details
at line 22
__construct(RedisProxy $redis)
No description
at line 34
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 58
array|null
claim(string $appId, int $maxEvents, int $maxPayloadBytes)
Atomically claim a batch of events from the buffer into a processing hash.
The batch's webhook ID, time and events are stored with a claim token. A batch claimed longer ago than the claim timeout is taken over with a new token and its stored identity, so a re-sent batch has the same body and receivers can recognize it. Returns null when the buffer is empty or another flush holds a current claim.
at line 98
bool
shouldScheduleFlush(string $appId)
Determine whether a flush job should be scheduled for recovery.
A flush is needed for a batch claimed longer ago than the claim timeout, or for buffered events that no flush has claimed. The scheduling key is taken atomically, so only one of the workers checking at once schedules it.
at line 118
void
acknowledge(string $appId, string $token)
Acknowledge a sent batch by deleting its processing hash.
The hash is kept when another flush has since taken the batch over.
at line 132
bool
hasRemaining(string $appId)
Check if the buffer has remaining items.
at line 142
void
clearFlushLock(string $appId)
Clear the debounce lock so a new flush can be scheduled.
at line 152
protected string
appHashTag(string $appId)
Build the Redis Cluster hash tag for an application.
at line 167
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 197
protected string
claimScript()
Lua script: atomically claim a batch, or take over an abandoned one.
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 ARGV[4] = claim timeout in seconds ARGV[5] = claim token ARGV[6] = webhook ID for a new batch ARGV[7] = time in milliseconds for a new batch
Uses redis.call('TIME') for claimed_at so all nodes sharing Redis use the same clock source for staleness detection.
A current claim returns empty, and an abandoned one gets the new token and keeps its stored batch. Otherwise the batch is the longest prefix of the buffer within the event and byte limits, keeping the buffer's order; an oversized first event is still taken so the buffer always progresses. Returns the webhook ID, time and JSON events array of the claimed batch.
at line 255
protected string
scheduleFlushScript()
Lua script: decide whether recovery should schedule a flush.
KEYS[1] = processing hash key KEYS[2] = buffer list key KEYS[3] = lock key ARGV[1] = claim timeout in seconds ARGV[2] = lock TTL in milliseconds
A claimed batch needs a flush once its claim is older than the timeout, and unclaimed events need one whenever the buffer is non-empty. Returns 1 if the lock was newly acquired for that flush, 0 otherwise.
at line 278
protected string
acknowledgeScript()
Lua script: delete the processing hash if the token still owns it.
KEYS[1] = processing hash key ARGV[1] = claim token