如何确保在ElasticSearch Bonsai免费实例中使用Firebase FlashLight索引每个对象

Ant*_*ony 7 concurrency node.js elasticsearch firebase

感谢FlashLight https://github.com/firebase/flashlight的教程,这对使用Firebase进行全文搜索很容易.

但是,如果您保留免费的ES实例,则它在并发访问方面受到限制,并且当您启动节点应用程序时,您会在日志中看到以下消息:

无法索引firebase/xxx/-KHLhdwGplb3lHWjm8RS:错误:超出了并发请求限制.请考虑批量处理您的请求,或联系support@bonsai.io寻求帮助.

怎么解决这个?

Ant*_*ony 11

如果您有一堆要索引的数据,Flashlight应用程序将要求ES动态索引每个对象,而不会有任何资源访问限制.您必须使用信号量控制/限制对此共享资源的访问.

安装Semaphore lib

npm i --save semaphore
Run Code Online (Sandbox Code Playgroud)

编辑该PathMonitor.js文件,并将对ES资源的访问权限限制为1

PathMonitor.prototype = {
    _init: function () {
        this.sem = require('semaphore')(1);
        this.addMonitor = this.ref.on('child_added', this._process.bind(this, this._childAdded));
        this.changeMonitor = this.ref.on('child_changed', this._process.bind(this, this._childChanged));
        this.removeMonitor = this.ref.on('child_removed', this._process.bind(this, this._childRemoved));
    },
    ...
    _index: function (key, data, callback) {
        var that = this;
        that.sem.take(function () {
            that.esc.index({
                index: that.index,
                type : that.type,
                id   : key,
                body : data
            }, function (error, response) {
                that.sem.leave();
                if (callback) {
                    callback(error, response);
                }
            }.bind(that));
        });
    },
    ...
}
Run Code Online (Sandbox Code Playgroud)

付费计划可能不需要这样做.