Skip to content

Commit 800fadc

Browse files
Implement 'quit' and 'core count' RPCs on the worker service (#1833)
Motivation: These are 2 of the RPCs on the worker service that we need for benchmarking. Modifications: - Created the WorkerService struct that has a GRPCClient and a GRPCServer property - Implemented the 'coreCount' and 'quitWorker' RPCs Result: - The driver will be able to request 'quitWorker' and 'coreCount' from the workers
1 parent 62653ba commit 800fadc

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ extension Target {
250250
name: "performance-worker",
251251
dependencies: [
252252
.grpcCore,
253-
.grpcProtobuf
253+
.grpcProtobuf,
254+
.nioCore
254255
]
255256
)
256257

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 NIOConcurrencyHelpers
19+
import NIOCore
20+
21+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
22+
final class WorkerService: Grpc_Testing_WorkerService.ServiceProtocol, Sendable {
23+
private let state: NIOLockedValueBox<State>
24+
25+
init() {
26+
let clientAndServer = State()
27+
self.state = NIOLockedValueBox(clientAndServer)
28+
}
29+
30+
private struct State {
31+
var role: Role?
32+
33+
enum Role {
34+
case client(GRPCClient)
35+
case server(GRPCServer)
36+
}
37+
38+
init() {}
39+
40+
init(role: Role) {
41+
self.role = role
42+
}
43+
44+
init(server: GRPCServer) {
45+
self.role = .server(server)
46+
}
47+
48+
init(client: GRPCClient) {
49+
self.role = .client(client)
50+
}
51+
}
52+
53+
func quitWorker(
54+
request: ServerRequest.Single<Grpc_Testing_WorkerService.Method.QuitWorker.Input>
55+
) async throws -> ServerResponse.Single<Grpc_Testing_WorkerService.Method.QuitWorker.Output> {
56+
57+
let role = self.state.withLockedValue { state in
58+
defer { state.role = nil }
59+
return state.role
60+
}
61+
62+
if let role = role {
63+
switch role {
64+
case .client(let client):
65+
client.close()
66+
case .server(let server):
67+
server.stopListening()
68+
}
69+
}
70+
71+
return ServerResponse.Single(message: Grpc_Testing_WorkerService.Method.QuitWorker.Output())
72+
}
73+
74+
func coreCount(
75+
request: ServerRequest.Single<Grpc_Testing_WorkerService.Method.CoreCount.Input>
76+
) async throws -> ServerResponse.Single<Grpc_Testing_WorkerService.Method.CoreCount.Output> {
77+
let coreCount = System.coreCount
78+
return ServerResponse.Single(
79+
message: Grpc_Testing_WorkerService.Method.CoreCount.Output.with {
80+
$0.cores = Int32(coreCount)
81+
}
82+
)
83+
}
84+
85+
func runServer(
86+
request: GRPCCore.ServerRequest.Stream<Grpc_Testing_WorkerService.Method.RunServer.Input>
87+
) async throws
88+
-> GRPCCore.ServerResponse.Stream<Grpc_Testing_WorkerService.Method.RunServer.Output>
89+
{
90+
throw RPCError(code: .unimplemented, message: "This RPC has not been implemented yet.")
91+
}
92+
93+
func runClient(
94+
request: GRPCCore.ServerRequest.Stream<Grpc_Testing_WorkerService.Method.RunClient.Input>
95+
) async throws
96+
-> GRPCCore.ServerResponse.Stream<Grpc_Testing_WorkerService.Method.RunClient.Output>
97+
{
98+
throw RPCError(code: .unimplemented, message: "This RPC has not been implemented yet.")
99+
}
100+
}

0 commit comments

Comments
 (0)