Node-mysqlでの接続プールコード学習

8896 ワード

node-mysqlはnodeです.js下のmysql駆動は,この間接続プールの問題を処理する上で接続が解放されない難病に遭遇し,解決したが,次回の重複を避けるための経験をまとめる必要がある.次はnode-mysqlの接続プールのコードの一部で、メモ用に詳細なログを追加しました.
/**

 *        Connection

 * @param cb

 * @returns {*}

 */

Pool.prototype.getConnection = function (cb) {

    //      

    console.log("getConnection _allConnections.length: %j, _freeConnections.length: %j", this._allConnections.length, this._freeConnections.length);



    //     

    if (this._closed) {

        return process.nextTick(function () {

            return cb(new Error('Pool is closed.'));

        });

    }



    var connection;

    //    Connection,  0          

    if (this._freeConnections.length > 0) {

        connection = this._freeConnections.shift();

        return process.nextTick(function () {

            return cb(null, connection);

        });

    }



    //           (  10),       (   CreateConnection)

    if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {

        connection = new PoolConnection(this, { config: this.config.connectionConfig });



        //          

        this._allConnections.push(connection);



        //      Connection    

        return connection.connect(function (err) {

            if (this._closed) {

                return cb(new Error('Pool is closed.'));

            }

            if (err) {

                return cb(err);

            }



            this.emit('connection', connection);

            return cb(null, connection);

        }.bind(this));

    }



    //          (  True),False     

    if (!this.config.waitForConnections) {

        return process.nextTick(function () {

            return cb(new Error('No connections available.'));

        });

    }



    //            (  0,  )

    if (this.config.queueLimit && this._connectionQueue.length >= this.config.queueLimit) {

        return cb(new Error('Queue limit reached.'));

    }



    //    

    this._connectionQueue.push(cb);

};



/**

 *   Connection

 * @param connection

 */

Pool.prototype.releaseConnection = function (connection) {

    var cb;

    //        

    if (!connection._pool) {

        //      

        if (this._connectionQueue.length) {

            //        

            cb = this._connectionQueue.shift();

            //  getConnection  Connection      

            process.nextTick(this.getConnection.bind(this, cb));

        }

    }

    /**

     *            

     */

    else if (this._connectionQueue.length) {

        cb = this._connectionQueue.shift();

        //    connection               

        process.nextTick(cb.bind(null, null, connection));

    }

    //            

    else {

        //    connection        ,   

        this._freeConnections.push(connection);

        //       

        console.log("releaseConnection _allConnections.length: %j, _freeConnections.length: %j", this._allConnections.length, this._freeConnections.length);

    }

};