开发者

Node.js error: EADDRINUSE, Address already in use

开发者 https://www.devze.com 2023-03-21 20:27 出处:网络
For testing purposes, I\'ve set up this little script: Client = require(\'mysql\').Client, client = new Client();

For testing purposes, I've set up this little script:

Client = require('mysql').Client, client = new Client(); 
client.user = 'root'; client.password = 'root'; client.port = 8889;

counter = 0; data = '';
setInterval(update, 10);

function update(){
    counter++; request();
}
var sys = require('sys'), fs = require('fs'), http = require('http'),
    url = require('url'),port = 8001;

http.createServer(function(request, response) {
    response.writeHead(200, {
        'Content-Type': 'text/html'
    }); response.end('Hello world');
}).listen(port);

function request(){
    client.connect();
    client.query('USE db');
    client.query(
    'SELECT * FROM table', function selectCb(err, results, fields) {
        if (err) { throw err; }
        if(results.length > 0)
        {
            var firstResult = results[0];
            data = firstResult['data'];
        }
        client.end();
    }
    );
}

It works fine, but, after an hour or so, the server crashes and outputs the following error:

node.js:180
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EADDRINUSE, Address already in use

UPDATE

I updated the code. It now includes some mysql. This definitely is causing the problem, although I don't know exactly why. Leaving this part out stabilizes the loop. Also, I updated the time interval to every 10 ms. (The server crashes after 开发者_如何学Pythona few minutes with the mysql part in it, but keeps on running without)


You really should honor pimvb's comment about not connecting 100 times a second to the database. If node-mysql doesn't clean up the connections correctly or immediately this will most likely have you run out of available ports for connections within about an hour (actually more like 10 minutes or so, use netstat | grep WAIT | wc -l to see how many closed connections you have pending).

Also, you should be aware of the asynchronous nature of JavaScript/node.js. You might not want to run new queries in such a tight loop while other queries are still pending. This will only pile up and the SQL server will choke.

UPDATE

Something like the following should prevent you from choking your server. It will issue as many request as possible but no more than 1 every 10 ms. Disclaimer: hacked in so textbox, not tested!

Client = require('mysql').Client, client = new Client(); 
client.user = 'root'; client.password = 'root'; client.port = 8889;

counter = 0; data = '';

client.connect(function {
    setInterval(update, 10);

    var queryPending = false, queryReIssue = false;

    function update(){
        if (queryPending) {
            queryReIssue = true;
        } else {
            request();
        }
        counter++;
    }
    var sys = require('sys'), fs = require('fs'), http = require('http'),
        url = require('url'),port = 8001;

    http.createServer(function(request, response) {
        response.writeHead(200, {
            'Content-Type': 'text/html'
        }); response.end('Hello world');
    }).listen(port);

    function request(){
        requestPending = true;
        client.query('USE db');
        client.query('SELECT * FROM table', function selectCb(err, results, fields) {
            requestPending = false;
            if (err) { console.log('ERROR: ' + err.message) }
            if(results.length > 0) {
                var firstResult = results[0];
                data = firstResult['data'];
            }
            if (queryReIssue) {
                queryReIssue = false;
                request();
            }
        });
    }
});


You are connecting every 10ms, which puts a heavy load on your MySQL server. I do not know how it's possible that you get a 'address in use' error, but you might want to try connecting once and use the connection to execute queries.

Just like you don't log out and log back in again and again at a messenger application for each message you want to send to someone :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号