Skip to content

Commit b679e71

Browse files
committed
chore: add LICENSE and update docs
1 parent 0822bc4 commit b679e71

File tree

4 files changed

+102
-29
lines changed

4 files changed

+102
-29
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2022 <COPYRIGHT HOLDER>
2+
3+
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.

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,4 @@ setInterval(() => {
147147
```
148148

149149
## LICENSE
150-
151-
## TODO
152-
- seqpacket sockets should be closed automatically
150+
MIT

docs/README.md

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ nix-socket / [Exports](modules.md)
1616
## `SO_REUSEPORT` enabled TCP net.Server
1717

1818
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+
1920
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.
2021

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+
![cpu_usage](./resource/cpu_usage.png)
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.
2227

2328
### Example
2429

@@ -48,39 +53,102 @@ for (let i = 0; i < 2; i += 1) {
4853
}
4954

5055
setInterval(() => {
51-
const client = new Socket()
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+
const client = new Socket()
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+
})
5964
}, 1000)
6065
```
6166

6267
## Seqpacket Sockets
6368

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.
72+
73+
### Example
74+
6475
```js
6576
const { SeqpacketServer, SeqpacketSocket } = require('nix-socket')
6677
const os = require('os')
6778
const path = require('path')
79+
const fs = require('fs')
6880

69-
const bindPath = path.resolve(os.tmp(), './my.sock')
81+
const bindPath = path.resolve(os.tmpdir(), './my_seqpacket.sock')
7082

71-
const server = new SeqpacketServer()
83+
try { fs.unlinkSync(bindPath) } catch (e) {}
7284

85+
const server = new SeqpacketServer()
7386
server.listen(bindPath)
87+
server.on('connection', socket => {
88+
socket.on('data', buf => {
89+
console.log('received', buf.toString())
90+
})
91+
});
7492

7593
const client = new SeqpacketSocket()
76-
77-
// TODO
78-
client.write()
94+
client.connect(bindPath, () => {
95+
const data = [
96+
'hello, ',
97+
'w',
98+
'o',
99+
'r',
100+
'l',
101+
'd'
102+
]
103+
104+
for (const str of data) {
105+
client.write(Buffer.from(str))
106+
}
107+
client.end()
108+
})
79109
```
80110

81111
## Dgram Sockets
82112

113+
### Example
114+
83115
```js
116+
const { DgramSocket } = require('nix-socket')
117+
const os = require('os')
118+
const path = require('path')
119+
const fs = require('fs')
120+
121+
const path1 = path.resolve(os.tmpdir(), './my_dgram_1.sock')
122+
const path2 = path.resolve(os.tmpdir(), './my_dgram_2.sock')
123+
124+
try {
125+
fs.unlinkSync(path1);
126+
fs.unlinkSync(path2);
127+
} catch (err) {}
128+
129+
const socket1 = new DgramSocket()
130+
const socket2 = new DgramSocket()
131+
132+
socket1.bind(path1)
133+
socket2.bind(path2)
134+
135+
socket2.on('data', (data, remoteAddr) => {
136+
console.log(`socket2 received: ${data.toString()}`)
137+
// echo
138+
socket2.sendTo(data, 0, data.length, remoteAddr);
139+
})
140+
141+
socket1.on('data', (data) => {
142+
console.log(`socket1 received: ${data.toString()}`)
143+
})
144+
145+
setInterval(() => {
146+
const buf = Buffer.from('hello')
147+
socket1.sendTo(buf, 0, buf.length, path2)
148+
}, 1000)
84149
```
85150

86151
## LICENSE
152+
153+
## TODO
154+
- seqpacket sockets should be closed automatically

docs/classes/SeqpacketSocket.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ EventEmitter.constructor
6666

6767
#### Defined in
6868

69-
seqpacket.ts:148
69+
seqpacket.ts:150
7070

7171
## Methods
7272

@@ -92,7 +92,7 @@ However, connect() will throw error synchronously if the 'serverPath' is not a v
9292

9393
#### Defined in
9494

95-
seqpacket.ts:210
95+
seqpacket.ts:222
9696

9797
___
9898

@@ -108,7 +108,7 @@ Ensures that no more I/O activity happens on this socket. Destroys the stream an
108108

109109
#### Defined in
110110

111-
seqpacket.ts:273
111+
seqpacket.ts:289
112112

113113
___
114114

@@ -130,7 +130,7 @@ Half-closes the socket. i.e., it sends a FIN packet. It is possible the server w
130130

131131
#### Defined in
132132

133-
seqpacket.ts:232
133+
seqpacket.ts:248
134134

135135
___
136136

@@ -148,7 +148,7 @@ Default size is 256KB.
148148

149149
#### Defined in
150150

151-
seqpacket.ts:243
151+
seqpacket.ts:259
152152

153153
___
154154

@@ -164,7 +164,7 @@ Reference the socket so that it will prevent Node.js process from exiting automa
164164

165165
#### Defined in
166166

167-
seqpacket.ts:259
167+
seqpacket.ts:275
168168

169169
___
170170

@@ -186,7 +186,7 @@ Set the size of buffer that SeqpacketSocket uses to receive data.
186186

187187
#### Defined in
188188

189-
seqpacket.ts:252
189+
seqpacket.ts:268
190190

191191
___
192192

@@ -202,13 +202,13 @@ Unreference the socket so that it won't prevent Node.js process from exiting aut
202202

203203
#### Defined in
204204

205-
seqpacket.ts:266
205+
seqpacket.ts:282
206206

207207
___
208208

209209
### write
210210

211-
**write**(`buf`, `offset`, `length`, `cb?`): `void`
211+
**write**(`buf`, `offset?`, `length?`, `cb?`): `void`
212212

213213
Sends data on the socket.
214214

@@ -217,8 +217,8 @@ Sends data on the socket.
217217
| Name | Type |
218218
| :------ | :------ |
219219
| `buf` | `Buffer` |
220-
| `offset` | `number` |
221-
| `length` | `number` |
220+
| `offset?` | `number` |
221+
| `length?` | `number` |
222222
| `cb?` | [`NotifyCb`](../modules.md#notifycb) |
223223

224224
#### Returns
@@ -227,4 +227,4 @@ Sends data on the socket.
227227

228228
#### Defined in
229229

230-
seqpacket.ts:223
230+
seqpacket.ts:235

0 commit comments

Comments
 (0)