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

Avoid creating async_register_repository tasks to register_unknown_repositories #3536

Merged
merged 8 commits into from May 23, 2024

Conversation

bdraco
Copy link
Contributor

@bdraco bdraco commented Mar 1, 2024

Await these in series to avoid creating tasks as they are unlikely to suspend.

fixes #3535

@bdraco
Copy link
Contributor Author

bdraco commented Mar 1, 2024

first call takes 0.00011390098370611668s
second call and after take 3.6492012441158295e-05

await in series seems like a better option here

@bdraco bdraco changed the title Create async_register_repository restore tasks eagerly when available Avoid creating async_register_repository tasks to register_unknown_repositories Mar 1, 2024
@bdraco
Copy link
Contributor Author

bdraco commented Mar 1, 2024

need to do a fresh install to make sure this is ok

@ludeeus
Copy link
Member

ludeeus commented Mar 1, 2024

need to do a fresh install to make sure this is ok

if you do that you should tick the last box in the flow to enable experimental not to give yourself a GitHub rate limit break 😄

@bdraco
Copy link
Contributor Author

bdraco commented Mar 1, 2024

This works well on a fresh install as well

@bdraco bdraco marked this pull request as ready for review March 1, 2024 21:37
Copy link
Member

@ludeeus ludeeus left a comment

Choose a reason for hiding this comment

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

Thanks 👍

I'm currently in the process of changing things around with actions and testing, so If you do not mind I will leave it as-is (open) until that is complete so this can be run against that as well.

It will still go in the next version.

@bdraco
Copy link
Contributor Author

bdraco commented Mar 1, 2024

Its been a problem for years and we have lived with it so no rush at all.

@bdraco
Copy link
Contributor Author

bdraco commented Mar 3, 2024

Screenshot 2024-03-02 at 2 23 22 PM

All the time is spent on line 30

@bdraco
Copy link
Contributor Author

bdraco commented Mar 3, 2024

constructing enums can be oddly slow because they are singletons that have to find the object

don't know if translates to real world savings, but first attempt to make it faster would be:

diff --git a/custom_components/hacs/repositories/integration.py b/custom_components/hacs/repositories/integration.py
index 70b8b547..72fe5f6b 100644
--- a/custom_components/hacs/repositories/integration.py
+++ b/custom_components/hacs/repositories/integration.py
@@ -18,6 +18,7 @@ from .base import HacsRepository
 if TYPE_CHECKING:
     from ..base import HacsBase
 
+CATEGORY_INTEGRATION = HacsCategory.INTEGRATION
 
 class HacsIntegrationRepository(HacsRepository):
     """Integrations in HACS."""
@@ -27,7 +28,7 @@ class HacsIntegrationRepository(HacsRepository):
         super().__init__(hacs=hacs)
         self.data.full_name = full_name
         self.data.full_name_lower = full_name.lower()
-        self.data.category = HacsCategory.INTEGRATION
+        self.data.category = CATEGORY_INTEGRATION
         self.content.path.remote = "custom_components"
         self.content.path.local = self.localpath
 

@ludeeus
Copy link
Member

ludeeus commented Mar 3, 2024

That will eventually be set as a class instead of an instance attribute.
That will be a better saving than doing this.

Copy link

coderabbitai bot commented May 22, 2024

Walkthrough

Walkthrough

The register_unknown_repositories method in data.py has been refactored to improve event loop performance by restructuring the iteration over repositories, adding registration conditions, and introducing a sleep delay. These changes aim to prevent the creation of numerous concurrent tasks that could slow down the event loop.

Changes

Files Change Summary
custom_components/hacs/utils/data.py Refactored register_unknown_repositories method to iterate over repositories, add conditions, and introduce sleep.
custom_components/hacs/utils/data.py Modified method signature to accept repositories as a dictionary with specific types.

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Prevent flood of async_register_repository tasks at startup (#3535)
Improve event loop performance by restructuring repository registration (#3535)
Add conditions to repository registration (#3535)
Introduce sleep to avoid blocking the event loop (#3535)

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits Files that changed from the base of the PR and between a8a92a9 and 744a224.
Files selected for processing (1)
  • custom_components/hacs/utils/data.py (1 hunks)
Additional comments not posted (2)
custom_components/hacs/utils/data.py (2)

253-254: Updated method signature to accept a dictionary with specific typing annotations.


257-274: The logic to await tasks in series is implemented correctly. This should help in managing the event loop more efficiently by preventing the creation of excessive tasks.

Verification successful

The async_register_repository method is implemented correctly, using await for asynchronous operations and including appropriate error handling. This ensures that the event loop is not overwhelmed by tasks.

  • custom_components/hacs/base.py: The async_register_repository method handles the event loop efficiently.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the event loop is not overwhelmed by tasks after the change.

# Test: Monitor the event loop's performance metrics before and after the change.
echo "Monitor event loop performance metrics."

Length of output: 88



Script:

#!/bin/bash
# Locate the definition of the async_register_repository method
ast-grep --lang python --pattern $'def async_register_repository($_, $_, $_, $_, $_):\n    $$$'

# Locate all calls to async_register_repository in the codebase
rg 'async_register_repository\('

Length of output: 705



Script:

#!/bin/bash
# Extract the implementation of the async_register_repository method from custom_components/hacs/base.py
ast-grep --lang python --pattern $'def async_register_repository($_, $_, $_, $_, $_):\n    $$$' custom_components/hacs/base.py

Length of output: 126



Script:

#!/bin/bash
# Search for the definition of the async_register_repository method in custom_components/hacs/base.py and extract the implementation
rg -A 20 'def async_register_repository' custom_components/hacs/base.py

Length of output: 843



Script:

#!/bin/bash
# Extract more lines from the async_register_repository method implementation in custom_components/hacs/base.py
rg -A 50 'def async_register_repository' custom_components/hacs/base.py

Length of output: 2224



Script:

#!/bin/bash
# Extract more lines from the async_register_repository method implementation in custom_components/hacs/base.py
rg -A 100 'def async_register_repository' custom_components/hacs/base.py

Length of output: 4297


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.

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.
  • 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: 1

custom_components/hacs/utils/data.py Show resolved Hide resolved
@ludeeus ludeeus merged commit 9ad1e76 into hacs:main May 23, 2024
26 checks passed
@hacs-bot hacs-bot bot added this to the next milestone May 23, 2024
@bdraco bdraco deleted the register branch May 23, 2024 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A flood of async_register_repository tasks gets created at startup which slows down the event loop
2 participants