Skip to content

Commit c7f5f78

Browse files
Mike ProsserMike Prosser
authored andcommitted
use a channel pool
1 parent a7e4914 commit c7f5f78

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/nipanel/_panel.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from types import TracebackType
66
from typing import TYPE_CHECKING, Optional, Type
77

8-
from grpc import RpcError, StatusCode, insecure_channel
8+
from grpc import RpcError, StatusCode
99
from ni.pythonpanel.v1.python_panel_service_pb2 import ConnectRequest, DisconnectRequest
1010
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub
11+
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool
1112

1213
from nipanel._panel_not_found_error import PanelNotFoundError
1314

@@ -21,14 +22,16 @@
2122
class Panel(ABC):
2223
"""This class allows you to connect to a panel and specify values for its controls."""
2324

25+
_channel_pool: GrpcChannelPool
2426
_stub: PythonPanelServiceStub | None
2527
_panel_id: str
2628
_panel_uri: str
2729

28-
__slots__ = ["_stub", "_panel_id", "_panel_uri", "__weakref__"]
30+
__slots__ = ["_channel_pool", "_stub", "_panel_id", "_panel_uri", "__weakref__"]
2931

3032
def __init__(self, panel_id: str, panel_uri: str) -> None:
3133
"""Initialize the panel."""
34+
self._channel_pool = GrpcChannelPool()
3235
self._panel_id = panel_id
3336
self._panel_uri = panel_uri
3437

@@ -59,8 +62,8 @@ def __exit__(
5962

6063
def connect(self) -> None:
6164
"""Connect to the panel and open it."""
62-
# TODO: use the channel pool
63-
channel = insecure_channel(self._resolve_service_address())
65+
address = self._resolve_service_address()
66+
channel = self._channel_pool.get_channel(address)
6467
self._stub = PythonPanelServiceStub(channel)
6568
connect_request = ConnectRequest(panel_id=self._panel_id, panel_uri=self._panel_uri)
6669

@@ -88,7 +91,7 @@ def disconnect(self) -> None:
8891
raise
8992

9093
self._stub = None
91-
# TODO: channel pool cleanup?
94+
self._channel_pool.close()
9295

9396
def get_value(self, value_id: str) -> object:
9497
"""Get the value for a control on the panel.

tests/utils/_port_panel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33

44
class PortPanel(Panel):
5-
"""This class allows you to connect to the PythonPanelService with a specified port, for testing."""
5+
"""This class allows you to connect to the PythonPanelService with a specified port."""
66

77
def __init__(self, port: int, panel_id: str, panel_uri: str) -> None:
8-
"""Create a panel and connect to a specified port, for testing.
8+
"""Create a panel and connect to a specified port.
99
1010
Args:
1111
port: The port number for the gRPC server.
1212
panel_id: A unique identifier for the panel.
1313
panel_uri: The file path of the panel script.
1414
1515
Returns:
16-
A new FakePanel instance.
16+
A new PortPanel instance.
1717
"""
1818
super().__init__(panel_id, panel_uri)
1919
self.port = port

0 commit comments

Comments
 (0)