alisaifee / limits

Rate limiting using various strategies and storage backends such as redis & memcached
https://limits.readthedocs.org
MIT License
428 stars 58 forks source link

Increased default timeout for mongodb to 10 seconds to accomodate slow connections, considering production replica sets and sharding. #201

Closed Vishwamithra37 closed 9 months ago

Vishwamithra37 commented 9 months ago

Summary by CodeRabbit

coderabbitai[bot] commented 9 months ago

Walkthrough

This update enhances the resilience and reliability of the MongoDB connection in the MongoDBStorage class by significantly increasing the timeouts for server selection and connection establishment. By adjusting the timeout values from 1 second to 10 seconds, the system aims to reduce potential issues related to brief network interruptions or slow server responses, ensuring a smoother operation and better user experience.

Changes

File Change Summary
limits/aio/storage/mongodb.py Increased serverSelectionTimeoutMS and connectTimeoutMS from 1000ms to 10000ms in DEFAULT_OPTIONS.

πŸ‡βœ¨

In the code's garden, under the moon's soft glow,

A rabbit hopped, with changes in tow.

"Let's wait a bit longer," it whispered with glee,

For connections to bloom, like flowers, you see.

Tenfold patience, now the norm,

Ensuring data flows, through calm and storm.

πŸŒ±πŸŒŸπŸ’ΎπŸ•’βœ¨

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

Tips ### Chat There are 3 ways to chat with CodeRabbit: - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit-tests for this file.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit tests for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository from git and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit tests.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - The JSON schema for the configuration file is available [here](https://coderabbit.ai/integrations/coderabbit-overrides.v2.json). - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json` ### CodeRabbit Discord Community Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback.
alisaifee commented 9 months ago

@Vishwamithra37 that default value is too high - if in your setup that is expected why not pass it in explicitly?

codecov[bot] commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (f1fe9f3) 96.83% compared to head (90442cf) 96.83%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #201 +/- ## ======================================= Coverage 96.83% 96.83% ======================================= Files 25 25 Lines 1265 1265 Branches 135 135 ======================================= Hits 1225 1225 Misses 20 20 Partials 20 20 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Vishwamithra37 commented 9 months ago

@Vishwamithra37 that default value is too high - if in your setup that is expected why not pass it in explicitly?

I believe 10s is okay. Because of the following reasons:

  1. In my and many setups I know where flask-limiter is used, (a wrapper which uses limits library), it is not obvious as to where and how to pass on the connection options for the Database.
  2. Given it is not obvious and the dev need to seperately mention the timeout for the limits library again -- Assuming they also mention the same in their normal database operations config, 10s is actually too low, the 10s is just enough for developing upon replica sets and sharded databases locally.
  3. The 1s is far too low and will only work when there is a single instance of the DB working. (In my specific case mongodb). And since this library is used as secondary, it can get hard to find out why exactly the error is occuring. In the case of replica set: ServerSelectionTimeoutError on mongo replica set using pymongo Which makes the dev question if the db is at fault. -- in this case its not*
alisaifee commented 9 months ago

In my and many setups I know where flask-limiter is used, (a wrapper which uses limits library), it is not obvious as to where and how to pass on the connection options for the Database.

To solve this - I think the better solution would be to improve the documentation in flask-limiter (and even here in limits) to make it clearer what the defaults are and how to provide ones own configuration.

alisaifee commented 9 months ago

ServerSelectionTimeoutError on mongo replica set using pymongo

The error you shared above seems to be referring to a misconfiguration rather than an actual timeout?

Vishwamithra37 commented 9 months ago

ServerSelectionTimeoutError on mongo replica set using pymongo

The error you shared above seems to be referring to a misconfiguration rather than an actual timeout?

My main concern is that it is redundant to make the user specify everything two times, one for their original application and one for their limits library And 1 second is not the standard. And 10s is fine because in case of production/developing connection failure, I'm 100% sure 10s is somewhat equal to 1s as it will crash the application anyway. And the developer would rather give mongodb some time than quickly crash the application by default. (From first hand experience.)

The mongodb forum answers: https://www.mongodb.com/community/forums/t/what-is-the-recommended-value-for-the-serverselectiontimeoutms-setting-for-atlas/149806

Scale Grid answers: https://scalegrid.io/blog/mongodb-timeout-settings/#:~:text=By%20default%2C%20this%20timeout%20is,this%20setting%20does%20affect%20performance.

Vishwamithra37 commented 9 months ago

ServerSelectionTimeoutError on mongo replica set using pymongo

The error you shared above seems to be referring to a misconfiguration rather than an actual timeout?

It is also important to understand that timeout should be long enough by default to make the library more stable rather than quickly go into error mode. As mentioned, timeout is the Maxiumum time its gonna wait for the transaction/CRUD/Connection to stabilize and reconnect to be done. If the connection is stable enough, this shouldnt be an issue. And in case of sudden mishaps, the system should be able to at least wait for the minimum time required for the connection to resolve itself. (Both in case of Mongodb and other SQL dbs). A quick connect and reconnect should not directly impact any application (under 10s).

alisaifee commented 9 months ago

And 1 second is not the standard.

Fair enough. I'd like to propose however, instead of setting a higher timeout by default, how about we just remove the DEFAULT_OPTIONS entirely and leave it to the defaults of the underlying pymongo / motor clients?

Vishwamithra37 commented 9 months ago

And 1 second is not the standard.

Fair enough. I'd like to propose however, instead of setting a higher timeout by default, how about we just remove the DEFAULT_OPTIONS entirely and leave it to the defaults of the underlying pymongo / motor clients?

Yes!, that sounds like a good way to handle it too :) . I think we should proceed with removing the default options altogether.

alisaifee commented 9 months ago

Thanks for the input and suggestions. Could we close this PR in favour of 55cdfb9a3731a46fcb768fd5dad93dc99534909b?

Vishwamithra37 commented 9 months ago

Thanks for the input and suggestions. Could we close this PR in favour of 55cdfb9a3731a46fcb768fd5dad93dc99534909b?

Yep please do :)

alisaifee commented 9 months ago

Change available in 3.9.0