Skip to content

Commit

Permalink
make watching more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Nov 17, 2023
1 parent 5dcef91 commit 56c0c77
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 26 deletions.
64 changes: 56 additions & 8 deletions pnpm-lock.yaml

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

38 changes: 21 additions & 17 deletions websocketfs/lib/metadata-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import debug from "debug";
import { stat, readFile } from "fs/promises";
import binarySearch from "binarysearch";
import { symbolicToMode, readFileLz4 } from "./util";
import { watch } from "chokidar";
import { delay } from "awaiting";

const log = debug("websocketfs:metadata-file");
const log_cache = debug("cache");

const METADATA_FILE_INTERVAL_MS = 3000;

export class MetadataFile {
private attrCache;
private dirCache;
private metadataFile: string;
private metadataFileContents: string[];
private metadataFileInterval?: ReturnType<typeof setInterval>;
private watcher?;
private lastMtimeMs: number = 0;
private state: "init" | "ready" | "expired" | "closed" = "init";

Expand All @@ -28,14 +28,19 @@ export class MetadataFile {
return this.state == "ready";
};

private init = () => {
if (this.metadataFileInterval) {
throw Error("bug -- do not call init more than once");
private init = async () => {
// We keep trying util we succesfully
// read the file once.
while (!(await this.update())) {
await delay(3000);
}
this.metadataFileInterval = setInterval(
this.update,
METADATA_FILE_INTERVAL_MS,
);
// Only after reading the file, do we setup the watcher.
// Why? Because we might want to put the file itself inside
// of the websocketfs filesystem, and if we try to watch too
// soon then it doesn't work. Subtle.
this.watcher = watch(this.metadataFile);
this.watcher.on("change", this.update);
this.watcher.on("add", this.update);
this.state = "expired";
this.update();
};
Expand All @@ -45,9 +50,9 @@ export class MetadataFile {
try {
const { mtimeMs } = await stat(this.metadataFile);
if (mtimeMs <= this.lastMtimeMs) {
log("metadataFile:", this.metadataFile, "watching for changes");
log("metadataFile:", this.metadataFile, "- no change");
// it hasn't changed so nothing to do
return;
return false;
}
const start = Date.now();
this.lastMtimeMs = mtimeMs;
Expand All @@ -69,25 +74,24 @@ export class MetadataFile {
Date.now() - start,
"ms",
);
return true;
} catch (err) {
log(
"metadataFile:",
this.metadataFile,
" - non-fatal issue reading - ",
err.code == "ENOENT"
? `no file '${this.metadataFile}' -- NOT updating metadata file cache (will try again soon)`
? `no file '${this.metadataFile}' -- NOT updating`
: err,
);
return false;
}
};

close = () => {
this.state = "closed";
this.metadataFileContents = [];
if (this.metadataFileInterval) {
clearInterval(this.metadataFileInterval);
delete this.metadataFileInterval;
}
this.watcher?.close();
};

private find = (path: string): number => {
Expand Down
3 changes: 2 additions & 1 deletion websocketfs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websocketfs",
"version": "0.13.1",
"version": "0.14.1",
"description": "Like sshfs, but over a WebSocket",
"main": "./dist/lib/index.js",
"scripts": {
Expand Down Expand Up @@ -45,6 +45,7 @@
"@isaacs/ttlcache": "^1.4.1",
"awaiting": "^3.0.0",
"binarysearch": "^1.0.1",
"chokidar": "^3.5.3",
"cookie": "^0.5.0",
"debug": "^4.3.4",
"lz4": "^0.6.5",
Expand Down

0 comments on commit 56c0c77

Please sign in to comment.