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

__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|null
claim(string $appId, int $maxEvents, int $maxPayloadBytes)

Atomically claim a batch of events from the buffer into a processing hash.

bool
shouldScheduleFlush(string $appId)

Determine whether a flush job should be scheduled for recovery.

void
acknowledge(string $appId, string $token)

Acknowledge a sent batch by deleting its processing hash.

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
appHashTag(string $appId)

Build the Redis Cluster hash tag for an application.

string
appendAndLockScript()

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

string
claimScript()

Lua script: atomically claim a batch, or take over an abandoned one.

string
scheduleFlushScript()

Lua script: decide whether recovery should schedule a flush.

string
acknowledgeScript()

Lua script: delete the processing hash if the token still owns it.

Details

at line 22
__construct(RedisProxy $redis)

No description

Parameters

RedisProxy $redis

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

Parameters

string $appId
array $eventData

Return Value

bool

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.

Parameters

string $appId
int $maxEvents
int $maxPayloadBytes

Return Value

array|null

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.

Parameters

string $appId

Return Value

bool

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.

Parameters

string $appId
string $token

Return Value

void

at line 132
bool hasRemaining(string $appId)

Check if the buffer has remaining items.

Parameters

string $appId

Return Value

bool

at line 142
void clearFlushLock(string $appId)

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

Parameters

string $appId

Return Value

void

at line 152
protected string appHashTag(string $appId)

Build the Redis Cluster hash tag for an application.

Parameters

string $appId

Return Value

string

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.

Return Value

string

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.

Return Value

string

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.

Return Value

string

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

Return Value

string