Skip to content

Commit

Permalink
feat: add autoUnref option
Browse files Browse the repository at this point in the history
With autoUnref set to true (default: false), the Socket.IO client will
allow the program to exit if there is no other active timer/socket in
the event system.

```js
const socket = io({
  autoUnref: true
});
```

Note: this option only applies to Node.js clients.

Related: #1446
  • Loading branch information
KC Erb authored and darrachequesne committed Mar 10, 2021
1 parent 5902365 commit 6abfa1f
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 13 deletions.
15 changes: 15 additions & 0 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ export interface ManagerOptions extends EngineOptions {
*/
autoConnect: boolean;

/**
* weather we should unref the reconnect timer when it is
* create automatically
* @default false
*/
autoUnref: boolean;

/**
* the parser to use. Defaults to an instance of the Parser that ships with socket.io.
*/
Expand Down Expand Up @@ -531,6 +538,10 @@ export class Manager<
socket.emit("error", new Error("timeout"));
}, timeout);

if (this.opts.autoUnref) {
timer.unref();
}

this.subs.push(function subDestroy(): void {
clearTimeout(timer);
});
Expand Down Expand Up @@ -769,6 +780,10 @@ export class Manager<
});
}, delay);

if (this.opts.autoUnref) {
timer.unref();
}

this.subs.push(function subDestroy() {
clearTimeout(timer);
});
Expand Down
18 changes: 6 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"backo2": "~1.0.2",
"component-emitter": "~1.3.0",
"debug": "~4.3.1",
"engine.io-client": "~4.1.0",
"engine.io-client": "~5.0.0",
"parseuri": "0.0.6",
"socket.io-parser": "~4.0.4"
},
Expand Down Expand Up @@ -108,5 +108,8 @@
},
"tsd": {
"directory": "test"
},
"browser": {
"./test/node.ts": false
}
}
8 changes: 8 additions & 0 deletions test/fixtures/no-unref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const io = require("../..");
const socket = io("http://localhost:3211", {
autoUnref: false,
});

setTimeout(() => {
console.log("process should not exit");
}, 500);
12 changes: 12 additions & 0 deletions test/fixtures/unref-during-reconnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const io = require("../..");
const socket = io("http://localhost:3211", {
autoUnref: true,
});

socket.on("open", () => {
console.log("open");
});

setTimeout(() => {
console.log("process should exit now");
}, 500);
13 changes: 13 additions & 0 deletions test/fixtures/unref-polling-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const io = require("../..");
const socket = io("http://localhost:3210", {
autoUnref: true,
transports: ["polling"],
});

socket.on("open", () => {
console.log("open");
});

setTimeout(() => {
console.log("process should exit now");
}, 500);
13 changes: 13 additions & 0 deletions test/fixtures/unref-websocket-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const io = require("../..");
const socket = io("http://localhost:3210", {
autoUnref: true,
transports: ["websocket"],
});

socket.on("open", () => {
console.log("open");
});

setTimeout(() => {
console.log("process should exit now");
}, 500);
12 changes: 12 additions & 0 deletions test/fixtures/unref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const io = require("../..");
const socket = io("http://localhost:3210", {
autoUnref: true,
});

socket.on("open", () => {
console.log("open");
});

setTimeout(() => {
console.log("process should exit now");
}, 500);
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { browser } = require("./support/env");
// whitelist some globals to avoid warnings
if (browser) {
window.mocha.globals(["___eio", "eio_iframe_*"]);
} else {
require("./node.ts");
}

require("./url.ts");
Expand Down
33 changes: 33 additions & 0 deletions test/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const path = require("path");
const { exec } = require("child_process");

describe("autoRef option", () => {
const fixture = (filename) =>
process.execPath + " " + path.join(__dirname, "fixtures", filename);

it("should stop once the timer is triggered", (done) => {
exec(fixture("unref.ts"), done);
});

it("should stop once the timer is triggered (even when trying to reconnect)", (done) => {
exec(fixture("unref-during-reconnection.ts"), done);
});

it("should stop once the timer is triggered (polling)", (done) => {
exec(fixture("unref-polling-only.ts"), done);
});

it("should stop once the timer is triggered (websocket)", (done) => {
exec(fixture("unref-websocket-only.ts"), done);
});

it("should not stop with autoUnref set to false", (done) => {
const process = exec(fixture("no-unref.ts"), () => {
done(new Error("should not happen"));
});
setTimeout(() => {
process.kill();
done();
}, 1000);
});
});

0 comments on commit 6abfa1f

Please sign in to comment.