also looking at this
fix: hydrate durable registry snapshots before serving writes
#244
11 files
+202
-35
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import type { S3Client } from './aws-client' |
| 2 | import { isS3ObjectNotFound } from './aws-client' | |
| 2 | 3 | import { InMemoryAnalytics } from '../analytics' |
| 3 | 4 | |
| 4 | 5 | /** |
| @@ -16,6 +17,7 @@ import { InMemoryAnalytics } from '../analytics' | ||
| 16 | 17 | * are debounced (counters accumulate in memory between flushes). |
| 17 | 18 | */ |
| 18 | 19 | export class ObjectAnalytics extends InMemoryAnalytics { |
| 20 | private hydrated = false | |
| 19 | 21 | private s3: S3Client |
| 20 | 22 | private bucket: string |
| 21 | 23 | private key: string |
| @@ -39,16 +41,12 @@ export class ObjectAnalytics extends InMemoryAnalytics { | ||
| 39 | 41 | private async load(): Promise<void> { |
| 40 | 42 | try { |
| 41 | 43 | const buf = await this.s3.getObjectBuffer(this.bucket, this.key) |
| 42 | if (!buf || buf.byteLength === 0) | |
| 43 | return | |
| 44 | 44 | this.applyAnalyticsState(JSON.parse(buf.toString('utf-8'))) |
| 45 | this.hydrated = true | |
| 45 | 46 | } |
| 46 | 47 | catch (err) { |
| 47 | const msg = (err as Error).message || '' | |
| 48 | // A missing object on first run is expected (404/403); start empty otherwise. | |
| 49 | if (!/\b40[34]\b/.test(msg)) { | |
| 50 | console.error(`ObjectAnalytics: failed to load ${this.bucket}/${this.key}: ${msg}. Starting empty.`) | |
| 51 | } | |
| 48 | if (!isS3ObjectNotFound(err)) throw err | |
| 49 | this.hydrated = true | |
| 52 | 50 | } |
| 53 | 51 | } |
| 54 | 52 | |
| @@ -66,6 +64,8 @@ export class ObjectAnalytics extends InMemoryAnalytics { | ||
| 66 | 64 | } |
| 67 | 65 | |
| 68 | 66 | private async save(): Promise<void> { |
| 67 | if (!this.hydrated) | |
| 68 | throw new Error(`Snapshot not hydrated: ${this.key}`) | |
| 69 | 69 | while (this.savePromise) await this.savePromise |
| 70 | 70 | this.savePromise = this.doSave() |
| 71 | 71 | try { |
| @@ -88,6 +88,7 @@ export class ObjectAnalytics extends InMemoryAnalytics { | ||
| 88 | 88 | |
| 89 | 89 | /** Flush any pending save immediately (e.g. before shutdown). */ |
| 90 | 90 | async flush(): Promise<void> { |
| 91 | await this.loaded | |
| 91 | 92 | if (this.saveTimeout) { |
| 92 | 93 | clearTimeout(this.saveTimeout) |
| 93 | 94 | this.saveTimeout = null |