-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0eff63e
commit 910bcdb
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Utilities for working with registries.""" | ||
|
||
import httpx | ||
|
||
from reflex.utils import console | ||
|
||
|
||
def latency(registry: str) -> int: | ||
"""Get the latency of a registry. | ||
Args: | ||
registry (str): The URL of the registry. | ||
Returns: | ||
int: The latency of the registry in microseconds. | ||
""" | ||
try: | ||
return httpx.get(registry).elapsed.microseconds | ||
except httpx.HTTPError: | ||
console.info(f"Failed to connect to {registry}.") | ||
return 10_000_000 | ||
|
||
|
||
def average_latency(registry, attempts: int = 3) -> int: | ||
"""Get the average latency of a registry. | ||
Args: | ||
registry (str): The URL of the registry. | ||
attempts (int): The number of attempts to make. Defaults to 10. | ||
Returns: | ||
int: The average latency of the registry in microseconds. | ||
""" | ||
return sum(latency(registry) for _ in range(attempts)) // attempts | ||
|
||
|
||
def _get_best_registry() -> str: | ||
"""Get the best registry based on latency. | ||
Returns: | ||
str: The best registry. | ||
""" | ||
registries = [ | ||
"https://registry.npmjs.org", | ||
"https://r.cnpmjs.org", | ||
] | ||
|
||
return min(registries, key=average_latency) |
910bcdb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好耶!这下会方便很多