You are creating a server in Node.js and you need to control the accessible defaults for the server.
Solution
When you create any type of networked server, you often find that the default configuration might need to be tweaked to meet your specific needs. Aside from setting the host and port for a TCP server, you might like to be able to set the maximum number of connections, or control what the system backlog queue length for pending connections is configured as in your server. Many of these settings have default values on your server.
Naturally, one of the simplest parts of your server that you can control is the port and host where the server will be listening. These are set when calling the listen() method on your server. The listen method (as seen in Section 2-1) also takes the listener callback, but a third parameter that is optionally placed before this callback is the backlog setting, which limits your server’s connection queue length. Putting these defaults into place, you can see what the listen() function will look like in Listing 2-11.
Listing 2-11. Setting the listen( ) Defaults
server.listen(8181, '127.0.0.1', 12, function() { // listen on 127.0.0.1:8181
// backlog queue capped at 12 console.log('server is listening'); });
Chapter 2 ■ NetworkiNg with Node.js
Another default to consider is the option set when calling the createServer() method, which allows for a half- open connection and which defaults to false but is set in the method as shown in Listing 2-12.
Listing 2-12. allowHalfOpen: true
var server = net.createServer({ allowHalfOpen: true }, function(connectionListener) { /* connection Listener stuffs */
});
Setting a maximum number of connections to your server can also be quite useful in your Node.js application. If you wish to limit this, you must explicitly set the number, as it defaults to undefined. This is best set in the connectionListener callback, as shown in Listing 2-13.
Listing 2-13. Setting a Maximum Number of Connections to Your Server
var server = net.createServer({ allowHalfOpen: true }, function(connectionListener) { console.log('connected');
//get maxConnections - default undefined console.log(this.maxConnections);
// set maxConnections to 4 this.maxConnections = 4;
// check set maxConnections is 4 console.log(this.maxConnections); });
How It Works
Setting and overriding server defaults happens by checking them against the default settings; then they are
overwritten. What happens when you pass a backlog argument to the listen() method in Node.js? First, the default value that is passed into the backlog argument is 511. The value 511 is passed because of how the backlog size is determined by the operating system kernel.
// Use a backlog of 512 entries. We pass 511 to the listen( ) call because
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
// which will thus give us a backlog of 512 entries.
This is interesting to know. Because you set the backlog queue to be capped at 12 in the server.listen() example from Listing 2-11, you can now know that this will be calculated to be 16. This is because the value you set, 12, is incremented by one, then rounded up to the nearest power of 2, which is 16. It should be noted that in the example server.listen from Listing 2-11, you set the value of the host address as 127.0.0.1, which is IPv4. However, Node.js just as easily handles IPv6 connections, so you could change your default server listen to use IPv6, as shown in Listing 2-14.
Listing 2-14. Configuring a Server Using IPv6 server.listen(8181, '::1', 12, function() {
Chapter 2 ■ NetworkiNg with Node.js
Subsequently the server.address() function will log the new host and also the family will now be IPv6 instead of IPv4.
{ address: '::1', family: 'IPv6', port: 8181 }
Allowing a half-open connection was an option you set in Listing 2-12, { allowHalfOpen: true }. This sets the connection to allow for what you may find to be a more finely grained control of your server’s connection. This will allow a connection to send the TCP FIN packet, the packet that requests a termination of the connection but does not automatically send the response FIN packet to the connection.
This means that you will leave one half of the TCP connection open, allowing the socket to remain writable but not readable. To officially close the connection, you must explicitly send the FIN packet by calling the .end( ) method on the connection.
You also saw how you could set a limit to the maximum number of connections to your server via Node.js and the net module’s maxConnections setting. This, by default, is undefined, but in Listing 2-13 it was set to a low number of 4. This means your connections are limited to 4, but what happens when you connect, or attempt to connect, to a server that has a maximum number of connections set? You can see what the Node.js source does to this setting in Listing 2-15.
Listing 2-15. Node.js Handles the maxConnections Setting
if (self.maxConnections && self._connections >= self.maxConnections) { clientHandle.close();
return; }
This gives you a little more insight into why maxConnections defaults to undefined. This is because if it isn’t set, there is no need for Node.js to bother with this section of code. However, if it is set, a simple check will see if the current number of connections on the server is greater than or equal to the maxConnections setting, and it will close the connection. If you have a Node.js client connection you wish to connect (which you will read more about in Section 2-4), but the number of connections exceeds this limit, you will see the connection’s close event emitted, and you can handle it appropriately, as shown in Listing 2-16.
Listing 2-16. Handling the close Event on a Connection Handle connection.on('close', function() {
console.log('connection closed'); });
If, on the other hand, you have simply hit the server endpoint via Telnet (telnet ::1 8181), the response will be “connection closed by foreign host,” as shown in Figure 2-1.
Chapter 2 ■ NetworkiNg with Node.js