Firebase存储&云功能 - ECONNRESET

时间:2017-04-05 07:47:42

标签: firebase google-cloud-storage firebase-storage google-cloud-functions

我开发了一个Firebase Cloud功能,可以对上传的图像进行多处理操作。 我的代码基于this documentation articlethis Cloud Function example。因此,它正在使用Google Cloud Storage package

几乎所有时间都工作正常,但有时我在上传到存储或从存储中删除时会出现此错误:

 Error: read ECONNRESET
    at exports._errnoException (util.js:1026:11)
    at TLSWrap.onread (net.js:569:26)

我正在使用我的应用程序的默认存储桶,由event.data.bucket引用。

如果您需要其他信息或代码段,请告诉我,即使我的代码非常接近我之前链接的功能示例。

我找到this GitHub issue,但我检查过我每次都会回复一个承诺。例如,以下是触发错误的删除部分:

index.js

exports.exampleFunction = functions.storage.object().onChange(event => {
    return f_thumbnails.exampleFunction(event);
});

example_function.js

module.exports = exports = function (_admin, _config) {
    admin = _admin;
    config = _config;

    return {
        "exampleFunction": function (event) {
            return exampleFunction(event);
        }
    };
};

const exampleFunction = function (event) {
    const gcsSourceFilePath = event.data.name;
    const gcsSourceFilePathSplit = gcsSourceFilePath.split('/');
    const gcsBaseFolder = gcsSourceFilePathSplit.length > 0 ? gcsSourceFilePathSplit[0] : '';
    const gcsSourceFileName = gcsSourceFilePathSplit.pop();
    const gceSourceFileDir = gcsSourceFilePathSplit.join('/') + (gcsSourceFilePathSplit.length > 0 ? '/' : '');

    // Not an image
    if (!event.data.contentType.startsWith('image/')) {
        console.log('Not an image !');
        return;
    }

    // Thumbnail
    if (gcsSourceFileName.startsWith(config.IMAGES_THUMBNAIL_PREFIX)) {
        console.log('Thumbnail !');
        return;
    }

    const bucket = gcs.bucket(event.data.bucket);
    const gcsThumbnailFilePath = gceSourceFileDir + config.IMAGES_THUMBNAIL_PREFIX + gcsSourceFileName;


    // File deletion
    if (event.data.resourceState === 'not_exists') {
        console.log('Thumbnail deletion : ' + gcsThumbnailFilePath);
        return bucket.file(gcsThumbnailFilePath).delete().then(() => {
            console.log('Deleted thumbnail ' + gcsThumbnailFilePath);
        });
    }
    ...

1 个答案:

答案 0 :(得分:3)

这似乎与google-cloud-node库对套接字的处理以及云函数环境中的默认套接字超时有关。

用户验证的一个解决方案是修改库调用requests的方式,不要通过指定forever: false来永久保持套接字打开,例如。

var request = require('request').defaults({
  timeout: 60000,
  gzip: true,
  forever: false,
  pool: {
    maxSockets: Infinity
  }
});

这是 packages/common/src/utils.js 中的硬编码,因此您需要将修改后的库的副本提供给项目,而不是将其作为NPM依赖项包含在内。有关问题的更多详细信息,请参阅related public issue,并使用patch applied查看指向分叉的链接。

相关问题