diff options
author | isaacs <i@izs.me> | 2012-06-12 10:02:52 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-06-13 12:24:45 -0700 |
commit | e733dc3bc3bcc876a29f2b78c5a29bae9dedb0c3 (patch) | |
tree | 9acdeed0a9f8eb02c8d5f0d0ad3c0727beff57af /doc | |
parent | 0187b657ae5c12ea98da8266c493d493566e1fc9 (diff) | |
download | node-new-e733dc3bc3bcc876a29f2b78c5a29bae9dedb0c3.tar.gz |
Fix #3388 Support listening on file descriptors
This implements server.listen({ fd: <filedescriptor> }). The fd should
refer to an underlying resource that is already bound and listening, and
causes the new server to also accept connections on it.
Not supported on Windows. Raises ENOTSUP.
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/cluster.markdown | 46 | ||||
-rw-r--r-- | doc/api/http.markdown | 18 | ||||
-rw-r--r-- | doc/api/net.markdown | 19 |
3 files changed, 83 insertions, 0 deletions
diff --git a/doc/api/cluster.markdown b/doc/api/cluster.markdown index c648f5942b..0464b898cc 100644 --- a/doc/api/cluster.markdown +++ b/doc/api/cluster.markdown @@ -41,6 +41,52 @@ Running node will now share port 8000 between the workers: This feature was introduced recently, and may change in future versions. Please try it out and provide feedback. +## How It Works + +<!--type=misc--> + +The worker processes are spawned using the `child_process.fork` method, +so that they can communicate with the parent via IPC and pass server +handles back and forth. + +When you call `server.listen(...)` in a worker, it serializes the +arguments and passes the request to the master process. If the master +process already has a listening server matching the worker's +requirements, then it passes the handle to the worker. If it does not +already have a listening server matching that requirement, then it will +create one, and pass the handle to the child. + +This causes potentially surprising behavior in three edge cases: + +1. `server.listen({fd: 7})` Because the message is passed to the worker, + file descriptor 7 **in the parent** will be listened on, and the + handle passed to the worker, rather than listening to the worker's + idea of what the number 7 file descriptor references. +2. `server.listen(handle)` Listening on handles explicitly will cause + the worker to use the supplied handle, rather than talk to the master + process. If the worker already has the handle, then it's presumed + that you know what you are doing. +3. `server.listen(0)` Normally, this will case servers to listen on a + random port. However, in a cluster, each worker will receive the + same "random" port each time they do `listen(0)`. In essence, the + port is random the first time, but predictable thereafter. If you + want to listen on a unique port, generate a port number based on the + cluster worker ID. + +When multiple processes are all `accept()`ing on the same underlying +resource, the operating system load-balances across them very +efficiently. There is no routing logic in Node.js, or in your program, +and no shared state between the workers. Therefore, it is important to +design your program such that it does not rely too heavily on in-memory +data objects for things like sessions and login. + +Because workers are all separate processes, they can be killed or +re-spawned depending on your program's needs, without affecting other +workers. As long as there are some workers still alive, the server will +continue to accept connections. Node does not automatically manage the +number of workers for you, however. It is your responsibility to manage +the worker pool for your application's needs. + ## cluster.settings * {Object} diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 962aa49081..ac8518edc9 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -158,6 +158,24 @@ a listener for the ['listening'](net.html#event_listening_) event. See also [net.Server.listen()](net.html#server.listen). +### server.listen(handle, [listeningListener]) + +* `handle` {Object} +* `listeningListener` {Function} + +The `handle` object can be set to either a server or socket (anything +with an underlying `_handle` member), or a `{fd: <n>}` object. + +This will cause the server to accept connections on the specified +handle, but it is presumed that the file descriptor or handle has +already been bound to a port or domain socket. + +Listening on a file descriptor is not supported on Windows. + +This function is asynchronous. The last parameter `callback` will be added as +a listener for the ['listening'](net.html#event_listening_) event. +See also [net.Server.listen()](net.html#server.listen). + ### server.close([cb]) Stops the server from accepting new connections. diff --git a/doc/api/net.markdown b/doc/api/net.markdown index 7fb07df955..41923ae67c 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -163,6 +163,25 @@ This function is asynchronous. When the server has been bound, the last parameter `listeningListener` will be added as an listener for the ['listening'](#event_listening_) event. +### server.listen(handle, [listeningListener]) + +* `handle` {Object} +* `listeningListener` {Function} + +The `handle` object can be set to either a server or socket (anything +with an underlying `_handle` member), or a `{fd: <n>}` object. + +This will cause the server to accept connections on the specified +handle, but it is presumed that the file descriptor or handle has +already been bound to a port or domain socket. + +Listening on a file descriptor is not supported on Windows. + +This function is asynchronous. When the server has been bound, +['listening'](#event_listening_) event will be emitted. +the last parameter `listeningListener` will be added as an listener for the +['listening'](#event_listening_) event. + ### server.close([cb]) Stops the server from accepting new connections and keeps existing |