Skip to content

Commit

Permalink
feat(ext/http): ref/unref for server (#19197)
Browse files Browse the repository at this point in the history
Add `ref` and `unref` to return value from `Deno.serve`. Unblocks #3326.
  • Loading branch information
mmastrac authored May 19, 2023
1 parent af72a9c commit 7f5290b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cli/tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
assertThrows,
Deferred,
deferred,
execCode,
fail,
} from "./test_util.ts";

Expand Down Expand Up @@ -56,6 +57,21 @@ Deno.test(async function httpServerShutsDownPortBeforeResolving() {
listener!.close();
});

Deno.test(
{ permissions: { read: true, run: true } },
async function httpServerUnref() {
const [statusCode, _output] = await execCode(`
async function main() {
const server = Deno.serve({ port: 4501, handler: () => null });
server.unref();
await server.finished; // This doesn't block the program from exiting
}
main();
`);
assertEquals(statusCode, 0);
},
);

Deno.test(async function httpServerCanResolveHostnames() {
const ac = new AbortController();
const listeningPromise = deferred();
Expand Down
12 changes: 12 additions & 0 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,19 @@ declare namespace Deno {
* the signal passed to {@linkcode ServeOptions.signal}.
*/
finished: Promise<void>;

/**
* Make the server block the event loop from finishing.
*
* Note: the server blocks the event loop from finishing by default.
* This method is only meaningful after `.unref()` is called.
*/
ref(): void;

/** Make the server not block the event loop from finishing. */
unref(): void;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* Serves HTTP requests with the given handler.
Expand Down
28 changes: 26 additions & 2 deletions ext/http/00_serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const {
SetPrototypeAdd,
SetPrototypeDelete,
Symbol,
SymbolFor,
TypeError,
Uint8Array,
Uint8ArrayPrototype,
Expand Down Expand Up @@ -660,13 +661,22 @@ function serve(arg1, arg2) {

onListen({ port: listenOpts.port });

let ref = true;
let currentPromise = null;
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");

// Run the server
const finished = (async () => {
while (true) {
const rid = context.serverRid;
let req;
try {
req = await op_http_wait(rid);
currentPromise = op_http_wait(rid);
if (!ref) {
core.unrefOp(currentPromise[promiseIdSymbol]);
}
req = await currentPromise;
currentPromise = null;
} catch (error) {
if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
break;
Expand All @@ -691,7 +701,21 @@ function serve(arg1, arg2) {
}
})();

return { finished };
return {
finished,
ref() {
ref = true;
if (currentPromise) {
core.refOp(currentPromise[promiseIdSymbol]);
}
},
unref() {
ref = false;
if (currentPromise) {
core.unrefOp(currentPromise[promiseIdSymbol]);
}
},
};
}

internals.addTrailers = addTrailers;
Expand Down

0 comments on commit 7f5290b

Please sign in to comment.