Skip to content

Commit 4f809bf

Browse files
Add condition to check if the existent item is older than expected.
1 parent f38410d commit 4f809bf

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

src/providers/dynamoDB.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ export class DynamoDB implements Provider {
3939
public async isProcessing(messageId: string): Promise<boolean> {
4040
const creationTime = Math.floor(Date.now() / 1000);
4141
const ttl = creationTime + this.ttl;
42+
const validTime = creationTime - this.ttl;
4243

4344
try {
4445
await this.ddb
4546
.put({
46-
ConditionExpression: "attribute_not_exists(messageId)",
47+
ConditionExpression:
48+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
49+
ExpressionAttributeNames: {
50+
"#ttl": "ttl",
51+
},
52+
ExpressionAttributeValues: {
53+
":validTime": validTime,
54+
},
4755
Item: { messageId: messageId, ttl },
4856
TableName: this.tableName,
4957
})

test/idempotency-http-wrapper.spec.ts

+54-6
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ describe("idempotency-http-wrapper", () => {
4040
},
4141
});
4242
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
43+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
4344
nock(endpoint)
4445
.post("/", {
45-
ConditionExpression: "attribute_not_exists(messageId)",
46+
ConditionExpression:
47+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
48+
ExpressionAttributeNames: {
49+
"#ttl": "ttl",
50+
},
51+
ExpressionAttributeValues: {
52+
":validTime": { N: expectedValidTime },
53+
},
4654
Item: { messageId: { S: requestId }, ttl: { N: expectedTtl } },
4755
TableName: tableName,
4856
})
@@ -92,9 +100,17 @@ describe("idempotency-http-wrapper", () => {
92100
},
93101
});
94102
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
103+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
95104
nock(endpoint)
96105
.post("/", {
97-
ConditionExpression: "attribute_not_exists(messageId)",
106+
ConditionExpression:
107+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
108+
ExpressionAttributeNames: {
109+
"#ttl": "ttl",
110+
},
111+
ExpressionAttributeValues: {
112+
":validTime": { N: expectedValidTime },
113+
},
98114
Item: { messageId: { S: requestId }, ttl: { N: expectedTtl } },
99115
TableName: tableName,
100116
})
@@ -173,9 +189,17 @@ describe("idempotency-http-wrapper", () => {
173189
},
174190
});
175191
const expectedTtl = String(Math.floor(mockNow / 1000) + defaultTTL);
192+
const expectedValidTime = String(Math.floor(mockNow / 1000) - defaultTTL);
176193
nock(endpoint)
177194
.post("/", {
178-
ConditionExpression: "attribute_not_exists(messageId)",
195+
ConditionExpression:
196+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
197+
ExpressionAttributeNames: {
198+
"#ttl": "ttl",
199+
},
200+
ExpressionAttributeValues: {
201+
":validTime": { N: expectedValidTime },
202+
},
179203
Item: { messageId: { S: requestId }, ttl: { N: expectedTtl } },
180204
TableName: tableName,
181205
})
@@ -223,9 +247,17 @@ describe("idempotency-http-wrapper", () => {
223247
},
224248
});
225249
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
250+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
226251
nock(endpoint)
227252
.post("/", {
228-
ConditionExpression: "attribute_not_exists(messageId)",
253+
ConditionExpression:
254+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
255+
ExpressionAttributeNames: {
256+
"#ttl": "ttl",
257+
},
258+
ExpressionAttributeValues: {
259+
":validTime": { N: expectedValidTime },
260+
},
229261
Item: { messageId: { S: requestId }, ttl: { N: expectedTtl } },
230262
TableName: tableName,
231263
})
@@ -269,9 +301,17 @@ describe("idempotency-http-wrapper", () => {
269301
},
270302
});
271303
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
304+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
272305
nock(endpoint)
273306
.post("/", {
274-
ConditionExpression: "attribute_not_exists(messageId)",
307+
ConditionExpression:
308+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
309+
ExpressionAttributeNames: {
310+
"#ttl": "ttl",
311+
},
312+
ExpressionAttributeValues: {
313+
":validTime": { N: expectedValidTime },
314+
},
275315
Item: { messageId: { S: requestId }, ttl: { N: expectedTtl } },
276316
TableName: tableName,
277317
})
@@ -316,9 +356,17 @@ describe("idempotency-http-wrapper", () => {
316356
},
317357
});
318358
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
359+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
319360
nock(endpoint)
320361
.post("/", {
321-
ConditionExpression: "attribute_not_exists(messageId)",
362+
ConditionExpression:
363+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
364+
ExpressionAttributeNames: {
365+
"#ttl": "ttl",
366+
},
367+
ExpressionAttributeValues: {
368+
":validTime": { N: expectedValidTime },
369+
},
322370
Item: { messageId: { S: requestId }, ttl: { N: expectedTtl } },
323371
TableName: tableName,
324372
})

test/idempotency-sqs-wrapper.spec.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ describe("idempotency-sqs-wrapper", () => {
4141
ttl,
4242
});
4343
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
44+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
4445
nock(endpointDynamo)
4546
.post("/", {
46-
ConditionExpression: "attribute_not_exists(messageId)",
47+
ConditionExpression:
48+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
49+
ExpressionAttributeNames: {
50+
"#ttl": "ttl",
51+
},
52+
ExpressionAttributeValues: {
53+
":validTime": { N: expectedValidTime },
54+
},
4755
Item: { messageId: { S: messageId }, ttl: { N: expectedTtl } },
4856
TableName: tableName,
4957
})
@@ -82,9 +90,17 @@ describe("idempotency-sqs-wrapper", () => {
8290
ttl,
8391
});
8492
const expectedTtl = String(Math.floor(mockNow / 1000) + ttl);
93+
const expectedValidTime = String(Math.floor(mockNow / 1000) - ttl);
8594
nock(endpointDynamo)
8695
.post("/", {
87-
ConditionExpression: "attribute_not_exists(messageId)",
96+
ConditionExpression:
97+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
98+
ExpressionAttributeNames: {
99+
"#ttl": "ttl",
100+
},
101+
ExpressionAttributeValues: {
102+
":validTime": { N: expectedValidTime },
103+
},
88104
Item: { messageId: { S: messageId }, ttl: { N: expectedTtl } },
89105
TableName: tableName,
90106
})
@@ -125,9 +141,17 @@ describe("idempotency-sqs-wrapper", () => {
125141
},
126142
});
127143
const expectedTtl = String(Math.floor(mockNow / 1000) + defaultTTL);
144+
const expectedValidTime = String(Math.floor(mockNow / 1000) - defaultTTL);
128145
nock(endpointDynamo)
129146
.post("/", {
130-
ConditionExpression: "attribute_not_exists(messageId)",
147+
ConditionExpression:
148+
"attribute_not_exists(messageId) or (#ttl < :validTime)",
149+
ExpressionAttributeNames: {
150+
"#ttl": "ttl",
151+
},
152+
ExpressionAttributeValues: {
153+
":validTime": { N: expectedValidTime },
154+
},
131155
Item: { messageId: { S: messageId }, ttl: { N: expectedTtl } },
132156
TableName: tableName,
133157
})

0 commit comments

Comments
 (0)