Skip to content

Commit 22e5926

Browse files
committed
fix: improve logging and documentation clarity in RedisPersistenceLayer
1 parent 39167e7 commit 22e5926

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

packages/idempotency/src/persistence/RedisPersistenceLayer.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
218218

219219
const encodedItem = JSON.stringify(item);
220220
const ttl = this.#getExpirySeconds(record.expiryTimestamp);
221-
const now = Date.now();
222221

223222
try {
224223
/**
@@ -229,14 +228,15 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
229228
* | (in_progress_expiry) (expiry)
230229
*
231230
* Conditions to successfully save a record:
232-
* * The idempotency key does not exist:
231+
*
232+
* The idempotency key does not exist:
233233
* - first time that this invocation key is used
234234
* - previous invocation with the same key was deleted due to TTL
235235
* - SET see https://redis.io/commands/set/
236236
*/
237237

238238
console.debug(
239-
`Putting record for idempotency key: ${record.idempotencyKey}`
239+
`Putting record on redis for idempotency key: ${record.idempotencyKey}`
240240
);
241241
const response = await this.#client.set(
242242
record.idempotencyKey,
@@ -268,9 +268,8 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
268268
const existingRecord = await this._getRecord(record.idempotencyKey);
269269

270270
/** If the status of the idempotency record is `COMPLETED` and the record has not expired
271-
* (i.e., the expiry timestamp is greater than the current timestamp), then a valid completed
272-
* record exists. We raise an error to prevent duplicate processing of a request that has already
273-
* been completed successfully.
271+
* then a valid completed record exists. We raise an error to prevent duplicate processing
272+
* of a request that has already been completed successfully.
274273
*/
275274
if (
276275
existingRecord.getStatus() === IdempotencyRecordStatus.COMPLETED &&
@@ -283,14 +282,14 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
283282
}
284283

285284
/** If the idempotency record has a status of 'INPROGRESS' and has a valid `inProgressExpiryTimestamp`
286-
* (meaning the timestamp is greater than the current timestamp in milliseconds), then we have encountered
287-
* a valid in-progress record. This indicates that another process is currently handling the request, and
288-
* to maintain idempotency, we raise an error to prevent concurrent processing of the same request.
285+
* (meaning the timestamp is greater than the current timestamp in milliseconds), then we have encountered
286+
* a valid in-progress record. This indicates that another process is currently handling the request, and
287+
* to maintain idempotency, we raise an error to prevent concurrent processing of the same request.
289288
*/
290289
if (
291290
existingRecord.getStatus() === IdempotencyRecordStatus.INPROGRESS &&
292291
existingRecord.inProgressExpiryTimestamp &&
293-
existingRecord.inProgressExpiryTimestamp > now
292+
existingRecord.inProgressExpiryTimestamp > Date.now()
294293
) {
295294
throw new IdempotencyItemAlreadyExistsError(
296295
`Failed to put record for in-progress idempotency key: ${record.idempotencyKey}`,
@@ -299,19 +298,19 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
299298
}
300299

301300
/** Reaching this point indicates that the idempotency record found is an orphan record. An orphan record is
302-
* one that is neither completed nor in-progress within its expected time frame. It may result from a
303-
* previous invocation that has timed out or an expired record that has yet to be cleaned up by Redis.
304-
* We raise an error to handle this exceptional scenario appropriately.
301+
* one that is neither completed nor in-progress within its expected time frame. It may result from a
302+
* previous invocation that has timed out or an expired record that has yet to be cleaned up by Redis.
303+
* We raise an error to handle this exceptional scenario appropriately.
305304
*/
306305
throw new IdempotencyPersistenceConsistencyError(
307306
'Orphaned record detected'
308307
);
309308
} catch (error) {
310309
if (error instanceof IdempotencyPersistenceConsistencyError) {
311310
/** Handle an orphan record by attempting to acquire a lock, which by default lasts for 10 seconds.
312-
* The purpose of acquiring the lock is to prevent race conditions with other processes that might
313-
* also be trying to handle the same orphan record. Once the lock is acquired, we set a new value
314-
* for the idempotency record in Redis with the appropriate time-to-live (TTL).
311+
* The purpose of acquiring the lock is to prevent race conditions with other processes that might
312+
* also be trying to handle the same orphan record. Once the lock is acquired, we set a new value
313+
* for the idempotency record in Redis with the appropriate time-to-live (TTL).
315314
*/
316315
await this.#acquireLock(record.idempotencyKey);
317316

@@ -337,9 +336,9 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
337336

338337
/**
339338
* Attempt to acquire a lock for a specified resource name, with a default timeout.
340-
* This method attempts to set a lock using Redis to prevent concurrent
341-
* access to a resource identified by 'idempotencyKey'. It uses the 'NX' flag to ensure that
342-
* the lock is only set if it does not already exist, thereby enforcing mutual exclusion.
339+
* This method attempts to set a lock using Redis to prevent concurrent access to a resource
340+
* identified by 'idempotencyKey'. It uses the 'NX' flag to ensure that the lock is only
341+
* set if it does not already exist, thereby enforcing mutual exclusion.
343342
*
344343
* @param idempotencyKey - The key to create a lock for
345344
*/
@@ -354,9 +353,10 @@ class RedisPersistenceLayer extends BasePersistenceLayer {
354353
});
355354

356355
if (acquired) return;
357-
// If the lock acquisition fails, it suggests a race condition has occurred. In this case, instead of
358-
// proceeding, we log the event and raise an error to indicate that the current operation should be
359-
// retried after the lock is released by the process that currently holds it.
356+
/** If the lock acquisition fails, it suggests a race condition has occurred. In this case, instead of
357+
* proceeding, we log the event and raise an error to indicate that the current operation should be
358+
* retried after the lock is released by the process that currently holds it.
359+
*/
360360
console.debug('Lock acquisition failed, raise to retry');
361361
throw new IdempotencyItemAlreadyExistsError(
362362
'Lock acquisition failed, raise to retry'

0 commit comments

Comments
 (0)