Skip to content

Commit

Permalink
No more need to close sockets explicitly in threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
rolftimmermans committed Oct 24, 2019
1 parent 0538240 commit 6b4904a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 36 deletions.
24 changes: 9 additions & 15 deletions examples/threaded-worker/threaded-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,25 @@ export class ThreadedWorker {
}

async stop() {
/* Closing sockets explicitly in threads is required until Node 13+.
https://github.com/nodejs/node/pull/28428 */
this.input.close()
this.output.close()
this.signal.close()
}

/* Loop over input and produce output. */
async run() {
try {
for await (const [pos, req] of this.input) {
if (req.length !== 1) {
console.log(`skipping invalid '${req}'`)
continue
}
for await (const [pos, req] of this.input) {
if (req.length !== 1) {
console.log(`skipping invalid '${req}'`)
continue
}

console.log(`received work '${req}' at ${pos}`)
console.log(`received work '${req}' at ${pos}`)

const res = await this.work(req.toString())
await this.output.send([pos, res])
const res = await this.work(req.toString())
await this.output.send([pos, res])

console.log(`finished work '${req}' -> '${res}' at ${pos}`)
}
} finally {
this.stop()
console.log(`finished work '${req}' -> '${res}' at ${pos}`)
}
}

Expand Down
23 changes: 2 additions & 21 deletions test/unit/socket-thread-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ for (const proto of testProtos("tcp", "ipc", "inproc")) {
await sockA.connect(address)
await sockA.send(["foo", "bar"])

const incoming = await sockB.receive()

/* Closing sockets explicitly in threads is required until Node 13+.
https://github.com/nodejs/node/pull/28428 */
sockA.close()
sockB.close()
return incoming
return sockB.receive()
})

assert.deepEqual(["foo", "bar"], recv.map((buf) => Buffer.from(buf).toString()))
Expand All @@ -45,10 +39,6 @@ for (const proto of testProtos("tcp", "ipc", "inproc")) {
await sockB.connect(address)
for await (const msg of sockB) {
await sockB.send(msg)

/* Closing sockets explicitly in threads is required until Node 13+.
https://github.com/nodejs/node/pull/28428 */
sockB.close()
return
}
})
Expand All @@ -69,12 +59,7 @@ for (const proto of testProtos("tcp", "ipc", "inproc")) {
await sockA.bind(address)
await sockA.send(["foo", "bar"])

const incoming = await sockA.receive()

/* Closing sockets explicitly in threads is required until Node 13+.
https://github.com/nodejs/node/pull/28428 */
sockA.close()
return incoming
return sockA.receive()
})

/* tslint:disable-next-line: no-shadowed-variable */
Expand All @@ -83,10 +68,6 @@ for (const proto of testProtos("tcp", "ipc", "inproc")) {
await sockB.connect(address)
for await (const msg of sockB) {
await sockB.send(msg)

/* Closing sockets explicitly in threads is required until Node 13+.
https://github.com/nodejs/node/pull/28428 */
sockB.close()
return
}
})
Expand Down

0 comments on commit 6b4904a

Please sign in to comment.