首页 > 解决方案 > 从谷歌云存储下载文件导致nodejs内存泄漏

问题描述

我正在从我的 nodejs 服务上的谷歌存储下载,我有一个如下所示的实用程序模块:

'use strict';

const BPromise = require('bluebird');
const logger = require('../../setup/logger');

module.exports = () => {

    let GCloudStorage = {};

    GCloudStorage.getStorage = () => {
        return require('@google-cloud/storage')();
    };

    /**
     * @param bucketName
     * @param filePath
     * @param destination the local disk destination
     */
    GCloudStorage.downloadFile = (bucketName, filePath, destination) => {

        return new BPromise((resolve, reject) => {
            const storage = GCloudStorage.getStorage();

            logger.info('Downloading file %s in bucket %s to local path: %s', filePath, bucketName, destination);

            const file = storage.bucket(bucketName).file(filePath);
            const config = {
                destination: destination,
                validation: false
            };

            file.download(config)
              .then(() => {
                logger.debug('Downloaded gs://%s/%s to %s', bucketName, filePath, destination);
                resolve(destination);
              })
              .catch((err) => {
                reject(err);
              });
        });
    };

    return GCloudStorage;
};

然后在我的业务代码上,我像这样使用它:

let promises = [];
images.map(image => {
    promises.push(storageUtil.downloadFile(config.userFolder, image.image, path.resolve(`./reports/${reportId}/${image.name.replace(/[^a-zA-Z0-9-_\.]/g, '')}`)));
});
return BPromise.all(promises);

当此进程运行时,服务会增加其内存使用量,并且不会在其生命周期内收集它。

我知道这可能是我的代码的 nodejs 问题,但有什么我可以做的吗?

标签: javascriptnode.jsmemory-leaksgoogle-cloud-storage

解决方案


推荐阅读