Skip to content

Commit

Permalink
feat(shim): add nginx cache purger on pre-registration (#469)
Browse files Browse the repository at this point in the history
* feat(shim): add nginx cache purger on pre-registration

* Don't fail on unlink failures

* Update deps
  • Loading branch information
Diego Rodríguez Baquero authored Aug 14, 2023
1 parent d121b8e commit de1e497
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
14 changes: 7 additions & 7 deletions container/shim/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion container/shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"express-async-handler": "^1.2.0",
"fast-glob": "^3.3.1",
"logfmt": "^1.3.2",
"lru-cache": "^10.0.0",
"lru-cache": "^10.0.1",
"mime-types": "^2.1.35",
"multiformats": "^12.0.1",
"node-fetch": "^3.3.2",
Expand Down
18 changes: 18 additions & 0 deletions container/shim/src/modules/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { parseVersionNumber } from "../utils/version.js";
import { orchestratorAgent } from "../utils/http.js";
import { prefillCache } from "../utils/prefill.js";
import { check } from "../lib/ocsp/check.js";
import { purgeCacheFile } from "../utils/purger.js";

const debug = Debug.extend("registration");

Expand Down Expand Up @@ -50,6 +51,13 @@ export async function register(initial = false) {
preregisterResponse = await sendPreRegisterRequest(postOptions({ nodeId: NODE_ID }));
}

if (preregisterResponse.filesToPurge) {
debug("Purging %d files", preregisterResponse.filesToPurge.length);
for (const file of preregisterResponse.filesToPurge) {
await purgeCacheFile(file);
}
}

if (VERSION !== DEV_VERSION && initial && preregisterResponse?.speedtestRequired !== false) {
let speedtest;
try {
Expand Down Expand Up @@ -247,6 +255,16 @@ async function sendRegisterRequest(initial, registerOptions) {
}
}

/**
* Sends a pre-registration request to the orchestrator.
*
* @typedef {object} PreRegisterResponse
* @property {boolean} speedtestRequired
* @property {string[]} filesToPurge
*
* @param {object} postOptions
* @returns {Promise<PreRegisterResponse>}
*/
async function sendPreRegisterRequest(postOptions) {
debug("Pre-registering with orchestrator");

Expand Down
20 changes: 20 additions & 0 deletions container/shim/src/utils/purger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from "node:fs/promises";
import { createHash } from "node:crypto";
import path from "node:path";
import { debug } from "./logging.js";

const CACHE_DIR = "/usr/src/app/shared/nginx_cache";

export async function purgeCacheFile(key) {
debug("Purging cache file for key %s", key);
const cacheFilePath = keyToCacheFilePath(key);
await fs
.unlink(path.join(CACHE_DIR, cacheFilePath))
.catch((err) => debug("Failed to purge cache file: %s", err.message));
}

function keyToCacheFilePath(path) {
const hash = createHash("md5").update(path).digest("hex");

return `${hash.slice(-2)}/${hash.slice(-4, -2)}/${hash.slice(-6, -4)}/${hash}`;
}

0 comments on commit de1e497

Please sign in to comment.