mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
fix(storage): use wall-clock TTL and half-lifetime margin for presigned URL cache (#541)
* fix(storage): use wall-clock TTL and half-lifetime margin for presigned URL cache * test(e2e): eliminate offline race in uploader resilience harness test
This commit is contained in:
+24
@@ -48,6 +48,30 @@ describe('LruTtlCache', () => {
|
||||
expect(cache.size).toBe(0)
|
||||
})
|
||||
|
||||
it('should expire items using wall-clock time even when performance.now pauses during sleep', () => {
|
||||
vi.useRealTimers()
|
||||
let wallNow = 1_700_000_000_000
|
||||
let perfNow = 1_000
|
||||
const dateNowSpy = vi.spyOn(Date, 'now').mockImplementation(() => wallNow)
|
||||
const perfNowSpy = vi.spyOn(performance, 'now').mockImplementation(() => perfNow)
|
||||
|
||||
try {
|
||||
const cache = new LruTtlCache<string, string>(10)
|
||||
cache.set('key1', 'value1', 60_000)
|
||||
expect(cache.get('key1')).toBe('value1')
|
||||
|
||||
// Simulate host sleep: wall clock advances past TTL while monotonic clock barely moves
|
||||
wallNow += 120_000
|
||||
perfNow += 500
|
||||
|
||||
expect(cache.get('key1')).toBeUndefined()
|
||||
expect(cache.size).toBe(0)
|
||||
} finally {
|
||||
dateNowSpy.mockRestore()
|
||||
perfNowSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('should proactively evict expired items on get', () => {
|
||||
const sampleSize = 5
|
||||
const cache = new LruTtlCache<string, string>(100, sampleSize)
|
||||
|
||||
+3
-2
@@ -101,10 +101,11 @@ export class LruTtlCache<TKey, TValue> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Monotonic time source to avoid issues with system clock jumps.
|
||||
* Wall-clock time source so TTLs reflect real elapsed time across host sleep / VM suspend
|
||||
* and stay aligned with wall-clock expirations such as presigned URL timestamps.
|
||||
*/
|
||||
private now(): number {
|
||||
return performance.now()
|
||||
return Date.now()
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
|
||||
Reference in New Issue
Block a user