Skip to content

Commit 7af7a9b

Browse files
committed
Revert the reuse of the discovery client
Signed-off-by: Joe Friedrichsen <[email protected]>
1 parent 760df05 commit 7af7a9b

File tree

4 files changed

+21
-64
lines changed

4 files changed

+21
-64
lines changed

src/nipanel/_panel_client.py

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
SetValueRequest,
1515
)
1616
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub
17-
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient, ServiceLocation
17+
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient
1818
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool
1919
from typing_extensions import ParamSpec
2020

@@ -45,14 +45,8 @@ def __init__(
4545
self._discovery_client = discovery_client
4646
self._grpc_channel_pool = grpc_channel_pool
4747
self._grpc_channel = grpc_channel
48-
self._proxy_location: ServiceLocation | None = None
4948
self._stub: PythonPanelServiceStub | None = None
5049

51-
@property
52-
def proxy_location(self) -> ServiceLocation:
53-
"""Return the ServiceLocation for the panel's proxy."""
54-
return self._get_proxy_location()
55-
5650
def start_panel(self, panel_id: str, panel_script_path: str, python_path: str) -> str:
5751
start_panel_request = StartPanelRequest(
5852
panel_id=panel_id, panel_script_path=panel_script_path, python_path=python_path
@@ -93,61 +87,29 @@ def try_get_value(self, panel_id: str, value_id: str) -> object | None:
9387
else:
9488
return None
9589

96-
def _get_proxy_location(self) -> ServiceLocation:
97-
if self._proxy_location is None:
98-
self._proxy_location = self._resolve_discovery_service()
99-
return self._proxy_location
100-
10190
def _get_stub(self) -> PythonPanelServiceStub:
10291
if self._stub is None:
10392
if self._grpc_channel is not None:
10493
self._stub = PythonPanelServiceStub(self._grpc_channel)
10594
else:
106-
channel = self._get_panel_service_channel()
107-
self._stub = PythonPanelServiceStub(channel)
95+
with self._initialization_lock:
96+
if self._grpc_channel_pool is None:
97+
_logger.debug("Creating unshared GrpcChannelPool.")
98+
self._grpc_channel_pool = GrpcChannelPool()
99+
if self._discovery_client is None:
100+
_logger.debug("Creating unshared DiscoveryClient.")
101+
self._discovery_client = DiscoveryClient(
102+
grpc_channel_pool=self._grpc_channel_pool
103+
)
104+
105+
service_location = self._discovery_client.resolve_service(
106+
provided_interface=self._provided_interface,
107+
service_class=self._service_class,
108+
)
109+
channel = self._grpc_channel_pool.get_channel(service_location.insecure_address)
110+
self._stub = PythonPanelServiceStub(channel)
108111
return self._stub
109112

110-
def _get_grpc_channel_pool(self) -> GrpcChannelPool:
111-
if self._grpc_channel_pool is None:
112-
_logger.debug("Creating unshared GrpcChannelPool.")
113-
self._grpc_channel_pool = GrpcChannelPool()
114-
return self._grpc_channel_pool
115-
116-
def _get_discovery_client(self, channel_pool: GrpcChannelPool) -> DiscoveryClient:
117-
if self._discovery_client is None:
118-
_logger.debug("Creating unshared DiscoveryClient.")
119-
self._discovery_client = DiscoveryClient(grpc_channel_pool=channel_pool)
120-
return self._discovery_client
121-
122-
def _resolve_service(
123-
self, discovery_client: DiscoveryClient, provided_interface: str, service_class: str = ""
124-
) -> ServiceLocation:
125-
_logger.debug("Resolving '%s'.", provided_interface)
126-
service_location = discovery_client.resolve_service(
127-
provided_interface=provided_interface,
128-
service_class=service_class,
129-
)
130-
return service_location
131-
132-
def _get_panel_service_channel(self) -> grpc.Channel:
133-
with self._initialization_lock:
134-
grpc_channel_pool = self._get_grpc_channel_pool()
135-
discovery_client = self._get_discovery_client(grpc_channel_pool)
136-
panel_location = self._resolve_service(
137-
discovery_client, self._provided_interface, self._service_class
138-
)
139-
channel = grpc_channel_pool.get_channel(panel_location.insecure_address)
140-
return channel
141-
142-
def _resolve_discovery_service(self) -> ServiceLocation:
143-
with self._initialization_lock:
144-
grpc_channel_pool = self._get_grpc_channel_pool()
145-
discovery_client = self._get_discovery_client(grpc_channel_pool)
146-
discovery_location = self._resolve_service(
147-
discovery_client, provided_interface="ni.http1.proxy"
148-
)
149-
return discovery_location
150-
151113
def _invoke_with_retry(
152114
self, method: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs
153115
) -> _T:

src/nipanel/_panel_value_accessor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import TypeVar, overload
77

88
import grpc
9-
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient, ServiceLocation
9+
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient
1010
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool
1111

1212
from nipanel._panel_client import _PanelClient
@@ -55,11 +55,6 @@ def panel_id(self) -> str:
5555
"""Read-only accessor for the panel ID."""
5656
return self._panel_id
5757

58-
@property
59-
def proxy_location(self) -> ServiceLocation:
60-
"""Read-only accessor for the panel's proxy location."""
61-
return self._panel_client.proxy_location
62-
6358
@overload
6459
def get_value(self, value_id: str) -> object: ...
6560

src/nipanel/_streamlit_panel_initializer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def get_panel_accessor() -> StreamlitPanelValueAccessor:
6363

6464
panel = cast(StreamlitPanelValueAccessor, st.session_state[PANEL_ACCESSOR_KEY])
6565
_sync_session_state(panel)
66-
proxy_url = panel.proxy_location.insecure_address
67-
refresh_component = initialize_refresh_component(proxy_url, panel.panel_id)
66+
refresh_component = initialize_refresh_component(panel.panel_id)
6867
refresh_component()
6968
return panel
7069

src/nipanel/streamlit_refresh/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from streamlit.components.v1.custom_component import CustomComponent
55

66

7-
def initialize_refresh_component(proxy_url: str, panel_id: str) -> CustomComponent:
7+
def initialize_refresh_component(panel_id: str) -> CustomComponent:
88
"""Initialize a refresh component to the Streamlit app."""
9+
proxy_url = "TODO"
910
_refresh_component_func = declare_component(
1011
"panelRefreshComponent",
1112
url=f"http://{proxy_url}/panel-service/refresh/{panel_id}",

0 commit comments

Comments
 (0)