Add ability to modify call timeout on LightWalletEndpoint

This commit is contained in:
Francisco Gindre 2020-11-18 16:12:15 -03:00
parent 24837bb209
commit 879edb6236
2 changed files with 20 additions and 7 deletions

View File

@ -27,6 +27,7 @@ public struct LightWalletEndpoint {
public var host: String
public var port: Int
public var secure: Bool
public var timeout: TimeInterval
/**
initializes a LightWalletEndpoint
@ -35,10 +36,11 @@ public struct LightWalletEndpoint {
- port: string with the port of the host address
- secure: true if connecting through TLS. Default value is true
*/
public init(address: String, port: Int, secure: Bool = true) {
public init(address: String, port: Int, secure: Bool = true, timeout: TimeInterval = 10) {
self.host = address
self.port = port
self.secure = secure
self.timeout = timeout
}
}

View File

@ -13,7 +13,7 @@ import NIOHPACK
public typealias Channel = GRPC.GRPCChannel
extension TimeAmount {
static let singleCallTimeout = TimeAmount.seconds(10)
static let singleCallTimeout = TimeAmount.seconds(30)
static let streamingCallTimeout = TimeAmount.seconds(90)
}
extension CallOptions {
@ -34,20 +34,20 @@ public class LightWalletGRPCService {
let channel: Channel
let compactTxStreamer: CompactTxStreamerClient
public init(channel: Channel) {
public init(channel: Channel, timeout: TimeInterval = 10) {
self.channel = channel
compactTxStreamer = CompactTxStreamerClient(channel: self.channel, defaultCallOptions: CallOptions.lwdCall)
compactTxStreamer = CompactTxStreamerClient(channel: self.channel, defaultCallOptions: Self.defaultCallOptions(with: timeout))
}
public convenience init(endpoint: LightWalletEndpoint) {
self.init(host: endpoint.host, port: endpoint.port, secure: endpoint.secure)
}
public convenience init(host: String, port: Int = 9067, secure: Bool = true) {
public convenience init(host: String, port: Int = 9067, secure: Bool = true, timeout: TimeInterval = 10) {
let configuration = ClientConnection.Configuration(target: .hostAndPort(host, port), eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1), tls: secure ? .init() : nil)
let channel = ClientConnection(configuration: configuration)
self.init(channel: channel)
self.init(channel: channel, timeout: timeout)
}
func stop() {
@ -68,6 +68,16 @@ public class LightWalletGRPCService {
return try compactTxStreamer.getTransaction(filter).response.wait()
}
static func defaultCallOptions(with timeout: TimeInterval) -> CallOptions {
CallOptions(customMetadata: HPACKHeaders(),
timeLimit: .timeout(.singleCallTimeout),
messageEncoding: .disabled,
requestIDProvider: .autogenerated,
requestIDHeader: nil,
cacheable: false)
}
}
extension LightWalletGRPCService: LightWalletService {
@ -240,3 +250,4 @@ extension LightWalletServiceError {
}
}
}