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 { InMemoryMetadataStorage } from './metadata' |
| 3 | 4 | |
| 4 | 5 | /** |
| @@ -15,6 +16,7 @@ import { InMemoryMetadataStorage } from './metadata' | ||
| 15 | 16 | * snapshot from the bucket. |
| 16 | 17 | */ |
| 17 | 18 | export class ObjectMetadataStorage extends InMemoryMetadataStorage { |
| 19 | private hydrated = false | |
| 18 | 20 | private s3: S3Client |
| 19 | 21 | private bucket: string |
| 20 | 22 | private key: string |
| @@ -38,19 +40,13 @@ export class ObjectMetadataStorage extends InMemoryMetadataStorage { | ||
| 38 | 40 | private async load(): Promise<void> { |
| 39 | 41 | try { |
| 40 | 42 | const buf = await this.s3.getObjectBuffer(this.bucket, this.key) |
| 41 | if (!buf || buf.byteLength === 0) | |
| 42 | return | |
| 43 | 43 | const data = JSON.parse(buf.toString('utf-8')) |
| 44 | 44 | this.applyState(data) |
| 45 | this.hydrated = true | |
| 45 | 46 | } |
| 46 | 47 | catch (err) { |
| 47 | // A missing object on first run is expected (404/403). Anything else we | |
| 48 | // log loudly but still start empty rather than block the server boot — | |
| 49 | // and we do NOT overwrite the remote object until the next real save. | |
| 50 | const msg = (err as Error).message || '' | |
| 51 | if (!/\b40[34]\b/.test(msg)) { | |
| 52 | console.error(`ObjectMetadataStorage: failed to load ${this.bucket}/${this.key}: ${msg}. Starting empty.`) | |
| 53 | } | |
| 48 | if (!isS3ObjectNotFound(err)) throw err | |
| 49 | this.hydrated = true | |
| 54 | 50 | } |
| 55 | 51 | } |
| 56 | 52 | |
| @@ -65,6 +61,8 @@ export class ObjectMetadataStorage extends InMemoryMetadataStorage { | ||
| 65 | 61 | } |
| 66 | 62 | |
| 67 | 63 | private async save(): Promise<void> { |
| 64 | if (!this.hydrated) | |
| 65 | throw new Error(`Snapshot not hydrated: ${this.key}`) | |
| 68 | 66 | // Serialize saves so a slow upload can't be overtaken by a newer one. |
| 69 | 67 | while (this.savePromise) await this.savePromise |
| 70 | 68 | this.savePromise = this._doSave() |
| @@ -88,6 +86,7 @@ export class ObjectMetadataStorage extends InMemoryMetadataStorage { | ||
| 88 | 86 | |
| 89 | 87 | /** Flush any pending save immediately (e.g. before shutdown). */ |
| 90 | 88 | async flush(): Promise<void> { |
| 89 | await this.loaded | |
| 91 | 90 | if (this.saveTimeout) { |
| 92 | 91 | clearTimeout(this.saveTimeout) |
| 93 | 92 | this.saveTimeout = null |