Skip to content

Support for AbortController and signal in NodeJS #153

Description

@twswilliansantos

Hello Couchnode team!

I have an API made with nodejs and express and I'm currently using couchbase: 4.7.1.

I recently implemented a new feature in this API, which consists by applying a request abortion, using the class AbortController and signal.

The request is proper cancelled, as per requested, although, I couldn't find an option in the couchbase that allows me to send this signal and get in-flights couchbase query cancelled when the related request is aborted.

If I understand correctly, even with a request been cancelled, the in-flight query keeps running in couchbase server.

Is that possible, somehow, pass the AbortController instance to the couchbase connection or query in order to cancel in-flight queries?

In case this option is not available in couchbase nodejs library, is it possible to have this implemented?

Here's some code examples to show what I'd like to implement:

// Called at the beginning of application
app.use(
  '/',
  cors({ origin: true, credentials: true }),
  bodyParser.json({ limit: '50mb' }),

  expressMiddleware(server, {
    context: async ({ req, res }) => {
      const abortController = new AbortController();

      // Client disconnected/aborted - stop waiting on downstream work for this request.
      req.on('close', () => {
        if (!res.writableEnded) abortController.abort();
      });

      const query = 'any N1QL query here';
      const parameters = { param1: 'value1', param2: 'value2' };

      executeQuery(query, parameters, abortController.signal);
    },
  })
);

// Function that executes the query
async function executeQuery(query, parameters = {}, abortSignal) {
  if (abortSignal?.aborted) {
    const abortErr = new Error(`Query aborted before execution`);
    abortErr.name = ABORT_ERROR_NAME;
    throw abortErr;
  }

  const { cluster } = await CouchbaseConnectionManager.getConnection();

  const result = await cluster.query(query, { 
      parameters, 
      signal: abortSignal // Here I pass the abort signal so that the query execution can be cancelled
    }); 

  return result.rows;
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions