Tuesday, 2 May 2017

Node net socket.end event fires twice?

This event is firing twice. I'm trying to figure out why.

On one client, I have:

import Net from 'net';
import Chalk from 'chalk';

const fallback = [2,5,10,25,50,100,250,500,1000,2000];

class LocalNetworkInterface {

    constructor({path}) {
        this._sock = new Net.Socket();
        this._pending = {};
        this._count = 0;
        this._retry = 0;

        const connect = () => {
            this._sock.connect({path});
        };
        this._sock.on('connect',() => {
            this._retry = 0;
            console.log(`Connected to ${Chalk.underline(path)}`);
        });
        this._sock.on('data',buffer => {
            let data = JSON.parse(buffer);
            this._pending[data.queryId].resolve(data);
            delete this._pending[data.queryId];
        });
        this._sock.on('end', () => {
            console.log(`Lost connection to ${Chalk.underline(path)}. Attempting to reconnect...`);
            connect();
        });
        this._sock.on('error', err => {
            if(err.code === 'ENOENT') {
                let ms = fallback[this._retry];
                if(this._retry < fallback.length - 1) ++this._retry;
                console.log(`Socket server unavailable. Trying again in ${ms}ms`);
                setTimeout(connect, ms);
            }
        });
        connect();
    }

    // ...
}

And the server:

const sockServer = Net.createServer(c => {
    c.on('data', buffer => {
        let data = JSON.parse(buffer);
        // log('Received',data);
        let ql = queryLogger();
        runQuery(Object.assign({}, data, {schema})).then(result => {
            ql(`${Chalk.magenta('socket')} ${print(data.query).trim()}`);
            let response = Object.assign({}, result, {queryId: data.queryId});
            c.write(JSON.stringify(response));
        });
    })
});

sockServer.on('error', serverError => {
    if(serverError.code === 'EADDRINUSE') {
        let clientSocket = new Net.Socket();
        clientSocket.on('error', clientError => {
            if(clientError.code === 'ECONNREFUSED') {
                FileSystem.unlink(SOCK_FILE, unlinkErr => {
                    if(unlinkErr) throw unlinkErr;
                    sockServer.listen(SOCK_FILE, () => {
                        log(`Sock server improperly shut down. Listening on '${sockServer.address()}'`)
                    });
                });
            }
        });
        clientSocket.connect({path: SOCK_FILE}, () => {
            throw new Error(`Server already running`);
        });
    }
});

['SIGTERM','SIGINT'].forEach(signal => process.on(signal, () => {
    console.log(`\rReceived ${Chalk.yellow(signal)}, shutting down ${Chalk.red('❤')}`);
    sockServer.close();
    process.exit();
}));

sockServer.listen(SOCK_FILE, () => {
    log(`Listening on ${Chalk.underline(sockServer.address())}`)
});

When I restart the server, I see "Lost connection" twice on the client. Why?

The documentation says:

Emitted when the other end of the socket sends a FIN packet.

The server isn't sending two "FIN" packets is it? Any way I can verify?



via mpen

No comments:

Post a Comment