You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.
19
+
19
20
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
21
-
Note that `SO_REUSEPORT` might behave much differently across operating systems. See this informative [post](https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ) for more information.
22
+
For example, the arrow in the image below shows cpu usage of a PM2 primary process which we found in our environment.
23
+
24
+

25
+
26
+
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.
22
27
23
28
### Example
24
29
@@ -48,39 +53,102 @@ for (let i = 0; i < 2; i += 1) {
48
53
}
49
54
50
55
setInterval(() => {
51
-
constclient=newSocket()
52
-
client.on('data', (buf) => {
53
-
console.log('client received:', buf)
54
-
client.destroy()
55
-
})
56
-
client.connect(port, host, () => {
57
-
client.write(Buffer.from('hello'))
58
-
})
56
+
constclient=newSocket()
57
+
client.on('data', (buf) => {
58
+
console.log('client received:', buf)
59
+
client.destroy()
60
+
})
61
+
client.connect(port, host, () => {
62
+
client.write(Buffer.from('hello'))
63
+
})
59
64
}, 1000)
60
65
```
61
66
62
67
## Seqpacket Sockets
63
68
69
+
`SOCK_SEQPACKET` sockets are like `SOCK_DGRAM` sockets and they will keep message boundaries.
70
+
71
+
Note that `SOCK_SEQPACKET` sockets don't work on MacOS.
0 commit comments