shawnbu
shawnbu12mo ago

Close a Deno.serve() connection?

Is it possible to close a Deno.serve() connection?
7 Replies
ioB
ioB12mo ago
Do you mean the server or the connection? If you mean the server, you should use the web-standard AbortSignal API:
const controller = new AbortController();
const signal = controller.signal;

Deno.serve({ signal }, () => {
// whenever you want to close the server
controller.abort();
})
const controller = new AbortController();
const signal = controller.signal;

Deno.serve({ signal }, () => {
// whenever you want to close the server
controller.abort();
})
shawnbu
shawnbu12mo ago
Oh wow! I thought I found it through unref(), this seems nicer. How would I close the connection outside the request loop?
ioB
ioB12mo ago
You could close the connection outside of the request loop like you'd expect! Just call controller.abort();
shawnbu
shawnbu12mo ago
If I have many Deno.servers going on for different ports (which is what I'm doing), how do I map a controller to a specific server? Oh, I pass the signal instance into the server? I see. And then how to pass configs with the signal at the same time? Forgive me, I'm new to Deno and trying to figure it all how.
ioB
ioB12mo ago
Presumably you'd want to shut down the servers at different times? If that's the case, I think you got it. If that's not the case, you only need one abort controller!
shawnbu
shawnbu12mo ago
Thanks so much for helping me with your answers.
Yes, at different times. I can remote send a command to do it. only thing is how to initialize server with the signal and config props at the same time (for port)
lcasdev
lcasdev12mo ago
Next to the signal, you can specify a port property
More Posts
Relative import path "preact" not prefixed with / or ./ or ../ at https://deno.land/x/fresh@1.4.Please help. ``` deno 1.36.1 (release, aarch64-apple-darwin) v8 11.6.189.12 typescript 5.1.6 ```Problems with Deno KV```ts import InputLoop from "https://deno.land/x/input@2.0.3/index.ts"; const kv = await Deno.openKFile differs only by case errorI'm getting a strange error from TypeScript. For some reason it thinks the case of my file name is Possible to have multiple http servers listening to different ports in the same module?I'm porting a service mesh platform we wrote for NodeJS over to Deno and it is based on the idea of How to have private state in embedded deno_core JsRuntime?I need to either keep a reference in rust to an object I then `delete globalThis.__context`, but I cIssues getting local environment running in VSCode and Visual Studio 2022I'm a windows user trying to contribute on an github issue, but I've run into a few blockers. Using Relative import path "ioredis" not prefixed with / or ./ or ../```ts // main.ts import Redis from "ioredis"; ``` ```json // deno.json { "imports": { "ioredisPixi.js breaks DenoI'm trying to import Pixi in a Fresh island: ``` import { Sprite, Stage } from "npm:@pixi/react@7.1Confused by deno vendor-related behaviorsI created a main.ts containing this code: ``` import * as thrift from "npm:thrift"; let connection =How to release node:zlib resourcesI have some testcases which use the `node:zlib` polyfill and I'm having a bit of trouble getting the