From 79dbe8f387187caf552c84e5922b0d4a1669e272 Mon Sep 17 00:00:00 2001 From: Francisco Gindre Date: Fri, 8 Jul 2022 16:49:24 -0300 Subject: [PATCH] [#404] Configure GRPC KeepAlive according to docs (#409) Closes #404 This commit updates LightWalletGRPCService conform to latest Swift-GRPC recommendations on how to set Connections KeepAlive parameters. See https://github.com/grpc/grpc-swift/blob/main/docs/keepalive.md --- .../Service/LightWalletGRPCService.swift | 21 ++++++++++++------- Tests/TestUtils/Tests+Utils.swift | 21 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Sources/ZcashLightClientKit/Service/LightWalletGRPCService.swift b/Sources/ZcashLightClientKit/Service/LightWalletGRPCService.swift index dacd8e71..7b8e72f4 100644 --- a/Sources/ZcashLightClientKit/Service/LightWalletGRPCService.swift +++ b/Sources/ZcashLightClientKit/Service/LightWalletGRPCService.swift @@ -112,14 +112,19 @@ public class LightWalletGRPCService { self.streamingCallTimeout = TimeLimit.timeout(.milliseconds(streamingCallTimeout)) self.singleCallTimeout = TimeLimit.timeout(.milliseconds(singleCallTimeout)) - let configuration = ClientConnection.Configuration( - target: .hostAndPort(host, port), - eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1), - connectivityStateDelegate: connectionManager, - connectivityStateDelegateQueue: queue, - tls: secure ? .init() : nil - ) - let channel = ClientConnection(configuration: configuration) + let connectionBuilder = secure ? + ClientConnection.usingPlatformAppropriateTLS(for: MultiThreadedEventLoopGroup(numberOfThreads: 1)) : + ClientConnection.insecure(group: MultiThreadedEventLoopGroup(numberOfThreads: 1)) + + let channel = connectionBuilder + .withConnectivityStateDelegate(connectionManager, executingOn: queue) + .withKeepalive( + ClientConnectionKeepalive( + interval: .seconds(15), + timeout: .seconds(10) + ) + ) + .connect(host: host, port: port) self.channel = channel diff --git a/Tests/TestUtils/Tests+Utils.swift b/Tests/TestUtils/Tests+Utils.swift index e42d9ba6..4d110bc1 100644 --- a/Tests/TestUtils/Tests+Utils.swift +++ b/Tests/TestUtils/Tests+Utils.swift @@ -38,14 +38,21 @@ enum LightWalletEndpointBuilder { class ChannelProvider { func channel(secure: Bool = false) -> GRPCChannel { let endpoint = LightWalletEndpointBuilder.default - - let configuration = ClientConnection.Configuration( - target: .hostAndPort(endpoint.host, endpoint.port), - eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1), - tls: secure ? .init() : nil - ) - return ClientConnection(configuration: configuration) + let connectionBuilder = secure ? + ClientConnection.usingPlatformAppropriateTLS(for: MultiThreadedEventLoopGroup(numberOfThreads: 1)) : + ClientConnection.insecure(group: MultiThreadedEventLoopGroup(numberOfThreads: 1)) + + let channel = connectionBuilder + .withKeepalive( + ClientConnectionKeepalive( + interval: .seconds(15), + timeout: .seconds(10) + ) + ) + .connect(host: endpoint.host, port: endpoint.port) + + return channel } }