-
Notifications
You must be signed in to change notification settings - Fork 8
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
Automatically abort task when client disconnects #8
Comments
FWIW Node.js 16 has an undocumented breaking change that broke this pattern, because I've switched to
now (Keep-Alive I guess...) 😅 . Edit: If we ignore the const cache = new WeakMap();
export default function (fastify) {
fastify.decorateRequest('abortSignal', null);
fastify.addHook('onRequest', (req, reply, done) => {
let socket = req.raw.socket;
let controller = cache.get(socket);
// Make sure we only create a single AbortController for each socket (Keep-Alive exists).
if (!controller) {
controller = new AbortController();
cache.set(socket, controller);
socket.once('close', () => {
controller.abort();
});
}
req.abortSignal = controller.signal;
done();
});
} |
You can use |
Thanks for pointing to fastify-racing! I'm happy with what I've posted above for now. |
Yeah, that's what it only does, adding |
I just found this plugin after already using piscina in fastify. I've decorated my request with an
abortSignal
:So I can do this in a request handler
I'm not sure how reliable this is, but I needed to abort expensive tasks when the client cancels the
fetch
in the browser viaAbortController
(I'm using this in an Electron app). Would it make sense to integrate something like this into the plugin? The semantic is somewhat inspired by Golang context.The text was updated successfully, but these errors were encountered: