From 78a20cbadee2a76fe29140bb9eae61768e2b43cf Mon Sep 17 00:00:00 2001 From: George Barnett Date: Wed, 3 Apr 2024 15:03:05 +0100 Subject: [PATCH] Add wait-for-ready to method config (#1850) Motivation: The wait-for-ready property was missing from method config. Modifications: - Add wait-for-ready property and parsing Result: wait-for-ready is available in method config --- .../Configuration/MethodConfiguration.swift | 13 +++++++++++++ .../MethodConfigurationCodingTests.swift | 3 +++ 2 files changed, 16 insertions(+) diff --git a/Sources/GRPCCore/Configuration/MethodConfiguration.swift b/Sources/GRPCCore/Configuration/MethodConfiguration.swift index 0e0af5d1b..f34211abc 100644 --- a/Sources/GRPCCore/Configuration/MethodConfiguration.swift +++ b/Sources/GRPCCore/Configuration/MethodConfiguration.swift @@ -64,6 +64,12 @@ public struct MethodConfiguration: Hashable, Sendable { /// The names of methods which this configuration applies to. public var names: [Name] + /// Whether RPCs for this method should wait until the connection is ready. + /// + /// If `false` the RPC will abort immediately if there is a transient failure connecting to + /// the server. Otherwise gRPC will attempt to connect until the deadline is exceeded. + public var waitForReady: Bool? + /// The default timeout for the RPC. /// /// If no reply is received in the specified amount of time the request is aborted @@ -112,18 +118,21 @@ public struct MethodConfiguration: Hashable, Sendable { /// /// - Parameters: /// - names: The names of methods this configuration applies to. + /// - waitForReady: Whether RPCs sent to this method should wait until the connection is ready. /// - timeout: The default timeout for the RPC. /// - maxRequestMessageBytes: The maximum allowed size of a request message in bytes. /// - maxResponseMessageBytes: The maximum allowed size of a response message in bytes. /// - executionPolicy: The execution policy to use for the RPC. public init( names: [Name], + waitForReady: Bool? = nil, timeout: Duration? = nil, maxRequestMessageBytes: Int? = nil, maxResponseMessageBytes: Int? = nil, executionPolicy: ExecutionPolicy? = nil ) { self.names = names + self.waitForReady = waitForReady self.timeout = timeout self.maxRequestMessageBytes = maxRequestMessageBytes self.maxResponseMessageBytes = maxResponseMessageBytes @@ -367,6 +376,7 @@ extension Duration { extension MethodConfiguration: Codable { private enum CodingKeys: String, CodingKey { case name + case waitForReady case timeout case maxRequestMessageBytes case maxResponseMessageBytes @@ -378,6 +388,9 @@ extension MethodConfiguration: Codable { let container = try decoder.container(keyedBy: CodingKeys.self) self.names = try container.decode([Name].self, forKey: .name) + let waitForReady = try container.decodeIfPresent(Bool.self, forKey: .waitForReady) + self.waitForReady = waitForReady + let timeout = try container.decodeIfPresent(GoogleProtobufDuration.self, forKey: .timeout) self.timeout = timeout?.duration diff --git a/Tests/GRPCCoreTests/Configuration/MethodConfigurationCodingTests.swift b/Tests/GRPCCoreTests/Configuration/MethodConfigurationCodingTests.swift index a9365909c..6fb7f17ea 100644 --- a/Tests/GRPCCoreTests/Configuration/MethodConfigurationCodingTests.swift +++ b/Tests/GRPCCoreTests/Configuration/MethodConfigurationCodingTests.swift @@ -350,6 +350,8 @@ internal final class MethodConfigurationCodingTests: XCTestCase { } ] + $0.waitForReady = true + $0.timeout = .with { $0.seconds = 1 $0.nanos = 0 @@ -364,6 +366,7 @@ internal final class MethodConfigurationCodingTests: XCTestCase { let jsonConfig = try config.jsonUTF8Data() let decoded = try self.decoder.decode(MethodConfiguration.self, from: jsonConfig) XCTAssertEqual(decoded.names, [MethodConfiguration.Name(service: "echo.Echo", method: "Get")]) + XCTAssertEqual(decoded.waitForReady, true) XCTAssertEqual(decoded.timeout, Duration(secondsComponent: 1, attosecondsComponent: 0)) XCTAssertEqual(decoded.maxRequestMessageBytes, 1024) XCTAssertEqual(decoded.maxResponseMessageBytes, 2048)