couchbase / couchbase/couchnode

Support for AbortController and signal in NodeJS

Open
#153 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
462
Forks
225
PR merge metrics
No merged PRs in 30d

Description

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;
};

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the cluster.query entry point and inspect how query options are passed to Couchbase. Trace whether an AbortSignal can reach the in-flight request, then verify that aborting the signal stops the query and produces a defined cancellation result; the issue names no tests or source files.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
api, backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.