Skip to content

Commit 94fce5f

Browse files
committed
Add isolation
1 parent 274e9af commit 94fce5f

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Sources/GRPCCore/GRPCClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,13 @@ public final class GRPCClient: Sendable {
386386
public func withGRPCClient<Result: Sendable>(
387387
transport: some ClientTransport,
388388
interceptors: [any ClientInterceptor] = [],
389+
isolation: isolated (any Actor)? = #isolation,
389390
handleClient: (GRPCClient) async throws -> Result
390391
) async throws -> Result {
391392
try await withGRPCClient(
392393
transport: transport,
393394
interceptorPipeline: interceptors.map { .apply($0, to: .all) },
395+
isolation: isolation,
394396
handleClient: handleClient
395397
)
396398
}
@@ -410,6 +412,7 @@ public func withGRPCClient<Result: Sendable>(
410412
public func withGRPCClient<Result: Sendable>(
411413
transport: some ClientTransport,
412414
interceptorPipeline: [ClientInterceptorPipelineOperation],
415+
isolation: isolated (any Actor)? = #isolation,
413416
handleClient: (GRPCClient) async throws -> Result
414417
) async throws -> Result {
415418
try await withThrowingDiscardingTaskGroup { group in

Sources/GRPCCore/GRPCServer.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,14 @@ public func withGRPCServer<Result: Sendable>(
258258
transport: any ServerTransport,
259259
services: [any RegistrableRPCService],
260260
interceptors: [any ServerInterceptor] = [],
261+
isolation: isolated (any Actor)? = #isolation,
261262
handleServer: (GRPCServer) async throws -> Result
262263
) async throws -> Result {
263264
try await withGRPCServer(
264265
transport: transport,
265266
services: services,
266267
interceptorPipeline: interceptors.map { .apply($0, to: .all) },
268+
isolation: isolation,
267269
handleServer: handleServer
268270
)
269271
}
@@ -285,6 +287,7 @@ public func withGRPCServer<Result: Sendable>(
285287
transport: any ServerTransport,
286288
services: [any RegistrableRPCService],
287289
interceptorPipeline: [ServerInterceptorPipelineOperation],
290+
isolation: isolated (any Actor)? = #isolation,
288291
handleServer: (GRPCServer) async throws -> Result
289292
) async throws -> Result {
290293
return try await withThrowingDiscardingTaskGroup { group in
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import GRPCCore
18+
import GRPCInProcessTransport
19+
import Testing
20+
21+
@Suite("withGRPCServer / withGRPCClient")
22+
struct WithMethods {
23+
@Test("Actor isolation")
24+
func actorIsolation() async throws {
25+
let testActor = TestActor()
26+
#expect(await !testActor.hasRun)
27+
try await testActor.run()
28+
#expect(await testActor.hasRun)
29+
}
30+
}
31+
32+
fileprivate actor TestActor {
33+
private(set) var hasRun = false
34+
35+
func run() async throws {
36+
let inProcess = InProcessTransport()
37+
38+
try await withGRPCServer(transport: inProcess.server, services: []) { server in
39+
try await withGRPCClient(transport: inProcess.client) { client in
40+
self.hasRun = true
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)