Skip to content

Automatically abort task when client disconnects #8

@Prinzhorn

Description

@Prinzhorn

I just found this plugin after already using piscina in fastify. I've decorated my request with an abortSignal:

const { AbortController } = require('abort-controller');

module.exports = function (fastify) {
  fastify.decorateRequest('abortSignal', null);

  fastify.addHook('onRequest', (req, reply, done) => {
    let controller = new AbortController();

    req.raw.once('close', () => {
      controller.abort();
    });

    req.abortSignal = controller.signal;

    done();
  });
};

So I can do this in a request handler

await piscina.runTask(task, req.abortSignal);

I'm not sure how reliable this is, but I needed to abort expensive tasks when the client cancels the fetch in the browser via AbortController (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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions