Firebase Storage & Cloud Functions - ECONNRESET

I developed the Cloud Firebase feature, which handles several manipulations of uploaded images. My code is based on this documentation article and this cloud function example . Therefore, it uses the Google Cloud Storage package .

It works fine almost all the time, but sometimes I get this error when loading or deleting from the repository:

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

I use the default cart for my referenced application event.data.bucket.

Let me know if you need more information or code snippets, even if my code is really close to the function example that I linked earlier.

I found this GitHub problem , but I checked that I return a promise every day. For example, here is the uninstall part that causes the error:

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);
        });
    }
    ...
+2
source share
1 answer

This is apparently due to the processing of the google-cloud-nodesocket library and the default socket timeout in the cloud function environment.

One solution verified by the user is to change the way the library calls requestsso that it does not leave the socket open forever by specifying forever: false, for example.

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

packages/common/src/utils.js, , NPM. . .

+2

Source: https://habr.com/ru/post/1692115/


All Articles