|
12 | 12 |
|
13 | 13 | [API Documents](./docs/modules.md)
|
14 | 14 |
|
15 |
| -## `SO_REUSEPORT` enabled TCP net.Server |
16 |
| - |
17 |
| -The [cluster](https://nodejs.org/dist/latest-v18.x/docs/api/cluster.html) module share server ports by accepting new connections in the primary process and distributing them to worker processes. |
18 |
| - |
19 |
| -With `SO_REUSEPORT`, sockets will be distributed by kernel instead, and which should be more performant especially for scenario of having a lot of short-lived connections. |
20 |
| - |
21 |
| -For example, the arrow in the image below shows cpu usage of a PM2 primary process which we found in our environment. |
22 |
| - |
23 |
| - |
24 |
| - |
25 |
| -Note that `SO_REUSEPORT` might behave much differently across operating systems. See this [post](https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ) for more information. |
26 |
| - |
27 |
| -### Example |
28 |
| - |
29 |
| -```js |
30 |
| -const { createReuseportFd } = require('node-unix-socket'); |
31 |
| -const { Server, Socket } = require('net'); |
32 |
| - |
33 |
| -const port = 8080; |
34 |
| -const host = '0.0.0.0'; |
35 |
| - |
36 |
| -// create multple servers listening to a same host, port. |
37 |
| -for (let i = 0; i < 2; i += 1) { |
38 |
| - const fd = createReuseportFd(port, host); |
39 |
| - const server = new Server((socket) => { |
40 |
| - socket.on('data', (buf) => { |
41 |
| - console.log(`server ${i} received:`, buf); |
42 |
| - // echo |
43 |
| - socket.write(buf); |
44 |
| - }); |
45 |
| - }); |
46 |
| - |
47 |
| - server.listen( |
48 |
| - { |
49 |
| - fd, |
50 |
| - }, |
51 |
| - () => { |
52 |
| - console.log(`server ${i} is listening on ${port}`); |
53 |
| - } |
54 |
| - ); |
55 |
| -} |
56 |
| - |
57 |
| -setInterval(() => { |
58 |
| - const client = new Socket(); |
59 |
| - client.on('data', (buf) => { |
60 |
| - console.log('client received:', buf); |
61 |
| - client.destroy(); |
62 |
| - }); |
63 |
| - client.connect(port, host, () => { |
64 |
| - client.write(Buffer.from('hello')); |
65 |
| - }); |
66 |
| -}, 1000); |
67 |
| -``` |
68 |
| - |
69 | 15 | ## Seqpacket Sockets
|
70 | 16 |
|
71 | 17 | `SOCK_SEQPACKET` sockets are like `SOCK_DGRAM` sockets and they will keep message boundaries.
|
@@ -145,6 +91,60 @@ setInterval(() => {
|
145 | 91 | }, 1000);
|
146 | 92 | ```
|
147 | 93 |
|
| 94 | +## `SO_REUSEPORT` enabled TCP net.Server |
| 95 | + |
| 96 | +The [cluster](https://nodejs.org/dist/latest-v18.x/docs/api/cluster.html) module share server ports by accepting new connections in the primary process and distributing them to worker processes. |
| 97 | + |
| 98 | +With `SO_REUSEPORT`, sockets will be distributed by kernel instead, and which should be more performant especially for scenario of having a lot of short-lived connections. |
| 99 | + |
| 100 | +For example, the arrow in the image below shows cpu usage of a PM2 primary process which we found in our environment. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +Note that `SO_REUSEPORT` might behave much differently across operating systems. See this [post](https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ) for more information. |
| 105 | + |
| 106 | +### Example |
| 107 | + |
| 108 | +```js |
| 109 | +const { createReuseportFd } = require('node-unix-socket'); |
| 110 | +const { Server, Socket } = require('net'); |
| 111 | + |
| 112 | +const port = 8080; |
| 113 | +const host = '0.0.0.0'; |
| 114 | + |
| 115 | +// create multple servers listening to a same host, port. |
| 116 | +for (let i = 0; i < 2; i += 1) { |
| 117 | + const fd = createReuseportFd(port, host); |
| 118 | + const server = new Server((socket) => { |
| 119 | + socket.on('data', (buf) => { |
| 120 | + console.log(`server ${i} received:`, buf); |
| 121 | + // echo |
| 122 | + socket.write(buf); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + server.listen( |
| 127 | + { |
| 128 | + fd, |
| 129 | + }, |
| 130 | + () => { |
| 131 | + console.log(`server ${i} is listening on ${port}`); |
| 132 | + } |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +setInterval(() => { |
| 137 | + const client = new Socket(); |
| 138 | + client.on('data', (buf) => { |
| 139 | + console.log('client received:', buf); |
| 140 | + client.destroy(); |
| 141 | + }); |
| 142 | + client.connect(port, host, () => { |
| 143 | + client.write(Buffer.from('hello')); |
| 144 | + }); |
| 145 | +}, 1000); |
| 146 | +``` |
| 147 | + |
148 | 148 | ## CONTRIBUTING
|
149 | 149 |
|
150 | 150 | [CONTRIBUTING.md](./CONTRIBUTING.md)
|
|
0 commit comments