Skip to content

refactor(NODE-5675): refactor server selection and connection checkout to use abort signals for timeout management #3890

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

Merged
merged 7 commits into from
Oct 17, 2023

Conversation

baileympearson
Copy link
Contributor

Description

What is changing?

  • A custom abort controller, TimeoutController, has been added that aborts its signal after a specified timeout
  • Server selection and connection checkout have been refactored to use the TimeoutController instead of managing timeouts
Is there new documentation needed for these changes?

No.

What is the motivation for this change?

Release Highlight

Fill in title or leave empty for no highlight

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

);
}, waitQueueTimeoutMS);
}
const waitQueueMember: WaitQueueMember = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might ask - why are we using AbortControllers and then manually consuming their signal, instead of making ConnectionPool.checkOut accept an abort signal? Really, there are a few reasons but it's primarily with the eventual state of CSOT in mind (note: everything here applies to server selection as well):

  • We need to clear any timeouts we set for connection checkout after we successfully obtain a connection. Abort signals do not provide a "cancellation" interface - and if they did, it would break the Controller/Signal abstraction. Controllers control signals and signals notify other components of abortion.
  • Given the above - in later work we will be introducing a TimeoutControllerFactory that is a property on the OperationContext. So eventually it will look like:
const waitQueueMember: WaitQueueMember = {
      callback,
      timeoutController: operationContext.timeoutFactory.controllerForConnectionCheckout()
}
  • To answer "why don't we make connection checkout accept a signal instead and pass it in?" - it's two fold. First - we need to cancel the controller like the first bullet mentions. Second - mostly preference, but in terms of encapsulation, I'd rather have connection checkout be responsible for managing its own timeout. If we pass a signal in as an option, we'd need to construct a timeout signal outside of every place we call ConnectionPool.checkout.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, considering the perspective how this piece of code relates to CSOT as a whole. What was the reasoning for not including this information in, say, the PR description so that team reviewers will be able to access it easier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just preference. I try to answer questions that reviewers might have as they're reviewing the code in advance of anyone reviewing it. I've found the most effective way to do that is by adding comments at the places I think reviewers might have questions

* consistent with existing timeout options in the Node driver (serverSelectionTimeoutMS, for example).
* @internal
*/
export class TimeoutController extends AbortController {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimeoutController intentionally returns an "infinite" timeout when timeout is <=0 or undefined. This unifies code paths for existing timeouts. We now always create a timeout controller to manage server selection and connection checkout, but only if a non-zero positive timeout was provided do we actually create a Nodejs timeout that cancels the request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. Do you feel it would be beneficial to also capture this behavior in the PR description or the ticket information?

@nbbeeken nbbeeken self-requested a review October 13, 2023 19:24
@nbbeeken nbbeeken self-assigned this Oct 13, 2023
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Oct 13, 2023
@nbbeeken nbbeeken added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Oct 16, 2023
@nbbeeken nbbeeken merged commit db90293 into mongodb:main Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants