class WebhookBatchBuffer

Methods

__construct(RedisProxy $redis)

No description

bool
appendAndCheckSchedule(string $appId, array $eventData)

Append an event to the buffer and check if a flush should be scheduled.

array
claim(string $appId, int $maxEvents, int $maxPayloadBytes)

Atomically claim events from the buffer into a processing hash.

bool
recoverStaleProcessingKeys(string $appId, int $maxAgeSeconds = 60)

Recover stale processing keys from crashed flush jobs.

void
acknowledge(string $appId)

Acknowledge successful processing — delete the processing key.

bool
hasRemaining(string $appId)

Check if the buffer has remaining items.

void
clearFlushLock(string $appId)

Clear the debounce lock so a new flush can be scheduled.

string
appendAndLockScript()

Lua script: RPUSH event data + SET NX debounce lock.

string
claimScript()

Lua script: atomically claim, trim by byte budget, and store in processing hash.

string
recoverScript()

Lua script: atomically recover stale processing events to the buffer.

Details

at line 11
__construct(RedisProxy $redis)

No description

Parameters

RedisProxy $redis

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).

Parameters

string $appId
array $eventData

Return Value

bool

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.

Parameters

string $appId
int $maxEvents
int $maxPayloadBytes

Return Value

array

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).

Parameters

string $appId
int $maxAgeSeconds

Return Value

bool

at line 94
void acknowledge(string $appId)

Acknowledge successful processing — delete the processing key.

Parameters

string $appId

Return Value

void

at line 102
bool hasRemaining(string $appId)

Check if the buffer has remaining items.

Parameters

string $appId

Return Value

bool

at line 110
void clearFlushLock(string $appId)

Clear the debounce lock so a new flush can be scheduled.

Parameters

string $appId

Return Value

void

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.

Return Value

string

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.

Return Value

string

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.

Return Value

string