forked from gagle/node-tftp
/
graceful-shutdown.js
72 lines (58 loc) · 2.16 KB
/
graceful-shutdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
var tftp = require ("../../lib");
/*
This example demonstrates how to close the server and all the current
connections gracefully.
This is slightly different from the http server where the "connection" event
returns the socket and you must call to socket.destroy() to close it. On the
other hand, this tftp server doesn't have a "connection" event because the
internal socket is not exposed to the public, it just has a "request" event
which is fired each time the server receives a new request. The "req" argument
acts like a "connection" object. When req.abort() is called it sends an error
message to the client and then the socket closes, that is, it's a real graceful
shutdown. Instead of killing the socket by brute force, the server informs the
client that the transfer has been aborted, so the client is able to abort the
transfer immediately instead of begin a timeout and then abort.
*/
var connections = [];
var server = tftp.createServer ();
server.on ("request", function (req){
req.on ("error", function (error){
//Error from the request
console.error (error);
});
//Save the connection
connections.push (req);
//The "close" event is fired when the internal socket closes, regardless
//whether it is produced by an error, the socket closes naturally due to the
//end of the transfer or the transfer has been aborted
req.on ("close", function (){
//Remove the connection
connections.splice (connections.indexOf (this), 1);
if (closed && !connections.length){
//The server and all the connections have been closed
console.log ("Server closed");
}
});
});
server.on ("error", function (error){
//Errors from the main socket
console.error (error);
});
server.listen ();
var closed = false;
setTimeout (function (){
//Close the server after 10s
server.on ("close", function (){
closed = true;
if (!connections.length){
return console.log ("Server closed");
}
//Abort all the current transfers
for (var i=0; i<connections.length; i++){
console.log ("Connection " + i + " aborted");
connections[i].abort ();
}
});
server.close ();
}, 10000);