-
Notifications
You must be signed in to change notification settings - Fork 85
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
[Refactor] Extract GrpcRunner from GRPCIndexBase class #395
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from functools import wraps | ||
from typing import Dict, Tuple | ||
|
||
from grpc._channel import _InactiveRpcError | ||
|
||
from pinecone import Config | ||
from .utils import _generate_request_id | ||
from .config import GRPCClientConfig | ||
from pinecone.utils.constants import REQUEST_ID, CLIENT_VERSION | ||
from pinecone.exceptions.exceptions import PineconeException | ||
|
||
|
||
class GrpcRunner: | ||
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. Good idea pulling all of this out into somewhere contained. 👍 |
||
def __init__(self, index_name: str, config: Config, grpc_config: GRPCClientConfig): | ||
self.config = config | ||
self.grpc_client_config = grpc_config | ||
|
||
self.fixed_metadata = { | ||
"api-key": config.api_key, | ||
"service-name": index_name, | ||
"client-version": CLIENT_VERSION, | ||
} | ||
if self.grpc_client_config.additional_metadata: | ||
self.fixed_metadata.update(self.grpc_client_config.additional_metadata) | ||
|
||
def run( | ||
self, | ||
func, | ||
request, | ||
timeout=None, | ||
metadata=None, | ||
credentials=None, | ||
wait_for_ready=None, | ||
compression=None, | ||
): | ||
@wraps(func) | ||
def wrapped(): | ||
user_provided_metadata = metadata or {} | ||
_metadata = self._prepare_metadata(user_provided_metadata) | ||
try: | ||
return func( | ||
request, | ||
timeout=timeout, | ||
metadata=_metadata, | ||
credentials=credentials, | ||
wait_for_ready=wait_for_ready, | ||
compression=compression, | ||
) | ||
except _InactiveRpcError as e: | ||
raise PineconeException(e._state.debug_error_string) from e | ||
|
||
return wrapped() | ||
|
||
async def run_asyncio( | ||
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. Not using or testing |
||
self, | ||
func, | ||
request, | ||
timeout=None, | ||
metadata=None, | ||
credentials=None, | ||
wait_for_ready=None, | ||
compression=None, | ||
): | ||
@wraps(func) | ||
async def wrapped(): | ||
user_provided_metadata = metadata or {} | ||
_metadata = self._prepare_metadata(user_provided_metadata) | ||
try: | ||
return await func( | ||
request, | ||
timeout=timeout, | ||
metadata=_metadata, | ||
credentials=credentials, | ||
wait_for_ready=wait_for_ready, | ||
compression=compression, | ||
) | ||
except _InactiveRpcError as e: | ||
raise PineconeException(e._state.debug_error_string) from e | ||
|
||
return await wrapped() | ||
|
||
def _prepare_metadata(self, user_provided_metadata: Dict[str, str]) -> Tuple[Tuple[str, str]]: | ||
return tuple( | ||
(k, v) | ||
for k, v in { | ||
**self.fixed_metadata, | ||
**self._request_metadata(), | ||
**user_provided_metadata, | ||
}.items() | ||
) | ||
|
||
def _request_metadata(self) -> Dict[str, str]: | ||
return {REQUEST_ID: _generate_request_id()} |
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.
This wasn't being used, right?
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.
Currently unused. The
_logger
reference is now used in theGrpcChannelFactory
which is logic I recently pulled out of this base class. Cleaning up this reference should have happened in that PR, but got overlooked.