A good heartbeat timer
npm install jasonkuhrt-heartbeat
var tcp = require('net');
var heartbeat = require('heartbeat');
tcp.createServer(9000, function(socket){
/* If we the socket ever doesn't send data for ten
consecutive seconds, consider it dead.*/
var thump = heartbeat(onFlatline, 10000);
socket.on('data', thump);
function onFlatline(){
log.warn('Socket timed out, destroying socket.');
socket.removeListener('data', thump);
socket.removeListener('end', onCleanDisconnect);
socket.destroy();
}
/* On a clean disconnect destroy the heartbeat.*/
socket.once('end', onCleanDisconnect);
function onCleanDisconnect(){
socket.removeListener('data', thump);
heartbeat.clear(thump);
}
});
> npm start
...
... (some time passes, your app does stuff, then maybe...)
...
Socket timed out, destroying socket.
Heartbeat h; Int i; :: ( -> ), i -> h
Returns a heartbeat instance. A heartbeat instance is a function. Invoke it to keep the heartbeat going. The identifier is typically thump
(see guide).
-
onFlatline
is invoked when/ifthump
is not invoked during an interval. -
intervalMs
sets the time between thump checks.
Heartbeat a :: a -> undefined
Destroy a heartbeat instance. Analagous to clearInterval
/clearTimeout
.
Only use clearHeartbeat
if you need to abort a heartbeat before onFlatline
.
https://github.com/jasonkuhrt/heartbeat/blob/master/test/index.js