Skip to content

Commit

Permalink
feat: add locking expires check
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed Sep 7, 2024
1 parent b28e09c commit 5a703da
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/api/src/lib/locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { deleteItem, put } from "./database";
export const acquireLock = async (key, ttl = 900) => {
const lockKey = `lock#${key}`;
const expires = Math.floor(Date.now() / 1000) + ttl;

try {
await put(
{
Expand All @@ -12,7 +13,12 @@ export const acquireLock = async (key, ttl = 900) => {
},
true,
{
ConditionExpression: "attribute_not_exists(pk)",
ConditionExpression: "attribute_not_exists(pk) OR _expires < :now",
AttributeValues: {
":now": {
N: `${Math.floor(Date.now() / 1000)}`,
},
},
},
);
return true;
Expand Down

0 comments on commit 5a703da

Please sign in to comment.