-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Archive games after 24h in GCS and restore automatically (#172)
* Archive games after 24h in GCS and restore automatically This reduces storage costs from $5/GB/month to about $0.02/GB/month (250x) with almost no downside. It also uses gzip which provides another 3x compression ratio, reducing cost again. So what was costing $150/month previously to store 10 million games, and required us to drop the database, now would only cost $0.20/month. Need to test thoroughly in setwihfriends-dev before I can promote to production. Don't want to lose a bunch of data! * Fix lint warning
- Loading branch information
Showing
7 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { type DataSnapshot, getDatabase } from "firebase-admin/database"; | ||
import * as zlib from "node:zlib"; | ||
|
||
/** Promises API wrapper around the 'zlib' Node library. */ | ||
export const gzip = { | ||
compress( | ||
input: string | Buffer | ArrayBuffer | Uint8Array | DataView, | ||
): Promise<Buffer> { | ||
return new Promise((resolve, reject) => { | ||
zlib.gzip(input, (err, result) => { | ||
if (err) reject(err); | ||
else resolve(result); | ||
}); | ||
}); | ||
}, | ||
decompress(input: Buffer): Promise<Buffer> { | ||
return new Promise((resolve, reject) => { | ||
zlib.gunzip(input, (err, result) => { | ||
if (err) reject(err); | ||
else resolve(result); | ||
}); | ||
}); | ||
}, | ||
}; | ||
|
||
/** | ||
* Iterate through a reference in the database, returning the children ordered | ||
* by key in batches. | ||
*/ | ||
export async function* databaseIterator( | ||
path: string, | ||
batchSize = 1000, | ||
): AsyncGenerator<[string, DataSnapshot]> { | ||
let lastKey = null; | ||
while (true) { | ||
const snap = lastKey | ||
? await getDatabase() | ||
.ref(path) | ||
.orderByKey() | ||
.startAfter(lastKey) | ||
.limitToFirst(batchSize) | ||
.get() | ||
: await getDatabase() | ||
.ref(path) | ||
.orderByKey() | ||
.limitToFirst(batchSize) | ||
.get(); | ||
if (!snap.exists()) return; | ||
|
||
const childKeys: string[] = []; | ||
snap.forEach((child) => { | ||
childKeys.push(child.key); | ||
lastKey = child.key; | ||
}); | ||
for (const key of childKeys) { | ||
yield [key, snap.child(key)]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// Note: This is deprecated. | ||
import { getDatabase } from "firebase-admin/database"; | ||
|
||
const batchSize = 1000; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters