Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/http): ref/unref for server #19197

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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