Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ let products: [Product] = [
name: "GRPCInterceptors",
targets: ["GRPCInterceptors"]
),
.library(
name: "GRPCServiceLifecycle",
targets: ["GRPCServiceLifecycle"]
),
.library(
name: "GRPCInteropTests",
targets: ["GRPCInteropTests"]
Expand All @@ -39,7 +43,7 @@ let products: [Product] = [
let dependencies: [Package.Dependency] = [
.package(
url: "https://github.com/grpc/grpc-swift.git",
exact: "2.0.0-beta.3"
branch: "main"
),
.package(
url: "https://github.com/grpc/grpc-swift-protobuf.git",
Expand All @@ -53,6 +57,10 @@ let dependencies: [Package.Dependency] = [
url: "https://github.com/apple/swift-distributed-tracing.git",
from: "1.1.2"
),
.package(
url: "https://github.com/swift-server/swift-service-lifecycle.git",
from: "2.6.3"
),
]

let defaultSwiftSettings: [SwiftSetting] = [
Expand Down Expand Up @@ -126,6 +134,26 @@ let targets: [Target] = [
swiftSettings: defaultSwiftSettings
),

// Retroactive conformances of gRPC client and server to swift-server-lifecycle's Service.
.target(
name: "GRPCServiceLifecycle",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great if you could add a documentation bundle for this too (and update .spi.yml). That can be a follow up though if you prefer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, looks like none of the modules other than the Health service actually have a doc bundle so maybe we should do that separately for all?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me

dependencies: [
.product(name: "GRPCCore", package: "grpc-swift"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
],
swiftSettings: defaultSwiftSettings
),
.testTarget(
name: "GRPCServiceLifecycleTests",
dependencies: [
.target(name: "GRPCServiceLifecycle"),
.product(name: "GRPCCore", package: "grpc-swift"),
.product(name: "ServiceLifecycleTestKit", package: "swift-service-lifecycle"),
.product(name: "GRPCInProcessTransport", package: "grpc-swift"),
],
swiftSettings: defaultSwiftSettings
),

// gRPC interop test implementation.
.target(
name: "GRPCInteropTests",
Expand Down
28 changes: 28 additions & 0 deletions Sources/GRPCServiceLifecycle/GRPCClient+Service.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2025, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public import GRPCCore
public import ServiceLifecycle

extension GRPCClient: @retroactive Service {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you drop a comment in why @retroactive is okay here? (and for the server)

public func run() async throws {
try await withGracefulShutdownHandler {
try await self.runConnections()
} onGracefulShutdown: {
self.beginGracefulShutdown()
}
}
}
28 changes: 28 additions & 0 deletions Sources/GRPCServiceLifecycle/GRPCServer+Service.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2025, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public import GRPCCore
public import ServiceLifecycle

extension GRPCServer: @retroactive Service {
public func run() async throws {
try await withGracefulShutdownHandler {
try await self.serve()
} onGracefulShutdown: {
self.beginGracefulShutdown()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2025, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import GRPCCore
import GRPCInProcessTransport
import GRPCServiceLifecycle
import ServiceLifecycleTestKit
import Testing

@Suite("gRPC ServiceLifecycle/Service conformance tests")
struct ServiceLifecycleConformanceTests {
@Test("Client respects graceful shutdown")
func clientGracefulShutdown() async throws {
let inProcess = InProcessTransport()
try await testGracefulShutdown { trigger in
try await withThrowingDiscardingTaskGroup { group in
group.addTask {
let client = GRPCClient(transport: inProcess.client)
try await client.run()
}

group.addTask {
try await Task.sleep(for: .milliseconds(10))
trigger.triggerGracefulShutdown()
}
}
}
}

@Test("Server respects graceful shutdown")
func serverGracefulShutdown() async throws {
let inProcess = InProcessTransport()
try await testGracefulShutdown { trigger in
try await withThrowingDiscardingTaskGroup { group in
group.addTask {
let server = GRPCServer(transport: inProcess.server, services: [])
try await server.run()
}

group.addTask {
try await Task.sleep(for: .milliseconds(10))
trigger.triggerGracefulShutdown()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class InProcessInteroperabilityTests: XCTestCase {
try await withThrowingTaskGroup(of: Void.self) { clientGroup in
let client = GRPCClient(transport: inProcess.client)
clientGroup.addTask {
try await client.run()
try await client.runConnections()
}
try await interopTestCase.makeTest().run(client: client)

Expand Down
Loading