generated from ni/github-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Define common code for a base class for a gRPC client #122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3242b1b
Add a GrpcServiceClientBase and use it in pinmap.v1.client
dixonjoel 4ba6ac6
Add a GrpcServiceClientBase and use it in sessionmanagement.v1.client
dixonjoel 3911b2f
Fix some linter errors
dixonjoel abb4212
Fix some linter errors
dixonjoel adb1511
Fix some unit test failures only found in Python 3.9
dixonjoel 0c5c618
Make the _client_base.py files identical
dixonjoel e73ab06
Fix string type hints to use from __future__ import annotations
dixonjoel f48b045
Fix _client.py comments
dixonjoel d8015fa
Fix a few PR comments, add a type param to GrpcServiceClientBase
dixonjoel d98ec51
Remove _get_stub from _client.py in sessionmanagement.client package
dixonjoel bc6ae09
Fix type hints and use slots
dixonjoel 570ec5e
Various PR feedback
dixonjoel 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
100 changes: 100 additions & 0 deletions
100
....measurementlink.pinmap.v1.client/src/ni/measurementlink/pinmap/v1/client/_client_base.py
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,100 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import threading | ||
| from typing import Generic, Protocol, TypeVar | ||
|
|
||
| import grpc | ||
| from ni.measurementlink.discovery.v1.client import DiscoveryClient | ||
| from ni_grpc_extensions.channelpool import GrpcChannelPool | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class StubProtocol(Protocol): | ||
| """Protocol for gRPC stub classes.""" | ||
|
|
||
| def __init__(self, channel: grpc.Channel) -> None: | ||
| """Initialize the gRPC client.""" | ||
|
|
||
|
|
||
| TStub = TypeVar("TStub", bound=StubProtocol) | ||
|
|
||
|
|
||
| class GrpcServiceClientBase(Generic[TStub]): | ||
| """Base class for NI gRPC service clients.""" | ||
|
|
||
| __slots__ = ( | ||
| "_initialization_lock", | ||
| "_discovery_client", | ||
| "_grpc_channel_pool", | ||
| "_stub", | ||
| "_service_interface_name", | ||
| "_service_class", | ||
| "_stub_class", | ||
| ) | ||
|
|
||
| _initialization_lock: threading.Lock | ||
| _discovery_client: DiscoveryClient | None | ||
| _grpc_channel_pool: GrpcChannelPool | None | ||
| _stub: TStub | None | ||
| _service_interface_name: str | ||
| _service_class: str | ||
| _stub_class: type[TStub] | ||
|
|
||
| def __init__( | ||
| self, | ||
| service_interface_name: str, | ||
| service_class: str, | ||
| stub_class: type[TStub], | ||
| *, | ||
| discovery_client: DiscoveryClient | None = None, | ||
| grpc_channel: grpc.Channel | None = None, | ||
| grpc_channel_pool: GrpcChannelPool | None = None, | ||
| ) -> None: | ||
| """Initialize the gRPC client. | ||
|
|
||
| Args: | ||
| service_interface_name: The fully qualified name of the service interface. | ||
| service_class: The name of the service class. | ||
| stub_class: The gRPC stub class for the service. | ||
| discovery_client: An optional discovery client (recommended). | ||
| grpc_channel: An optional pin map gRPC channel. | ||
| grpc_channel_pool: An optional gRPC channel pool (recommended). | ||
| """ | ||
| self._initialization_lock = threading.Lock() | ||
| self._discovery_client = discovery_client | ||
| self._grpc_channel_pool = grpc_channel_pool | ||
| self._stub = stub_class(grpc_channel) if grpc_channel is not None else None | ||
dixonjoel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._service_interface_name = service_interface_name | ||
| self._service_class = service_class | ||
| self._stub_class = stub_class | ||
|
|
||
| def _get_stub(self) -> TStub: | ||
| if self._stub is None: | ||
| with self._initialization_lock: | ||
| if self._grpc_channel_pool is None: | ||
| _logger.debug("Creating unshared GrpcChannelPool.") | ||
| self._grpc_channel_pool = GrpcChannelPool() | ||
|
|
||
| if self._discovery_client is None: | ||
| _logger.debug("Creating unshared DiscoveryClient.") | ||
| self._discovery_client = DiscoveryClient( | ||
| grpc_channel_pool=self._grpc_channel_pool | ||
| ) | ||
|
|
||
| if self._stub is None: | ||
| compute_nodes = self._discovery_client.enumerate_compute_nodes() | ||
| remote_nodes = [node for node in compute_nodes if not node.is_local] | ||
| target_url = remote_nodes[0].url if len(remote_nodes) == 1 else "" | ||
|
|
||
| service_location = self._discovery_client.resolve_service( | ||
| provided_interface=self._service_interface_name, | ||
| deployment_target=target_url, | ||
| service_class=self._service_class, | ||
| ) | ||
|
|
||
| channel = self._grpc_channel_pool.get_channel(service_location.insecure_address) | ||
| self._stub = self._stub_class(channel) | ||
|
|
||
| return self._stub | ||
bkeryan marked this conversation as resolved.
Show resolved
Hide resolved
|
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
100 changes: 100 additions & 0 deletions
100
...onmanagement.v1.client/src/ni/measurementlink/sessionmanagement/v1/client/_client_base.py
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,100 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import threading | ||
| from typing import Generic, Protocol, TypeVar | ||
|
|
||
| import grpc | ||
| from ni.measurementlink.discovery.v1.client import DiscoveryClient | ||
| from ni_grpc_extensions.channelpool import GrpcChannelPool | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class StubProtocol(Protocol): | ||
| """Protocol for gRPC stub classes.""" | ||
|
|
||
| def __init__(self, channel: grpc.Channel) -> None: | ||
| """Initialize the gRPC client.""" | ||
|
|
||
|
|
||
| TStub = TypeVar("TStub", bound=StubProtocol) | ||
|
|
||
|
|
||
| class GrpcServiceClientBase(Generic[TStub]): | ||
| """Base class for NI gRPC service clients.""" | ||
|
|
||
| __slots__ = ( | ||
| "_initialization_lock", | ||
| "_discovery_client", | ||
| "_grpc_channel_pool", | ||
| "_stub", | ||
| "_service_interface_name", | ||
| "_service_class", | ||
| "_stub_class", | ||
| ) | ||
|
|
||
| _initialization_lock: threading.Lock | ||
| _discovery_client: DiscoveryClient | None | ||
| _grpc_channel_pool: GrpcChannelPool | None | ||
| _stub: TStub | None | ||
| _service_interface_name: str | ||
| _service_class: str | ||
| _stub_class: type[TStub] | ||
|
|
||
| def __init__( | ||
| self, | ||
| service_interface_name: str, | ||
| service_class: str, | ||
| stub_class: type[TStub], | ||
| *, | ||
| discovery_client: DiscoveryClient | None = None, | ||
| grpc_channel: grpc.Channel | None = None, | ||
| grpc_channel_pool: GrpcChannelPool | None = None, | ||
| ) -> None: | ||
| """Initialize the gRPC client. | ||
|
|
||
| Args: | ||
| service_interface_name: The fully qualified name of the service interface. | ||
| service_class: The name of the service class. | ||
| stub_class: The gRPC stub class for the service. | ||
| discovery_client: An optional discovery client (recommended). | ||
| grpc_channel: An optional pin map gRPC channel. | ||
| grpc_channel_pool: An optional gRPC channel pool (recommended). | ||
| """ | ||
| self._initialization_lock = threading.Lock() | ||
| self._discovery_client = discovery_client | ||
| self._grpc_channel_pool = grpc_channel_pool | ||
| self._stub = stub_class(grpc_channel) if grpc_channel is not None else None | ||
| self._service_interface_name = service_interface_name | ||
| self._service_class = service_class | ||
| self._stub_class = stub_class | ||
|
|
||
| def _get_stub(self) -> TStub: | ||
| if self._stub is None: | ||
| with self._initialization_lock: | ||
| if self._grpc_channel_pool is None: | ||
| _logger.debug("Creating unshared GrpcChannelPool.") | ||
| self._grpc_channel_pool = GrpcChannelPool() | ||
|
|
||
| if self._discovery_client is None: | ||
| _logger.debug("Creating unshared DiscoveryClient.") | ||
| self._discovery_client = DiscoveryClient( | ||
| grpc_channel_pool=self._grpc_channel_pool | ||
| ) | ||
|
|
||
| if self._stub is None: | ||
| compute_nodes = self._discovery_client.enumerate_compute_nodes() | ||
| remote_nodes = [node for node in compute_nodes if not node.is_local] | ||
| target_url = remote_nodes[0].url if len(remote_nodes) == 1 else "" | ||
|
|
||
| service_location = self._discovery_client.resolve_service( | ||
| provided_interface=self._service_interface_name, | ||
| deployment_target=target_url, | ||
| service_class=self._service_class, | ||
| ) | ||
|
|
||
| channel = self._grpc_channel_pool.get_channel(service_location.insecure_address) | ||
| self._stub = self._stub_class(channel) | ||
|
|
||
| return self._stub |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.