Skip to content

Commit

Permalink
cleanup expired entries when adding new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello committed Mar 26, 2024
1 parent d0bd828 commit cc8d619
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ export class OverflowManager {
// The zset value is a timestamp in seconds.
const expiration = (now ?? Date.now()) / 1000 + this.cooldownSeconds
await this.redisClient.zadd(this.redisKey, 'NX', expiration, key)

// Cleanup old entries with values expired more than one hour ago.
// We run the cleanup here because we assume this will only run a dozen times per day per region.
// If this code path becomes too hot, it should move to a singleton loop.
const expired = (now ?? Date.now()) / 1000 - 3600
await this.redisClient.zremrangebyscore(this.redisKey, 0, expired)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,22 @@ describe('ingester', () => {
await ingestBurst(10, 150_000, 150_000)
expect(await redisConn.exists(CAPTURE_OVERFLOW_REDIS_KEY)).toEqual(0)
})

it('should cleanup older entries when triggering', async () => {
await redisConn.zadd(CAPTURE_OVERFLOW_REDIS_KEY, 'NX', Date.now() / 1000 - 7000, 'expired:session')
await redisConn.zadd(CAPTURE_OVERFLOW_REDIS_KEY, 'NX', Date.now() / 1000 - 1000, 'not_expired:session')
expect(await redisConn.zrange(CAPTURE_OVERFLOW_REDIS_KEY, 0, -1)).toEqual([
'expired:session',
'not_expired:session',
])

await ingestBurst(10, 150_000, 10)
expect(await redisConn.exists(CAPTURE_OVERFLOW_REDIS_KEY)).toEqual(1)
expect(await redisConn.zrange(CAPTURE_OVERFLOW_REDIS_KEY, 0, -1)).toEqual([
'not_expired:session',
`${team.id}:sid1`,
])
})
})

describe('lag reporting', () => {
Expand Down

0 comments on commit cc8d619

Please sign in to comment.