-
Notifications
You must be signed in to change notification settings - Fork 51
feat: add automatic recovery for streaming ASR/TTS #189
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
Open
pgowda1107
wants to merge
2
commits into
nvidia-riva:main
Choose a base branch
from
pgowda1107:feat/resilient-streaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ def create_channel( | |
| use_ssl: bool = False, | ||
| uri: str = "localhost:50051", | ||
| metadata: Optional[List[Tuple[str, str]]] = None, | ||
| options: Optional[List[Tuple[str, str]]] = [], | ||
| options: Optional[List[Tuple[str, Union[str, int]]]] = None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't get why this change is needed? |
||
| use_aio: Optional[bool] = False, | ||
| ) -> grpc.Channel: | ||
| def metadata_callback(context, callback): | ||
|
|
@@ -61,7 +61,7 @@ def __init__( | |
| metadata_args: List[List[str]] = None, | ||
| ssl_client_cert: Optional[Union[str, os.PathLike]] = None, | ||
| ssl_client_key: Optional[Union[str, os.PathLike]] = None, | ||
| options: Optional[List[Tuple[str, str]]] = [], | ||
| options: Optional[List[Tuple[str, Union[str, int]]]] = None, | ||
| use_aio: bool = False, | ||
| ) -> None: | ||
| """ | ||
|
|
@@ -82,8 +82,8 @@ def __init__( | |
| Used for mutual TLS authentication. Defaults to None. | ||
| ssl_client_key (Optional[Union[str, os.PathLike]], optional): Path to the SSL client private key file. | ||
| Used for mutual TLS authentication. Defaults to None. | ||
| options (Optional[List[Tuple[str, str]]], optional): Additional gRPC channel options. | ||
| Each tuple should contain (option_name, option_value). Defaults to []. | ||
| options (Optional[List[Tuple[str, Union[str, int]]]], optional): Additional gRPC channel options. | ||
| Each tuple should contain an option name and value. | ||
| use_aio (bool, optional): Whether to use asyncio for the channel. Defaults to False. | ||
|
|
||
| Raises: | ||
|
|
||
This file contains hidden or 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,76 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """Shared retry utilities for resilient streaming ASR and TTS clients. | ||
|
|
||
| This module provides constants and helpers for handling transient gRPC | ||
| failures with exponential backoff. It is designed to be used by both | ||
| :mod:`riva.client.asr` and :mod:`riva.client.tts`. | ||
| """ | ||
|
|
||
| import logging | ||
| import random | ||
| import grpc | ||
|
|
||
| LOGGER = logging.getLogger(__name__) | ||
|
|
||
| # gRPC status codes that are generally considered transient and safe to retry. | ||
| RETRYABLE_GRPC_CODES = frozenset({ | ||
| grpc.StatusCode.UNAVAILABLE, | ||
| grpc.StatusCode.DEADLINE_EXCEEDED, | ||
| grpc.StatusCode.INTERNAL, | ||
| grpc.StatusCode.RESOURCE_EXHAUSTED, | ||
| grpc.StatusCode.ABORTED, | ||
| }) | ||
|
|
||
| # gRPC status codes that should NEVER be retried (client-side errors). | ||
| NON_RETRYABLE_GRPC_CODES = frozenset({ | ||
| grpc.StatusCode.INVALID_ARGUMENT, | ||
| grpc.StatusCode.PERMISSION_DENIED, | ||
| grpc.StatusCode.UNAUTHENTICATED, | ||
| grpc.StatusCode.NOT_FOUND, | ||
| grpc.StatusCode.ALREADY_EXISTS, | ||
| grpc.StatusCode.FAILED_PRECONDITION, | ||
| grpc.StatusCode.OUT_OF_RANGE, | ||
| grpc.StatusCode.UNIMPLEMENTED, | ||
| }) | ||
|
|
||
| def is_retryable_grpc_error(exc: grpc.RpcError) -> bool: | ||
| """Return ``True`` if *exc* is a transient gRPC error that is safe to retry. | ||
|
|
||
| Args: | ||
| exc: The exception raised by a gRPC call. | ||
|
|
||
| Returns: | ||
| ``True`` if the error code is in :data:`RETRYABLE_GRPC_CODES`. | ||
| """ | ||
| code = exc.code() if hasattr(exc, "code") else None | ||
| if code is None: | ||
| return False | ||
| return code in RETRYABLE_GRPC_CODES | ||
|
|
||
|
|
||
| def exponential_backoff( | ||
| attempt: int, | ||
| base_delay: float = 1.0, | ||
| max_delay: float = 60.0, | ||
| jitter: bool = True, | ||
| ) -> float: | ||
| """Compute a sleep duration for the *attempt*-th retry. | ||
|
|
||
| Uses capped exponential backoff with optional full jitter to avoid | ||
| thundering-herd behaviour when many clients reconnect simultaneously. | ||
|
|
||
| Args: | ||
| attempt: Zero-based retry attempt number. | ||
| base_delay: Initial delay in seconds. | ||
| max_delay: Upper bound for the delay in seconds. | ||
| jitter: If ``True``, multiply the delay by a random factor in ``[0, 1)``. | ||
|
|
||
| Returns: | ||
| The number of seconds to sleep before the next attempt. | ||
| """ | ||
| delay = min(base_delay * (2 ** attempt), max_delay) | ||
| if jitter: | ||
| delay = delay * random.random() | ||
| return delay |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
instead of new wrapper classes, is it possible to add this logic into existing recognize/synthesize functions? default disabled and only take effect when custom_configuration arguments are sent