Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-6258

Cancel individual queries with AbortController

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.13.0
    • Affects Version/s: None
    • Component/s: None
    • 0
    • Needed
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      The Node driver's cursors now can be cancelled with abort signals:

      await collection.find({}, { signal }); // when signal fires, the cursor will be closed
      

      It would be nice to add documentation to our cursor reference page with information regarding cancellation of queries using abort signals.

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      It's not necessary but it might be nice to have some code examples.

      3. Which versions of the driver/connector does this apply to?

      This will apply to 6.13+.

      Show
      1. What would you like to communicate to the user about this feature? The Node driver's cursors now can be cancelled with abort signals: await collection.find({}, { signal }); // when signal fires, the cursor will be closed It would be nice to add documentation to our cursor reference page with information regarding cancellation of queries using abort signals. 2. Would you like the user to see examples of the syntax and/or executable code and its output? It's not necessary but it might be nice to have some code examples. 3. Which versions of the driver/connector does this apply to? This will apply to 6.13+.

      Use Case

      As a... Node.js Driver user
      I want... to abort in-progress operations
      So that... I can stop waiting for and spending resources on a result that will not be used

      Avoid load on DB for ignored queries.

      Eg when doing a search, if the count operation takes significantly longer (eg 200ms) than finding the results to display, we can cancel it instead of just ignoring it.

      Or when a client cancels their http request, we can cancel ongoing mongodb read queries for that request

      User Experience

      • What is the desired/expected outcome for the user once this ticket is implemented?

      Being able to pass a signal parameter to queries, which would be from an AbortController.

      Triggering:
       

      const abort = new AbortController();
      const query = db.coll("...").countDocuments(..., \{ signal: abort.signal});
      abort.abort();
      

      Dependencies

      • AbortController & AbortSignal

      Risks/Unknowns

      • What could go wrong while implementing this change? (e.g., performance, inadvertent behavioral changes in adjacent functionality, existing tech debt, etc)
        • Care must be taken to add and remove AbortSignal listeners to avoid memory leaks
        • Signal code should be written in a way that makes it no-op when a signal is not provided (optional chaining etc) for performance
      • Is there an opportunity for better cross-driver alignment or testing in this area?
        • No
      • Is there an opportunity to improve existing documentation on this subject?
        • Not existing, we should document how signals can be used with the APIs we add support to

      Acceptance Criteria

      Implementation Requirements

      • Add AbortSignal support to the TBD APIs
        • (list here)
      • Ensure signal interrupts a query during any point in its lifecycle:
        • Server Selection
        • Connection Checkout
        • (if enabled) Client Side Encryption
        • R/W Socket

      Testing Requirements

      • Check various signal states, already aborted, aborted while the driver is in one of the lifecycle states listed above, aborted after.
      • For cursor operations check abort works as expected in between commands (ex. before/after find and getMores accordingly)

      Documentation Requirements

      • Add API documentation to the signal option

      Follow Up Requirements

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            eliott@huggingface.co Eliott Coyac
            Bailey Pearson, Warren James
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: