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

fixing reconnect handling #1627

Merged
merged 1 commit into from
Oct 7, 2024
Merged

Conversation

frankstolle
Copy link
Contributor

@frankstolle frankstolle commented Oct 5, 2024

After a reconnect the old heartbeat checker was not finished, but a new heartbeat checker was created. After many reconnects, the checkers stopped working because some checker always recognized a timeout and caused a new reconnect.

The fix ensures that before creating a new heartbeat checker, the old one is stopped. Also on reset a connection, the old one is stopped.

This solves the reconnect problem for me:

nodered-1  | 5 Oct 16:55:46 - [info] [server:Home Assistant] Connection closed to http://localhost
nodered-1  | 5 Oct 16:55:46 - [info] [server:Home Assistant] Connecting to http://localhost
nodered-1  | 5 Oct 16:55:46 - [info] [server:Home Assistant] Connected to http://localhost
nodered-1  | 5 Oct 16:55:46 - [info] [server:Home Assistant] Connection closed to http://localhost
nodered-1  | 5 Oct 16:55:46 - [info] [server:Home Assistant] Connecting to http://localhost
nodered-1  | 5 Oct 16:55:46 - [info] [server:Home Assistant] Connected to http://localhost
nodered-1  | 5 Oct 16:55:51 - [info] [server:Home Assistant] Connection closed to http://localhost
nodered-1  | 5 Oct 16:55:51 - [info] [server:Home Assistant] Connecting to http://localhost
nodered-1  | 5 Oct 16:55:51 - [info] [server:Home Assistant] Connected to http://localhost
nodered-1  | 5 Oct 16:55:56 - [info] [server:Home Assistant] Connection closed to http://localhost

Similar issue: #1425

Summary by CodeRabbit

  • Bug Fixes
    • Improved management of the heartbeat mechanism to prevent multiple active heartbeats.
  • Refactor
    • Centralized heartbeat stopping logic for better clarity and maintainability.

After a reconnect the old heartbeat checker was not finished, but a new
heartbeat checker was created. After many reconnects, the checkers
stopped working because some checker always recognized a timeout and
caused a new reconnect.
Copy link

coderabbitai bot commented Oct 5, 2024

Walkthrough

The changes involve modifications to the Websocket class in src/homeAssistant/Websocket.ts. A new private method, #stopAssignedHeartbeat, is introduced to manage the stopping of the heartbeat mechanism. This method is called in the onClientOpen, close, and resetClient methods to ensure proper handling of the heartbeat across different states of the WebSocket connection. The updates streamline the process of managing heartbeats, enhancing the clarity and maintainability of the code.

Changes

File Change Summary
src/homeAssistant/Websocket.ts - Added private method #stopAssignedHeartbeat to manage heartbeat stopping logic.
- Updated resetClient to call #stopAssignedHeartbeat.
- Modified onClientOpen to call #stopAssignedHeartbeat before starting a new heartbeat.
- Changed close method to invoke #stopAssignedHeartbeat instead of directly checking this.#stopHeartbeat.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Websocket

    Client->>Websocket: open connection
    Websocket->>Websocket: #stopAssignedHeartbeat()
    Websocket->>Websocket: start new heartbeat
    Websocket-->>Client: connection established

    Client->>Websocket: close connection
    Websocket->>Websocket: #stopAssignedHeartbeat()
    Websocket-->>Client: connection closed
Loading

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 613e505 and 342b10f.

📒 Files selected for processing (1)
  • src/homeAssistant/Websocket.ts (2 hunks)
🔇 Additional comments (5)
src/homeAssistant/Websocket.ts (5)

Line range hint 558-564: Approve: Stopping existing heartbeat before starting a new one

This change addresses the issue of multiple heartbeat checkers running simultaneously. By calling this.#stopAssignedHeartbeat() before starting a new heartbeat, we ensure that any existing heartbeat is properly terminated. This should prevent the problems mentioned in the PR objectives, where multiple active heartbeats were causing timeouts and triggering unnecessary reconnect attempts.


585-587: Approve: Consistent heartbeat termination in close method

This change improves the close method by using the new #stopAssignedHeartbeat() method. This ensures consistent handling of heartbeat termination across different parts of the class, reducing code duplication and improving maintainability.


589-596: Approve: New #stopAssignedHeartbeat method improves heartbeat management

The addition of the #stopAssignedHeartbeat() method is a good improvement:

  1. It centralizes the heartbeat stopping logic, reducing code duplication.
  2. It safely handles cases where this.#stopHeartbeat might not be a function.
  3. By setting this.#stopHeartbeat to undefined after calling it, it prevents potential multiple calls to the same heartbeat stopper.

This change enhances the robustness of the heartbeat management system and aligns with the PR's objective of fixing reconnect handling issues.


Line range hint 598-611: Approve: Proper heartbeat termination during client reset

The addition of this.#stopAssignedHeartbeat() at the beginning of the resetClient method is an important improvement:

  1. It ensures that the heartbeat is properly stopped when resetting the client state, addressing one of the key objectives mentioned in the PR description.
  2. This change prevents potential issues that could arise from a lingering heartbeat after a client reset.
  3. The modification integrates seamlessly with the existing reset logic, maintaining the overall structure of the method.

This change contributes to the overall goal of stabilizing the reconnect process and preventing complications from multiple active heartbeat checkers.


Line range hint 1-824: Summary: Effective improvements to heartbeat management

The changes made in this file successfully address the issues outlined in the PR objectives:

  1. The introduction of the #stopAssignedHeartbeat() method centralizes and standardizes the heartbeat stopping logic.
  2. Proper heartbeat termination is now ensured in key areas: when opening a new connection, closing the client, and resetting the client state.
  3. These modifications effectively prevent the problem of multiple active heartbeat checkers, which was causing timeouts and unnecessary reconnect attempts.

Overall, these changes should significantly improve the stability of the reconnect process and resolve the issues with heartbeat management. The implementation is clean, consistent, and well-integrated with the existing code structure.


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@zachowj
Copy link
Owner

zachowj commented Oct 7, 2024

very nice

@zachowj zachowj merged commit 4d0a842 into zachowj:main Oct 7, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants