From 4b67055fc764f3d8bc5ff1f9391bc7ca02506aea Mon Sep 17 00:00:00 2001 From: flemming-it Date: Tue, 5 May 2026 22:51:19 +0200 Subject: [PATCH] refactor: share single ClientChannel between Hub and HubAdmin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Initial scaffold accidentally created three separate gRPC channels (one for each of `_channel`, `_hub`, `_admin`). Refactor to a single shared channel — cleaner, fewer file descriptors, and `close()` actually closes everything now. Idle timeout raised to 5 minutes so polling-style page loops don't keep reconnecting. No public-API change. Signed-off-by: flemming-it --- lib/src/hub_client.dart | 49 ++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/lib/src/hub_client.dart b/lib/src/hub_client.dart index 0f06397..b8173c5 100644 --- a/lib/src/hub_client.dart +++ b/lib/src/hub_client.dart @@ -50,38 +50,23 @@ class HubClient { final grpc_hub.HubClient _hub; final grpc_hub.HubAdminClient _admin; - HubClient({this.endpoint = const HubEndpoint()}) - : _channel = ClientChannel( - endpoint.host, - port: endpoint.port, - options: ChannelOptions( - credentials: endpoint.secure - ? const ChannelCredentials.secure() - : const ChannelCredentials.insecure(), - ), - ), - _hub = grpc_hub.HubClient( - ClientChannel( - endpoint.host, - port: endpoint.port, - options: ChannelOptions( - credentials: endpoint.secure - ? const ChannelCredentials.secure() - : const ChannelCredentials.insecure(), - ), - ), - ), - _admin = grpc_hub.HubAdminClient( - ClientChannel( - endpoint.host, - port: endpoint.port, - options: ChannelOptions( - credentials: endpoint.secure - ? const ChannelCredentials.secure() - : const ChannelCredentials.insecure(), - ), - ), - ); + HubClient._(this.endpoint, this._channel) + : _hub = grpc_hub.HubClient(_channel), + _admin = grpc_hub.HubAdminClient(_channel); + + factory HubClient({HubEndpoint endpoint = const HubEndpoint()}) { + final channel = ClientChannel( + endpoint.host, + port: endpoint.port, + options: ChannelOptions( + credentials: endpoint.secure + ? const ChannelCredentials.secure() + : const ChannelCredentials.insecure(), + idleTimeout: const Duration(minutes: 5), + ), + ); + return HubClient._(endpoint, channel); + } /// Liveness probe. Returns true when the hub responds with /// SERVING. False on any failure (includes connection refused).