-
Notifications
You must be signed in to change notification settings - Fork 82
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
support h2 stream resets through user events #241
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,14 @@ public final class HTTP2FramePayloadToHTTPClientCodec: ChannelDuplexHandler, Rem | |
context.fireErrorCaught(error) | ||
} | ||
} | ||
|
||
public func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) { | ||
if let ev = event as? HTTP2FramePayloadToHTTPEvent, case .reset(let code) = ev.kind { | ||
context.writeAndFlush(self.wrapOutboundOut(.rstStream(code)), promise: promise) | ||
return | ||
} | ||
context.triggerUserOutboundEvent(event, promise: promise) | ||
} | ||
} | ||
|
||
// MARK: - Server | ||
|
@@ -262,4 +270,34 @@ public final class HTTP2FramePayloadToHTTPServerCodec: ChannelDuplexHandler, Rem | |
let transformedPayload = self.baseCodec.processOutboundData(responsePart, allocator: context.channel.allocator) | ||
context.write(self.wrapOutboundOut(transformedPayload), promise: promise) | ||
} | ||
|
||
public func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) { | ||
if let ev = event as? HTTP2FramePayloadToHTTPEvent, case .reset(let code) = ev.kind { | ||
context.writeAndFlush(self.wrapOutboundOut(.rstStream(code)), promise: promise) | ||
return | ||
} | ||
context.triggerUserOutboundEvent(event, promise: promise) | ||
} | ||
} | ||
|
||
/// Events that can be sent by the application to be handled by the `HTTP2StreamChannel` | ||
public struct HTTP2FramePayloadToHTTPEvent { | ||
fileprivate enum Kind { | ||
case reset(HTTP2ErrorCode) | ||
} | ||
|
||
fileprivate var kind: Kind | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make these private and use the public API in the handlers above? |
||
|
||
/// Send a `RST_STREAM` with the specified code | ||
public static func reset(code: HTTP2ErrorCode) -> Self { | ||
.init(kind: .reset(code)) | ||
} | ||
|
||
/// Returns reset code if the event is a reset | ||
public func reset() -> HTTP2ErrorCode? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A computed property would be more idiomatic here |
||
if case let .reset(code) = self.kind { | ||
return code | ||
} | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: convention is NIO is to exhaustively switch over enums |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need a
NIO
prefix as it's new API (see https://github.com/apple/swift-nio/blob/main/docs/public-api.md#1-no-global-namespace-additions-that-arent-prefixed)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should also be
Hashable
andSendable
, it's generally useful to add these to value types.