Skip to content

Commit

Permalink
Add back GRPCPayload support for server handlers (#894)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glbrntt authored Jul 15, 2020
1 parent 6fccc59 commit 183e380
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 199 deletions.
76 changes: 62 additions & 14 deletions Sources/GRPC/CallHandlers/CallHandlerFactory.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright 2020, 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.
*/
* Copyright 2020, 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 NIO
import SwiftProtobuf

Expand All @@ -34,6 +34,18 @@ public enum CallHandlerFactory {
)
}

public static func makeUnary<Request: GRPCPayload, Response: GRPCPayload>(
callHandlerContext: CallHandlerContext,
eventObserverFactory: @escaping (UnaryContext<Response>) -> UnaryEventObserver<Request, Response>
) -> UnaryCallHandler<Request, Response> {
return UnaryCallHandler(
serializer: GRPCPayloadSerializer(),
deserializer: GRPCPayloadDeserializer(),
callHandlerContext: callHandlerContext,
eventObserverFactory: eventObserverFactory
)
}

public typealias ClientStreamingContext<Response> = UnaryResponseCallContext<Response>
public typealias ClientStreamingEventObserver<Request> = EventLoopFuture<(StreamEvent<Request>) -> Void>

Expand All @@ -49,6 +61,18 @@ public enum CallHandlerFactory {
)
}

public static func makeClientStreaming<Request: GRPCPayload, Response: GRPCPayload>(
callHandlerContext: CallHandlerContext,
eventObserverFactory: @escaping (ClientStreamingContext<Response>) -> ClientStreamingEventObserver<Request>
) -> ClientStreamingCallHandler<Request, Response> {
return ClientStreamingCallHandler(
serializer: GRPCPayloadSerializer(),
deserializer: GRPCPayloadDeserializer(),
callHandlerContext: callHandlerContext,
eventObserverFactory: eventObserverFactory
)
}

public typealias ServerStreamingContext<Response> = StreamingResponseCallContext<Response>
public typealias ServerStreamingEventObserver<Request> = (Request) -> EventLoopFuture<GRPCStatus>

Expand All @@ -64,6 +88,18 @@ public enum CallHandlerFactory {
)
}

public static func makeServerStreaming<Request: GRPCPayload, Response: GRPCPayload>(
callHandlerContext: CallHandlerContext,
eventObserverFactory: @escaping (ServerStreamingContext<Response>) -> ServerStreamingEventObserver<Request>
) -> ServerStreamingCallHandler<Request, Response> {
return ServerStreamingCallHandler(
serializer: GRPCPayloadSerializer(),
deserializer: GRPCPayloadDeserializer(),
callHandlerContext: callHandlerContext,
eventObserverFactory: eventObserverFactory
)
}

public typealias BidirectionalStreamingContext<Response> = StreamingResponseCallContext<Response>
public typealias BidirectionalStreamingEventObserver<Request> = EventLoopFuture<(StreamEvent<Request>) -> Void>

Expand All @@ -78,4 +114,16 @@ public enum CallHandlerFactory {
eventObserverFactory: eventObserverFactory
)
}

public static func makeBidirectionalStreaming<Request: GRPCPayload, Response: GRPCPayload>(
callHandlerContext: CallHandlerContext,
eventObserverFactory: @escaping (BidirectionalStreamingContext<Response>) -> BidirectionalStreamingEventObserver<Request>
) -> BidirectionalStreamingCallHandler<Request, Response> {
return BidirectionalStreamingCallHandler(
serializer: GRPCPayloadSerializer(),
deserializer: GRPCPayloadDeserializer(),
callHandlerContext: callHandlerContext,
eventObserverFactory: eventObserverFactory
)
}
}
Loading

0 comments on commit 183e380

Please sign in to comment.