aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

Very slow streaming from gridfs when some files does not exist #122

Open koblents opened 6 years ago

koblents commented 6 years ago

Hello!

I am using meteor-files with gridfs stream package and store files in Mongo DB. I have a page where I show a list of images. All Images I am trying to take only from gridfs.

Here is a code (taken from gridfs-stream package demo)

    interceptDownload(http, image, versionName) {
            // Serve file from GridFS
            const _id = (image.versions[versionName] || {}).gridFsFileId;
            if (_id) {
                var readStream = gfs.createReadStream({_id});
                if (readStream) {
                    readStream.pipe(http.response);
                    readStream.on('error', function (err) {
                        console.log(err);
                        return false;
                    });
                } else {
                    console.log(error);
                }
            } else {
                console.log(image._id + ' has no gridfs link');
            }
            return true;
            // return Boolean(_id); // Serve file from either GridFS or FS if it wasn't uploaded yet
        }
    },

So as you see I always return true. But now there is a case, when I have half images doesn't exist in gridfs and only few from the list exist.

In this case page become working VERY slow. I opened page and open log in separate windows. And it looks that it start to stream a new images after a few seconds.

So it looks that:

  1. It tries to get from gridfs image that not exists there and give for it a very long time
  2. And only after some time, it skip file and tries with next.

So my question - is this how it works? What is happening when I return true in method where gridfs file doesn't exist?

Thank you.