间歇性错误“此套接字已被另一方结束”

时间:2016-09-27 11:37:30

标签: javascript sockets neo4j

我正在连接我在docker容器上运行的neo4j,当我尝试进行另一个连接/查询时,我收到错误。错误是“此套接字已被另一方结束”。这是我的Dockerfile,其中包含已打开的端口:

EXPOSE 7474 7473 7687

这是我的驱动程序实用程序:

var neo4j = require("neo4j-driver").v1,
    config = require("../../config");

(function(module){ 
    module.exports = function(){
        config.neo4j.server = 'localhost';
        return neo4j.driver("bolt://" + config.neo4j.server, neo4j.auth.basic(config.neo4j.user, config.neo4j.password));
    }
}(module));

我对neo4j的查询方式:

(function(module){
    var driver = require('../../../utils/neo4j-driver')(),
        Q = require('q'),
        logger = require('../../../utils/logger'),
        BaseNeo4jModel = require('../../../utils/neo4j-model');

    function userDBAuth(user){
        var deferred = Q.defer();
        var session = driver.session();

        session
            //.run( "MATCH (a:Person) RETURN a.name AS name, a.uuid as uuid" )
            .run("MATCH (user:User {email:{email}})"
                    + "RETURN user",
                    { email: user.email})
            .then(function (result) {
                logger.debug('fetching results...');

                if(result.records.length > 0){
                    var records = [];

                    result.records.forEach(function(record){
                        records.push(new BaseNeo4jModel(record));
                    });

                    done();

                    deferred.resolve(records);
                } else {
                    deferred.reject({sucess: false, message: 'User not Found'});
                }

            }).catch( function(err) {
                logger.error(err);
                done();
                deferred.reject(err.fields[0]);
            });

            return deferred.promise;

        function done(){
            session.close();
            driver.close();
        }
    }

    module.exports = userDBAuth;

}(module));

这是我的堆栈跟踪:

{"name":"auth-services","hostname":"MacBook-Pro-de-Vinicius.local","pid":16292,"level":50,"err":{"message":"This socket has been ended by the other party","name":"Error","stack":"Error: This socket has been ended by the other party
at TLSSocket.writeAfterFIN [as write] (net.js:286:12)
at NodeChannel.write (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/node_modules/neo4j-driver/lib/v1/internal/ch-node.js:285:20)
at Chunker.flush (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/node_modules/neo4j-driver/lib/v1/internal/chunking.js:114:18)
at Connection.sync (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/node_modules/neo4j-driver/lib/v1/internal/connector.js:487:21)
at Session.run (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/node_modules/neo4j-driver/lib/v1/session.js:89:20)
at userDBAuth (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/app/api/auth/services/userDBAuth.js:13:7)
at Object._auth [as auth] (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/app/api/auth/services/auth.js:9:3)
at Server.auth (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/app/api/auth/resources/auth.js:7:12)
at next (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/node_modules/restify/lib/server.js:906:30)
at f (/Users/viniciussouza/Sites/WeddSocial/backend/weddsocial-auth/node_modules/once/once.js:25:25)","code":"EPIPE"},"msg":"This socket has been ended by the other party","time":"2016-09-27T11:12:33.163Z","v":0}

我很绝望,请帮助我!

2 个答案:

答案 0 :(得分:1)

只是一个建议 - 但看起来您的成功路径并不总是调用done()方法,因此会话不会一直关闭并返回到池中。

我建议你修改以下内容:

            if(result.records.length > 0){
                var records = [];

                result.records.forEach(function(record){
                    records.push(new BaseNeo4jModel(record));
                });

                done();

                deferred.resolve(records);
            } else {
                deferred.reject({sucess: false, message: 'User not Found'});
            }

在else块中添加done()调用:

            if(result.records.length > 0){
                var records = [];

                result.records.forEach(function(record){
                    records.push(new BaseNeo4jModel(record));
                });

                done();

                deferred.resolve(records);
            } else {
                done();
                deferred.reject({sucess: false, message: 'User not Found'});
            }

答案 1 :(得分:0)

我们收到了相同的错误,并将其追溯到我们的代码,我们有时在事务中的所有查询完成之前提交了我们的事务。

看看你的代码,我建议像:

(function (module) {
    var driver = require('../../../utils/neo4j-driver')(),
        Q = require('q'),
        logger = require('../../../utils/logger'),
        BaseNeo4jModel = require('../../../utils/neo4j-model');

    function userDBAuth(user) {
        var session = driver.session();

        return session
            .run("MATCH (user:User {email:{email}}) RETURN user", {email: user.email})
            .then(function (result) {
                logger.debug('fetching results...');

                if (result.records.length === 0) {
                    return Q.reject({success: false, message: 'User not Found'}); // will be caught by the .catch() below
                }
                var records = result.records.map(function(record) {
                        return new BaseNeo4jModel(record); // assume this is synchronous and doesn't return a promise
                    });
                done();
                return records;
            })
            .catch(function (err) {
                logger.error(err);
                done();
                Q.reject(err.fields[0]); // return a rejected promise that can be caught by the caller of userDBAuth
            });

        function done() {
            session.close();
            driver.close();
        }
    }

    module.exports = userDBAuth;

}(module));

不确定这会解决您的问题,因为您没有完全按照我们的目标行事,只是基于我们发现的建议。