Skip to content

Commit 183e380

Browse files
authored
Add back GRPCPayload support for server handlers (#894)
Motivation: Recent work in #886 and #889 made `GRPCProtobufPayload` redundant. Since we broke this work into multiple PRs we temporarily dropped support for custom `GRPCPayload`s on the server. This PR adds that back. Modifications: - Add `GRPCPayload` support back to the server by adding the relevant call handler factory functions - Re-enable the custom payload tests - Add a few more custom payload tests (since they were limited to just bidirectional streaming) Result: - Clients and servers support `SwiftProtobuf.Message` and `GRPCPayload` separately without using `GRPCProtobufPayload` to bridge between them.
1 parent 6fccc59 commit 183e380

File tree

3 files changed

+367
-199
lines changed

3 files changed

+367
-199
lines changed

Sources/GRPC/CallHandlers/CallHandlerFactory.swift

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
* Copyright 2020, 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-
*/
2+
* Copyright 2020, 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+
*/
1616
import NIO
1717
import SwiftProtobuf
1818

@@ -34,6 +34,18 @@ public enum CallHandlerFactory {
3434
)
3535
}
3636

37+
public static func makeUnary<Request: GRPCPayload, Response: GRPCPayload>(
38+
callHandlerContext: CallHandlerContext,
39+
eventObserverFactory: @escaping (UnaryContext<Response>) -> UnaryEventObserver<Request, Response>
40+
) -> UnaryCallHandler<Request, Response> {
41+
return UnaryCallHandler(
42+
serializer: GRPCPayloadSerializer(),
43+
deserializer: GRPCPayloadDeserializer(),
44+
callHandlerContext: callHandlerContext,
45+
eventObserverFactory: eventObserverFactory
46+
)
47+
}
48+
3749
public typealias ClientStreamingContext<Response> = UnaryResponseCallContext<Response>
3850
public typealias ClientStreamingEventObserver<Request> = EventLoopFuture<(StreamEvent<Request>) -> Void>
3951

@@ -49,6 +61,18 @@ public enum CallHandlerFactory {
4961
)
5062
}
5163

64+
public static func makeClientStreaming<Request: GRPCPayload, Response: GRPCPayload>(
65+
callHandlerContext: CallHandlerContext,
66+
eventObserverFactory: @escaping (ClientStreamingContext<Response>) -> ClientStreamingEventObserver<Request>
67+
) -> ClientStreamingCallHandler<Request, Response> {
68+
return ClientStreamingCallHandler(
69+
serializer: GRPCPayloadSerializer(),
70+
deserializer: GRPCPayloadDeserializer(),
71+
callHandlerContext: callHandlerContext,
72+
eventObserverFactory: eventObserverFactory
73+
)
74+
}
75+
5276
public typealias ServerStreamingContext<Response> = StreamingResponseCallContext<Response>
5377
public typealias ServerStreamingEventObserver<Request> = (Request) -> EventLoopFuture<GRPCStatus>
5478

@@ -64,6 +88,18 @@ public enum CallHandlerFactory {
6488
)
6589
}
6690

91+
public static func makeServerStreaming<Request: GRPCPayload, Response: GRPCPayload>(
92+
callHandlerContext: CallHandlerContext,
93+
eventObserverFactory: @escaping (ServerStreamingContext<Response>) -> ServerStreamingEventObserver<Request>
94+
) -> ServerStreamingCallHandler<Request, Response> {
95+
return ServerStreamingCallHandler(
96+
serializer: GRPCPayloadSerializer(),
97+
deserializer: GRPCPayloadDeserializer(),
98+
callHandlerContext: callHandlerContext,
99+
eventObserverFactory: eventObserverFactory
100+
)
101+
}
102+
67103
public typealias BidirectionalStreamingContext<Response> = StreamingResponseCallContext<Response>
68104
public typealias BidirectionalStreamingEventObserver<Request> = EventLoopFuture<(StreamEvent<Request>) -> Void>
69105

@@ -78,4 +114,16 @@ public enum CallHandlerFactory {
78114
eventObserverFactory: eventObserverFactory
79115
)
80116
}
117+
118+
public static func makeBidirectionalStreaming<Request: GRPCPayload, Response: GRPCPayload>(
119+
callHandlerContext: CallHandlerContext,
120+
eventObserverFactory: @escaping (BidirectionalStreamingContext<Response>) -> BidirectionalStreamingEventObserver<Request>
121+
) -> BidirectionalStreamingCallHandler<Request, Response> {
122+
return BidirectionalStreamingCallHandler(
123+
serializer: GRPCPayloadSerializer(),
124+
deserializer: GRPCPayloadDeserializer(),
125+
callHandlerContext: callHandlerContext,
126+
eventObserverFactory: eventObserverFactory
127+
)
128+
}
81129
}

0 commit comments

Comments
 (0)