Skip to content

Commit eafb334

Browse files
authored
Rename 'GRPCClient.run()' (#2156)
Motivation: To support swift-service-lifecycle the grpc client and server types will conform to its 'Service' protocol. This has a requirement for a method called 'run()'. Ideally this would respect graceful shutdown. We can't do this with the current client as we already have a run method. To do this we'd need to wrap the type and redeclare all of it's methods. Using a wrapper isn't a good user experience. Modifications: - Rename run to 'maintainConnections()' - Deprecate 'run()', we'll remove this later Result: - run is deprecated
1 parent a92a58b commit eafb334

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

Sources/GRPCCore/GRPCClient.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ private import Synchronization
4545
/// ## Creating a client manually
4646
///
4747
/// If the `with`-style methods for creating clients isn't suitable for your application then you
48-
/// can create and run a client manually. This requires you to call the ``run()`` method in a task
48+
/// can create and run a client manually. This requires you to call the ``runConnections()`` method in a task
4949
/// which instructs the client to start connecting to the server.
5050
///
51-
/// The ``run()`` method won't return until the client has finished handling all requests. You can
51+
/// The ``runConnections()`` method won't return until the client has finished handling all requests. You can
5252
/// signal to the client that it should stop creating new request streams by calling ``beginGracefulShutdown()``.
5353
/// This gives the client enough time to drain any requests already in flight. To stop the client
5454
/// more abruptly you can cancel the task running your client. If your application requires
@@ -114,7 +114,7 @@ public final class GRPCClient: Sendable {
114114
func checkExecutable() throws {
115115
switch self {
116116
case .notStarted, .running:
117-
// Allow .notStarted as making a request can race with 'run()'. Transports should tolerate
117+
// Allow .notStarted as making a request can race with 'runConnections()'. Transports should tolerate
118118
// queuing the request if not yet started.
119119
()
120120
case .stopping, .stopped:
@@ -208,7 +208,7 @@ public final class GRPCClient: Sendable {
208208
///
209209
/// The client, and by extension this function, can only be run once. If the client is already
210210
/// running or has already been closed then a ``RuntimeError`` is thrown.
211-
public func run() async throws {
211+
public func runConnections() async throws {
212212
try self.stateMachine.withLock { try $0.state.run() }
213213

214214
// When this function exits the client must have stopped.
@@ -227,6 +227,11 @@ public final class GRPCClient: Sendable {
227227
}
228228
}
229229

230+
@available(*, deprecated, renamed: "runConnections", message: "It'll be removed before v2.")
231+
public func run() async throws {
232+
try await self.runConnections()
233+
}
234+
230235
/// Close the client.
231236
///
232237
/// The transport will be closed: this means that it will be given enough time to wait for
@@ -338,7 +343,7 @@ public final class GRPCClient: Sendable {
338343

339344
/// Start a bidirectional streaming RPC.
340345
///
341-
/// - Note: ``run()`` must have been called and still executing, and ``beginGracefulShutdown()`` mustn't
346+
/// - Note: ``runConnections()`` must have been called and still executing, and ``beginGracefulShutdown()`` mustn't
342347
/// have been called.
343348
///
344349
/// - Parameters:
@@ -430,7 +435,7 @@ public func withGRPCClient<Result: Sendable>(
430435
try await withThrowingDiscardingTaskGroup { group in
431436
let client = GRPCClient(transport: transport, interceptorPipeline: interceptorPipeline)
432437
group.addTask {
433-
try await client.run()
438+
try await client.runConnections()
434439
}
435440

436441
let result = try await handleClient(client)

Tests/GRPCCoreTests/GRPCClientTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ final class GRPCClientTests: XCTestCase {
336336
}
337337

338338
group.addTask {
339-
try await client.run()
339+
try await client.runConnections()
340340
}
341341

342342
// Wait for client and server to be running.
@@ -377,13 +377,13 @@ final class GRPCClientTests: XCTestCase {
377377
let inProcess = InProcessTransport()
378378
let client = GRPCClient(transport: inProcess.client)
379379
// Run the client.
380-
let task = Task { try await client.run() }
380+
let task = Task { try await client.runConnections() }
381381
task.cancel()
382382
try await task.value
383383

384384
// Client is stopped, should throw an error.
385385
await XCTAssertThrowsErrorAsync(ofType: RuntimeError.self) {
386-
try await client.run()
386+
try await client.runConnections()
387387
} errorHandler: { error in
388388
XCTAssertEqual(error.code, .clientIsStopped)
389389
}
@@ -393,13 +393,13 @@ final class GRPCClientTests: XCTestCase {
393393
let inProcess = InProcessTransport()
394394
let client = GRPCClient(transport: inProcess.client)
395395
// Run the client.
396-
let task = Task { try await client.run() }
396+
let task = Task { try await client.runConnections() }
397397
// Make sure the client is run for the first time here.
398398
try await Task.sleep(for: .milliseconds(10))
399399

400400
// Client is already running, should throw an error.
401401
await XCTAssertThrowsErrorAsync(ofType: RuntimeError.self) {
402-
try await client.run()
402+
try await client.runConnections()
403403
} errorHandler: { error in
404404
XCTAssertEqual(error.code, .clientIsAlreadyRunning)
405405
}
@@ -551,7 +551,7 @@ struct ClientTests {
551551
}
552552

553553
group.addTask {
554-
try await client.run()
554+
try await client.runConnections()
555555
}
556556

557557
// Make sure both server and client are running

Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct InProcessTransportTests {
3535

3636
let client = GRPCClient(transport: inProcess.client)
3737
group.addTask {
38-
try await client.run()
38+
try await client.runConnections()
3939
}
4040

4141
try await execute(server, client)
@@ -60,7 +60,7 @@ struct InProcessTransportTests {
6060
#expect(messages == ["isCancelled=true"])
6161
}
6262

63-
// Finally, shutdown the client so its run() method returns.
63+
// Finally, shutdown the client so its runConnections() method returns.
6464
client.beginGracefulShutdown()
6565
}
6666
}

0 commit comments

Comments
 (0)