forked from grpc/grpc-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the requirement that messages conform to GRPCPayload on the se…
…rver Motivation: To support payloads other than `SwiftProtobuf.Message` we required that all messages conform to `GRPCPayload`. For protobuf messages we added `GRPCProtobufPayload` which provides a default implemenation of `GRPCPayload` for protobuf messages. We generated this conformance for all protobuf messages we saw. This lead to a number issues and workarounds including: grpc#738, grpc#778, grpc#801, grpc#837, grpc#877, grpc#881. The intention is to continue to support `GRPCPayload` in addition to protobuf, however, support for protobuf will not be via the `GRPCProtobufPayload` protocol. This PR adjust the server components such they only support SwiftProtobuf. Once the client side has had the same treatment (and `GRPCProtobufPayload` no longer inherits from `SwiftProtobuf.Message`), support for `GRPCPayload` will be added back. Modifications: - The `HTTP1ToGRPCServerCodec` has had the message encoding and decoding removed. It now deals in `ByteBuffer`s rather than request/response messages. - An additional `GRPCServerCodecHandler` which sits between the `HTTP1ToGRPCServerCodec` and `_BaseCallHandler` has been added which serializes/deserializes messages. - Custom payload tests have been commented out. They will return when the transition has completed. Result: - Servers only support SwiftProtobuf - Genertic constraints on the server have been removed; the constraints are place on the `init` of public handlers instead. - `GRPCProtobufPayload` is no longer required on the server.
- Loading branch information
Showing
18 changed files
with
586 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 | ||
|
||
class GRPCServerCodecHandler<Serializer: MessageSerializer, Deserializer: MessageDeserializer> { | ||
/// The response serializer. | ||
private let serializer: Serializer | ||
|
||
/// The request deserializer. | ||
private let deserializer: Deserializer | ||
|
||
internal init(serializer: Serializer, deserializer: Deserializer) { | ||
self.serializer = serializer | ||
self.deserializer = deserializer | ||
} | ||
} | ||
|
||
extension GRPCServerCodecHandler: ChannelInboundHandler { | ||
typealias InboundIn = _RawGRPCServerRequestPart | ||
typealias InboundOut = _GRPCServerRequestPart<Deserializer.Output> | ||
|
||
internal func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
switch self.unwrapInboundIn(data) { | ||
case .head(let head): | ||
context.fireChannelRead(self.wrapInboundOut(.head(head))) | ||
|
||
case .message(let buffer): | ||
do { | ||
let deserialized = try self.deserializer.deserialize(byteBuffer: buffer) | ||
context.fireChannelRead(self.wrapInboundOut(.message(deserialized))) | ||
} catch { | ||
context.fireErrorCaught(error) | ||
} | ||
|
||
case .end: | ||
context.fireChannelRead(self.wrapInboundOut(.end)) | ||
} | ||
} | ||
} | ||
|
||
extension GRPCServerCodecHandler: ChannelOutboundHandler { | ||
typealias OutboundIn = _GRPCServerResponsePart<Serializer.Input> | ||
typealias OutboundOut = _RawGRPCServerResponsePart | ||
|
||
internal func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { | ||
switch self.unwrapOutboundIn(data) { | ||
case .headers(let headers): | ||
context.write(self.wrapOutboundOut(.headers(headers)), promise: promise) | ||
|
||
case .message(let messageContext): | ||
do { | ||
let buffer = try self.serializer.serialize(messageContext.message, allocator: context.channel.allocator) | ||
context.write(self.wrapOutboundOut(.message(.init(buffer, compressed: messageContext.compressed))), promise: promise) | ||
} catch { | ||
let error = GRPCError.SerializationFailure().captureContext() | ||
promise?.fail(error) | ||
context.fireErrorCaught(error) | ||
} | ||
|
||
case .statusAndTrailers(let status, let trailers): | ||
context.write(self.wrapOutboundOut(.statusAndTrailers(status, trailers)), promise: promise) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.