[#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
This commit is contained in:
Francisco Gindre 2022-07-08 16:49:24 -03:00 committed by GitHub
parent acae6a0d7b
commit 79dbe8f387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 15 deletions

View File

@ -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

View File

@ -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
}
}