Skip to content

Commit cadcfca

Browse files
authored
Merge pull request #12 from fastly/kailan-prevent-auto-buildup
Expire automatic entry counter at end of entry period
2 parents e6eccf9 + 599beaf commit cadcfca

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/store.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ export async function incrementQueueLength(store, amt) {
3636
//
3737
// Returns the new counter value.
3838
export async function incrementAutoPeriod(store, config) {
39-
let period = Math.ceil(
39+
const period = Math.ceil(
4040
new Date().getTime() / (config.queue.automatic * 1000)
4141
);
4242

43-
return await store.incr(`${AUTO_KEY_PREFIX}:${period}`);
43+
const key = `${AUTO_KEY_PREFIX}:${period}`;
44+
const value = await store.incr(key);
45+
46+
// If this is the first call, set the key to expire at the end of the period.
47+
if (value === 1) await store.expire(key, config.queue.automatic);
48+
49+
return value;
4450
}
4551

4652
// Helper function for configuring a Redis client.

0 commit comments

Comments
 (0)