Skip to content

Investigate FileSystem access in src/server.js #53

@pdehaan

Description

@pdehaan

Randomly trolling through the code, and trying to understand the couple of fs.* calls in /src/server.js:

function respond(req, res, next) {
const dataset = req.params.dataset;
const manifestFilename = `manifests/${dataset}.json`;
if (!fs.existsSync(manifestFilename)) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Not found');
next();
return;
}

If I'm reading this correctly, ever call to server.get('/:dataset', ...) will call fs.existsSync(manifestFilename).

server.get('/:dataset', respond);

If the manifest for the dataset is found, we check Redis for a cache hit. If we have a cache hit, we return the cached data and everything is 👌.
If we have a cache miss, we call sendTransposeOutput(), which will call fs.readFile():

function sendTransposeOutput(res, dataset, manifestFilename) {
fs.readFile(manifestFilename, 'utf8', (error, contents) => {
if (error) {
res.send({
error: true,
});
console.error(error);
return;
} else {
const manifest = JSON.parse(contents);
transpose(manifest, output => {
console.log(`Setting cache for key: ${dataset}`);
redisClient.setex(dataset, cacheSeconds, JSON.stringify(output));
res.send(output);
});
}
});
}

Not sure how often we expect the manifests to change, or if we can try loading all 3 manifest JSON files at server startup and keep them cached instead of calling fs.existsSync() for each dataset request. Maybe the fs.readFile() isn't as serious if we're expecting 99.999% of the calls to be Redis cache hits and we only load the file when the cache has expired (which hopefully in production we'll have a long cache life).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions