Skip to content

Commit

Permalink
destroy sockets if reach wait threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Oct 26, 2021
1 parent 9914c37 commit ca6cbd0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bin/srvd.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function serve(

let last = Date.now();
let server;
const sockets = [];

let checkWaitTimeout, checkForCloseTimeout;

Expand All @@ -45,9 +46,20 @@ function serve(
if (checkForCloseTimeout) clearTimeout(checkForCloseTimeout);
}

function destroySockets() {
sockets.forEach(socket => {
try {
socket.destroy();
} catch (err) {
// pass
}
});
}

function checkWait() {
if (Date.now() - last > wait_ms) {
if (debug) console.log(`[srvd] we haven't received a request in ${wait} seconds, so closing the server`);
destroySockets();
server.close();
}
}
Expand All @@ -71,6 +83,10 @@ function serve(
}
});

server.on("connection", socket => {
sockets.push(socket);
});

server.listen(port);
if (debug) console.log("[srvd] serving on port " + port);

Expand Down
16 changes: 16 additions & 0 deletions srvd.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function serve(

let last = Date.now();
let server;
const sockets = [];

let checkWaitTimeout, checkForCloseTimeout;

Expand All @@ -43,9 +44,20 @@ function serve(
if (checkForCloseTimeout) clearTimeout(checkForCloseTimeout);
}

function destroySockets() {
sockets.forEach(socket => {
try {
socket.destroy();
} catch (err) {
// pass
}
});
}

function checkWait() {
if (Date.now() - last > wait_ms) {
if (debug) console.log(`[srvd] we haven't received a request in ${wait} seconds, so closing the server`);
destroySockets();
server.close();
}
}
Expand All @@ -69,6 +81,10 @@ function serve(
}
});

server.on("connection", socket => {
sockets.push(socket);
});

server.listen(port);
if (debug) console.log("[srvd] serving on port " + port);

Expand Down

0 comments on commit ca6cbd0

Please sign in to comment.