Skip to content
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

feat: impl PoolWaitTimeoutError #7

Merged
merged 1 commit into from
Aug 12, 2024
Merged

feat: impl PoolWaitTimeoutError #7

merged 1 commit into from
Aug 12, 2024

Conversation

killagu
Copy link
Member

@killagu killagu commented Aug 10, 2024

Throw error when get connection wait time great than poolWaitTimeout.

Summary by CodeRabbit

  • New Features

    • Enhanced connection management in the RDSClient with configurable timeout options.
    • Introduced a custom error handling mechanism for connection timeout scenarios.
    • New property added to RDSClientOptions for specifying pool wait timeout.
    • Unique identifier for RDSTransaction instances to improve tracking.
  • Bug Fixes

    • Improved error handling for connection timeouts, providing clearer feedback to users.
  • Tests

    • Added a suite of tests to validate connection pooling behavior under timeout conditions.

Copy link

coderabbitai bot commented Aug 10, 2024

Walkthrough

The recent updates enhance the RDSClient class by introducing robust connection timeout management and improved error handling. New properties and methods provide greater reliability and clarity in timeout scenarios, while additional configuration options allow users to specify wait times. Accompanying test cases ensure proper functionality under various conditions, reinforcing the robustness of database interactions.

Changes

Files Change Summary
README.md Added comments on connection timeout settings for RDSClient.
src/client.ts, src/types.ts Introduced #poolWaitTimeout, waitPoolConnection, and getConnectionWithTimeout methods for enhanced timeout management; added optional poolWaitTimeout property to RDSClientOptions interface.
src/util/PoolWaitTimeout.ts Defined PoolWaitTimeoutError class for specific timeout error handling.
test/client.test.ts Added PoolWaitTimeout suite with tests for timeout error handling, connection release, and execution time validation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RDSClient
    participant ConnectionPool
    participant Timer

    User->>RDSClient: Request connection
    RDSClient->>Timer: Start timeout
    RDSClient->>ConnectionPool: Get connection
    alt Connection available
        ConnectionPool-->>RDSClient: Provide connection
        RDSClient-->>User: Return connection
    else Timeout occurs
        Timer-->>RDSClient: Notify timeout
        RDSClient-->>User: Throw PoolWaitTimeoutError
    end
Loading

🐇 In a world where connections flow,
Timeout whispers soft and low.
With new methods, we shall see,
A rabbit's hop to reliability!
Errors caught with joyful cheer,
Gather 'round, the code is clear! 🐾


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
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 <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 194062e and 9ae7675.

Files selected for processing (5)
  • README.md (1 hunks)
  • src/client.ts (5 hunks)
  • src/types.ts (1 hunks)
  • src/util/PoolWaitTimeout.ts (1 hunks)
  • test/client.test.ts (2 hunks)
Files skipped from review due to trivial changes (1)
  • README.md
Additional comments not posted (12)
src/util/PoolWaitTimeout.ts (1)

1-5: LGTM!

The PoolWaitTimeoutError class is well-implemented, extending the Error class and setting a custom name. This is a good practice for distinguishing specific error types.

src/types.ts (1)

11-11: LGTM!

The addition of the poolWaitTimeout property to RDSClientOptions enhances configuration flexibility and is backward-compatible.

src/client.ts (6)

3-3: LGTM!

The import of setTimeout from node:timers/promises is appropriate for implementing the connection timeout functionality.


65-65: LGTM!

The #poolWaitTimeout property is well-defined, providing a default value of 500ms, which is a reasonable default for connection timeouts.


86-86: LGTM!

The initialization of #poolWaitTimeout in the constructor is correct, ensuring the property is set based on options or defaults.


155-159: LGTM!

The waitPoolConnection method is well-implemented, using setTimeout for timeout management and supporting cancellation with AbortSignal.


161-174: LGTM!

The getConnectionWithTimeout method is well-designed, using Promise.race to manage connection acquisition and handle timeouts effectively.


178-180: LGTM!

The update to use getConnectionWithTimeout in getConnection improves error handling and connection management.

test/client.test.ts (4)

1499-1504: LGTM!

The longQuery function is well-structured for simulating long-running queries with a default timeout.


1506-1515: LGTM!

The test case correctly verifies that an error is thrown when the pool wait timeout is exceeded.


1517-1544: LGTM!

The test case effectively checks that connections are released back to the pool after timeout.


1546-1587: LGTM!

The test case accurately validates that the overall execution time is within the expected threshold.

@fengmk2 fengmk2 added the enhancement New feature or request label Aug 12, 2024
src/client.ts Outdated Show resolved Hide resolved
const timeoutPromise = this.waitPoolConnection(timeoutAbortController.signal);
const connOrTimeout = await Promise.race([ connPromise, timeoutPromise ]);
if (typeof connOrTimeout === 'number') {
connPromise.then(conn => {
Copy link
Member

Choose a reason for hiding this comment

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

后续得去给 getConnection 加一个 abortSignal 支持,就不需要写下面这种 hack 逻辑。
connPromise 这段逻辑代码其实也奇怪,一拿到 conn 就马上释放。

Copy link
Member Author

Choose a reason for hiding this comment

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

mysql 还停留在 callback 的写法,这个改动比较难了。

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

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

单测修复一下?其他没问题了。

Throw error when get connection wait time great than poolWaitTimeout.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9ae7675 and 8dd743c.

Files selected for processing (7)
  • README.md (1 hunks)
  • src/client.ts (5 hunks)
  • src/connection.ts (3 hunks)
  • src/transaction.ts (1 hunks)
  • src/types.ts (1 hunks)
  • src/util/PoolWaitTimeout.ts (1 hunks)
  • test/client.test.ts (6 hunks)
Files skipped from review due to trivial changes (2)
  • README.md
  • src/util/PoolWaitTimeout.ts
Files skipped from review as they are similar to previous changes (3)
  • src/client.ts
  • src/types.ts
  • test/client.test.ts
Additional comments not posted (4)
src/transaction.ts (2)

4-4: Static variable id is correctly initialized.

The static variable id is initialized to 0 to serve as a unique identifier counter for RDSTransaction instances. This is a valid approach for tracking instances.


9-13: Unique identifier id is effectively implemented.

The id property is added to the RDSTransaction class and initialized with id++ to ensure each instance has a unique identifier. This implementation is straightforward and effective for tracking instances.

src/connection.ts (2)

10-14: Private property #released is correctly added and initialized.

The #released property is added to track the release state of the connection. It is initialized to false to indicate that the connection has not been released initially. This is a good practice for managing connection state.


30-31: Update to release method enhances robustness.

The assertion in the release method ensures that a connection can only be released once by checking the #released property. This prevents errors from multiple releases and improves connection management.

@killagu killagu merged commit f42012d into master Aug 12, 2024
11 checks passed
@killagu killagu deleted the feat/poolwaittime branch August 12, 2024 05:39
fengmk2 pushed a commit that referenced this pull request Aug 12, 2024
[skip ci]

## [1.2.0](v1.1.0...v1.2.0) (2024-08-12)

### Features

* impl PoolWaitTimeoutError ([#7](#7)) ([f42012d](f42012d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants