-
Notifications
You must be signed in to change notification settings - Fork 435
Add GRPCClientStreamHandler #1838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ec3630d
Add GRPCClientStreamHandler
gjcairo 9d5b5d7
Add missing encoding check to client's GRPCStreamStateMachine
gjcairo 2d0e49d
Changes to stream handler
gjcairo eace7ed
Add tests
gjcairo 3bfba92
Formatting
gjcairo 9454bbb
Add extra assertion to GRPCServerStreamHandler test
gjcairo e9c6eaf
Fix GRPCStreamStateMachine error message
gjcairo cdcacda
GRPCClientStreamHandler changes
gjcairo 066759f
Write and flush pending messages before forwarding close.
gjcairo 8219c3b
Change metadata init for dictionary literal
gjcairo 5c9931d
Fix close logic
gjcairo 95704b5
Change closing logic some more
gjcairo 7d313ab
Fix flushing on read complete
gjcairo dce608f
Fix another flush issue
gjcairo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
238 changes: 238 additions & 0 deletions
238
Sources/GRPCHTTP2Core/Client/GRPCClientStreamHandler.swift
This file contains hidden or 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,238 @@ | ||
| /* | ||
| * Copyright 2024, 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 NIOCore | ||
| import NIOHTTP2 | ||
|
|
||
| @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) | ||
| final class GRPCClientStreamHandler: ChannelDuplexHandler { | ||
| typealias InboundIn = HTTP2Frame.FramePayload | ||
| typealias InboundOut = RPCResponsePart | ||
|
|
||
| typealias OutboundIn = RPCRequestPart | ||
| typealias OutboundOut = HTTP2Frame.FramePayload | ||
|
|
||
| private var stateMachine: GRPCStreamStateMachine | ||
|
|
||
| private var isReading = false | ||
| private var flushPending = false | ||
|
|
||
| init( | ||
| methodDescriptor: MethodDescriptor, | ||
| scheme: Scheme, | ||
| outboundEncoding: CompressionAlgorithm, | ||
| acceptedEncodings: [CompressionAlgorithm], | ||
| maximumPayloadSize: Int, | ||
| skipStateMachineAssertions: Bool = false | ||
| ) { | ||
| self.stateMachine = .init( | ||
| configuration: .client( | ||
| .init( | ||
| methodDescriptor: methodDescriptor, | ||
| scheme: scheme, | ||
| outboundEncoding: outboundEncoding, | ||
| acceptedEncodings: acceptedEncodings | ||
| ) | ||
| ), | ||
| maximumPayloadSize: maximumPayloadSize, | ||
| skipAssertions: skipStateMachineAssertions | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // - MARK: ChannelInboundHandler | ||
|
|
||
| @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) | ||
| extension GRPCClientStreamHandler { | ||
| func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
| self.isReading = true | ||
| let frame = self.unwrapInboundIn(data) | ||
| switch frame { | ||
| case .data(let frameData): | ||
| let endStream = frameData.endStream | ||
| switch frameData.data { | ||
| case .byteBuffer(let buffer): | ||
| do { | ||
| try self.stateMachine.receive(buffer: buffer, endStream: endStream) | ||
| loop: while true { | ||
| switch self.stateMachine.nextInboundMessage() { | ||
| case .receiveMessage(let message): | ||
| context.fireChannelRead(self.wrapInboundOut(.message(message))) | ||
| case .awaitMoreMessages: | ||
| break loop | ||
| case .noMoreMessages: | ||
| context.fireUserInboundEventTriggered(ChannelEvent.inputClosed) | ||
| break loop | ||
| } | ||
| } | ||
| } catch { | ||
| context.fireErrorCaught(error) | ||
| } | ||
|
|
||
| case .fileRegion: | ||
| preconditionFailure("Unexpected IOData.fileRegion") | ||
| } | ||
|
|
||
| case .headers(let headers): | ||
| do { | ||
| let action = try self.stateMachine.receive( | ||
| headers: headers.headers, | ||
| endStream: headers.endStream | ||
| ) | ||
| switch action { | ||
| case .receivedMetadata(let metadata): | ||
| context.fireChannelRead(self.wrapInboundOut(.metadata(metadata))) | ||
|
|
||
| case .rejectRPC: | ||
| throw RPCError( | ||
| code: .internalError, | ||
| message: "Client cannot get rejectRPC." | ||
| ) | ||
|
|
||
| case .receivedStatusAndMetadata(let status, let metadata): | ||
| context.fireChannelRead(self.wrapInboundOut(.status(status, metadata))) | ||
|
|
||
| case .doNothing: | ||
| () | ||
| } | ||
| } catch { | ||
| context.fireErrorCaught(error) | ||
| } | ||
|
|
||
| case .ping, .goAway, .priority, .rstStream, .settings, .pushPromise, .windowUpdate, | ||
| .alternativeService, .origin: | ||
| () | ||
| } | ||
| } | ||
|
|
||
| func channelReadComplete(context: ChannelHandlerContext) { | ||
| self.isReading = false | ||
| if self.flushPending { | ||
| self.flushPending = false | ||
| self.flush(context: context) | ||
| } | ||
| context.fireChannelReadComplete() | ||
| } | ||
|
|
||
| func handlerRemoved(context: ChannelHandlerContext) { | ||
| self.stateMachine.tearDown() | ||
| } | ||
| } | ||
|
|
||
| // - MARK: ChannelOutboundHandler | ||
|
|
||
| @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) | ||
| extension GRPCClientStreamHandler { | ||
| func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { | ||
| switch self.unwrapOutboundIn(data) { | ||
| case .metadata(let metadata): | ||
| do { | ||
| self.flushPending = true | ||
| let headers = try self.stateMachine.send(metadata: metadata) | ||
| context.write(self.wrapOutboundOut(.headers(.init(headers: headers))), promise: nil) | ||
| // TODO: move the promise handling into the state machine | ||
| promise?.succeed() | ||
| } catch { | ||
| context.fireErrorCaught(error) | ||
| // TODO: move the promise handling into the state machine | ||
| promise?.fail(error) | ||
| } | ||
|
|
||
| case .message(let message): | ||
| do { | ||
| try self.stateMachine.send(message: message) | ||
| // TODO: move the promise handling into the state machine | ||
| promise?.succeed() | ||
| } catch { | ||
| context.fireErrorCaught(error) | ||
| // TODO: move the promise handling into the state machine | ||
| promise?.fail(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) { | ||
| switch mode { | ||
| case .output, .all: | ||
| do { | ||
| try self.stateMachine.closeOutbound() | ||
| // Force a flush by calling _flush | ||
| // (otherwise, we'd skip flushing if we're in a read loop) | ||
| self._flush(context: context) | ||
| context.close(mode: mode, promise: promise) | ||
| } catch { | ||
| promise?.fail(error) | ||
| context.fireErrorCaught(error) | ||
| } | ||
|
|
||
| case .input: | ||
| context.close(mode: .input, promise: promise) | ||
| } | ||
| } | ||
|
|
||
| func flush(context: ChannelHandlerContext) { | ||
| if self.isReading { | ||
| // We don't want to flush yet if we're still in a read loop. | ||
gjcairo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.flushPending = true | ||
| return | ||
| } | ||
|
|
||
| self._flush(context: context) | ||
| } | ||
|
|
||
| private func _flush(context: ChannelHandlerContext) { | ||
gjcairo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| do { | ||
| loop: while true { | ||
| switch try self.stateMachine.nextOutboundMessage() { | ||
| case .sendMessage(let byteBuffer): | ||
| self.flushPending = true | ||
| context.write( | ||
| self.wrapOutboundOut(.data(.init(data: .byteBuffer(byteBuffer)))), | ||
| promise: nil | ||
| ) | ||
|
|
||
| case .noMoreMessages: | ||
| // Write an empty data frame with the EOS flag set, to signal the RPC | ||
| // request is now finished. | ||
| context.write( | ||
| self.wrapOutboundOut( | ||
| HTTP2Frame.FramePayload.data( | ||
| .init( | ||
| data: .byteBuffer(.init()), | ||
| endStream: true | ||
| ) | ||
| ) | ||
| ), | ||
| promise: nil | ||
| ) | ||
|
|
||
| context.flush() | ||
| break loop | ||
|
|
||
| case .awaitMoreMessages: | ||
| if self.flushPending { | ||
| self.flushPending = false | ||
| context.flush() | ||
| } | ||
| break loop | ||
| } | ||
| } | ||
| } catch { | ||
| context.fireErrorCaught(error) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.