GoogleChrome / rendertron

A Headless Chrome rendering solution

Home Page:https://render-tron.appspot.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

report a bug in filesystem-cache: Not close the fd after open the cache file

yadoudou opened this issue · comments

In file: src/filesystem-cache.ts

getCachedContent(ctx: Koa.Context, key: string): CacheContent | null {
    if (ctx.query.refreshCache) {
      return null;
    } else {
      try {
        const cacheFile = JSON.parse(
          fs.readFileSync(path.join(this.getDir(''), key + '.json'), 'utf8')
        );
        const payload = cacheFile.responseBody;
        const response = JSON.stringify(cacheFile.responseHeaders);
        if (!payload) {
          return null;
        }
        const fd = fs.openSync(path.join(this.getDir(''), key + '.json'), 'r');
        const stats = fs.fstatSync(fd);
        // use modification time as the saved time
        const saved = stats.mtime;
        const expires = new Date(
          saved.getTime() +
          parseInt(this.cacheConfig.cacheDurationMinutes) * 60 * 1000
        );
        return {
          saved,
          expires,
          payload,
          response,
        };
      } catch (err) {
        return null;
      }
    }
  }

Not Close the cache file after open, and may cause too many open files error after run a long time

may modify the code like below

getCachedContent(ctx: Koa.Context, key: string): CacheContent | null {
    if (ctx.query.refreshCache) {
      return null;
    } else {
      let fd
      try {
        let cacheFileName = this.getDir(key + '.json')
        const cacheFile = JSON.parse(fs.readFileSync(cacheFileName, 'utf8'));
        const payload = cacheFile.responseBody;
        const response = JSON.stringify(cacheFile.responseHeaders);
        if (!payload) {
          Log.debug('no cache', {fileName: cacheFileName})
          return null;
        }
        fd = fs.openSync(cacheFileName, 'r');
        const stats = fs.fstatSync(fd);
        // use modification time as the saved time
        const saved = stats.mtime;
        const expires = new Date(saved.getTime() + parseInt(this.cacheConfig.cacheDurationMinutes) * 60 * 1000);
        Log.debug('from chache file', {fileName: cacheFileName})
        return {
          saved,
          expires,
          payload,
          response,
        };
      } catch (err) {
        return null;
      } finally {
        if (fd !== undefined) {
          fs.closeSync(fd)
        }
      }
    }
  }