Pure-Swift HTTP/2 implementation (#40)

* Updated to use SwiftNIO 1.10+ to gain access to HTTPHeaders internals.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Initial basic parser code.

* Adding unit tests.

* Add NIOHTTP2FlowControlHandler

Motivation:

One of the things that nghttp2 used to do for us was to handle buffering
DATA frames until flow control allowed them to go out. For users that
want that behaviour, we need to replace the nghttp2 code with a new
ChannelHandler that can do this work.

Modifications:

- Added NIOHTTP2FlowControlHandler.

Result:

Users will have the same feature set as they did with nghttp2.

* Generate Linux test information.

* Documentation comment.

* Updated new tests from master to use new payload types.

* Updated following review.

Notable changes:

- Implemented unit tests for the ALTSVC (0xa) and ORIGIN (0xc) frame types.
- Additional DATA frames are synthesized to wrap individual payload chunks as they arrive. If the payload of a DATA frame with 4MB data arrives in 4 separate 1MB chunks, the decoder will emit 4 DATA frames, with only the last containing the END_STREAM flag (if that was set on the incoming frame).
- HEADERS / PUSH_PROMISE frames and any following CONTINUATION frames are now handled properly: header fragments from all frames are gathered up into a single buffer. Once a frame with END_HEADERS is encountered, the resultant accumulated buffer is sent for decoding along with the flags and stream ID of the original HEADERS / PUSH_PROMISE frame.
- HPACK encoders/decoders now live in the frame encoder/decoder types, rather than being attached to a stream.

* Updated Linux tests.

* Headers placed in relevant files.

* Add pure-Swift HTTP/2 protocol implementation.

Motivation:

Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.

This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.

Modifications:

- Add pure Swift protocol implementations.
- Test them.

Results:

Another step towards protocol independence.

* Revamped frame decoder state machine and API. Now uses separate input and output methods to manage multiple frames in a single buffer.

* Updated Linux tests.

* Bumped swift-nio version to 1.12.0 and updated `HTTPHeaders` -> `HPACKHeaders` conversion initializer to directly copy the buffer and map the header name/value indices.

* Set up HTTP2Parser in handlerAdded if the channel had been previously activated. (#36)

By setting up the handler in the handlerAdded hook, HTTP2Parser supports
being added dynamically in a channelRead event, after the channel has sent
the channelActive event.

(cherry picked from commit 88bd6d369a)

* Minor tweaks to get the tests passing

* Initial integration here

* Minor clearing up of the handler

* Get the tests compiling

* Fix header issues.

We now pass the frame parser to the various setting operations to let it
update the state. We also fixed some issues with specific settings.

* Remove ngthttp2-based impl, clean up stream ID

* Tests with new HTTP2StreamID

* Simple test cleanup

* Get remaining non-core tests passing

* Allow GOAWAY even when fully quiesced

* We don't buffer outbound frames any more

* Emit StreamClosed events on GoAway frame

* Notify stream creation and closure

* This test started failing

* Goodbye, C code! 🎉

* Send PING ACK automatically

* Updated based on PR feedback.

* Correctly handle 1XX responses

* Add SETTINGS_MAX_FRAME_SIZE support to flow controller

* Correctly account for flow control

* Define ConcurrentStreamsHandler

* Prepare for signalling connection state changes

* Better state change message propagation

* Refactor to use result with effect everywhere

* Don't lose flow control info on stream error

* Add basic flow control management to stream multiplexer

* Better pipeline in sample server

* Clean up some reentrancy concerns

* Move to Swift 5 and NIO 2

* Move to new Swift 5 for CI

* Allow wider range of choices of years

* Catch up to NIO 2 again
This commit is contained in:
Cory Benfield authored and Johannes Weiss committed 2019-02-21 11:52:49 +00:00
1 parent ac7a0d41cf
commit 3c90345c36
88 files changed
+12998 -3374

No files matched your search

@@ -0,0 +1,421 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
public class NIOHTTP2FlowControlHandler: ChannelDuplexHandler {
public typealias InboundIn = HTTP2Frame
public typealias InboundOut = HTTP2Frame
public typealias OutboundIn = HTTP2Frame
public typealias OutboundOut = HTTP2Frame
/// A buffer of the data sent on the stream that has not yet been passed to the the connection state machine.
private var streamDataBuffers: [HTTP2StreamID: StreamFlowControlState]
// TODO(cory): This will eventually need to grow into a priority implementation. For now, it's sufficient to just
// use a set and worry about the data structure costs later.
/// The streams with pending data to output.
private var flushableStreams: Set<HTTP2StreamID> = Set()
/// The current size of the connection flow control window. May be negative.
private var connectionWindowSize: Int
/// The current value of SETTINGS_MAX_FRAME_SIZE set by the peer.
private var maxFrameSize: Int
/// Whether we need to flush on channelReadComplete.
private var needToFlush = false
public init(initialConnectionWindowSize: Int = 65535, initialMaxFrameSize: Int = 1<<14) {
/// By and large there won't be that many concurrent streams floating around, so we pre-allocate a decent-ish
/// size.
// TODO(cory): HEAP! This should be a heap, sorted off the number of pending bytes!
self.streamDataBuffers = Dictionary(minimumCapacity: 16)
self.connectionWindowSize = initialConnectionWindowSize
self.maxFrameSize = initialMaxFrameSize
}
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let frame = self.unwrapOutboundIn(data)
switch frame.payload {
case .data(let body):
// We buffer DATA frames.
if !self.streamDataBuffers[frame.streamID].apply({ $0.dataBuffer.bufferWrite((.data(body), frame.flags, promise)) }) {
// We don't have this stream ID. This is an internal error, but we won't precondition on it as
// it can happen due to channel handler misconfiguration or other weirdness. We'll just complain.
let error = NIOHTTP2Errors.NoSuchStream(streamID: frame.streamID)
promise?.fail(error)
ctx.fireErrorCaught(error)
}
case .headers(let headers, let priorityData):
// Headers are special. If we have a data frame buffered, we buffer behind it to avoid frames
// being reordered. However, if we don't have a data frame buffered we pass the headers frame on
// immediately, as there is no risk of violating ordering guarantees.
let bufferResult = self.streamDataBuffers[frame.streamID].modify { (state: inout StreamFlowControlState) -> Bool in
if state.dataBuffer.haveBufferedDataFrame {
state.dataBuffer.bufferWrite((.headers(headers, priorityData), frame.flags, promise))
return true
} else {
return false
}
}
switch bufferResult {
case .some(true):
// Buffered, do nothing.
break
case .some(false), .none:
// We don't need to buffer this, pass it on.
ctx.write(data, promise: promise)
}
default:
// For all other frame types, we don't care about them, pass them on.
ctx.write(data, promise: promise)
}
}
public func flush(ctx: ChannelHandlerContext) {
// Two stages of flushing. The first thing is to mark the flush point on all of the streams we have.
self.streamDataBuffers.mutatingForEachValue {
let hadData = $0.hasPendingData
$0.dataBuffer.markFlushPoint()
if $0.hasPendingData && !hadData {
assert(!self.flushableStreams.contains($0.streamID))
self.flushableStreams.insert($0.streamID)
}
}
// Ok, we want to send as much data as we can.
// This loop here is a while because we make outcalls here, and these outcalls
// may cause re-entrant behaviour. We need to not be holding our structures mutably
// over the duration of this outcall.
self.writeIfPossible(ctx: ctx)
ctx.flush()
}
public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
switch event {
case let event as NIOHTTP2WindowUpdatedEvent:
if let windowSize = event.outboundWindowSize {
// We don't try to write here, we do it in channelReadComplete or flush.
self.updateWindowOfStream(event.streamID, newSize: Int32(windowSize))
}
case let event as StreamClosedEvent:
self.streamClosed(event.streamID, errorCode: event.reason ?? .streamClosed)
case let event as NIOHTTP2StreamCreatedEvent:
// Any stream with a nil flow control window we set to a window size of 0, forbidding sending data.
self.streamCreated(event.streamID, initialWindowSize: event.localInitialWindowSize ?? 0)
default:
break
}
ctx.fireUserInboundEventTriggered(event)
}
public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
let frame = self.unwrapInboundIn(data)
if case .settings(let newSettings) = frame.payload, !frame.flags.contains(.ack) {
// This is a SETTINGS frame for new settings from the remote peer. We
// should apply this change. We only care about the value of SETTINGS_MAX_FRAME_SIZE:
// settings changes that affect flow control windows are notified to us in other ways.
if let newMaxFrameSize = newSettings.reversed().first(where: {$0.parameter == .maxFrameSize})?.value {
self.maxFrameSize = newMaxFrameSize
}
}
ctx.fireChannelRead(data)
}
public func channelReadComplete(ctx: ChannelHandlerContext) {
let didWrite = self.writeIfPossible(ctx: ctx)
if didWrite {
ctx.flush()
}
}
private func nextStreamToSend() -> HTTP2StreamID? {
return self.flushableStreams.first
}
private func updateWindowOfStream(_ streamID: HTTP2StreamID, newSize: Int32) {
if streamID == .rootStream {
self.connectionWindowSize = Int(newSize)
} else {
// We don't bother to check for missing stream dictionaries here, as we operate
// in a concurrent world where all sorts of weird nonsense can occur.
// If we have got the window update for the stream after the stream was closed
// or before it was opened, that's ok, do nothing.
self.streamDataBuffers[streamID].apply {
let hadData = $0.hasPendingData
$0.currentWindowSize = Int(newSize)
if $0.hasPendingData && !hadData {
assert(!self.flushableStreams.contains($0.streamID))
self.flushableStreams.insert($0.streamID)
}
}
}
}
private func streamCreated(_ streamID: HTTP2StreamID, initialWindowSize: UInt32) {
assert(streamID != .rootStream)
let streamState = StreamFlowControlState(streamID: streamID, initialWindowSize: Int(initialWindowSize))
self.streamDataBuffers[streamID] = streamState
}
// We received a stream closed event. Drop any stream state we're holding. If we have any
// unflushed or buffered DATA frames, we fail their promises.
private func streamClosed(_ streamID: HTTP2StreamID, errorCode: HTTP2ErrorCode) {
self.flushableStreams.remove(streamID)
guard var streamData = self.streamDataBuffers.removeValue(forKey: streamID) else {
// Huh, we didn't have any data for this stream. Oh well. That was easy.
return
}
streamData.failAllWrites(error: NIOHTTP2Errors.StreamClosed(streamID: streamID, errorCode: errorCode))
}
/// Writes whatever data may be written in a loop.
///
/// - returns: Whether this issued a write.
@discardableResult
private func writeIfPossible(ctx: ChannelHandlerContext) -> Bool {
// This loop here is a while because we make outcalls here, and these outcalls
// may cause re-entrant behaviour. We need to not be holding our structures mutably
// over the duration of this outcall.
var didWrite = false
while let nextStreamID = self.nextStreamToSend(), self.connectionWindowSize > 0 {
let nextWrite = self.streamDataBuffers[nextStreamID].modify { (state: inout StreamFlowControlState) -> DataBuffer.BufferElement in
let nextWrite = state.nextWrite(maxSize: min(self.connectionWindowSize, self.maxFrameSize))
if !state.hasPendingData {
self.flushableStreams.remove(nextStreamID)
}
return nextWrite
}
guard let (payload, flags, promise) = nextWrite else {
// The stream was not present. This is weird, it shouldn't ever happen, but we tolerate it
self.flushableStreams.remove(nextStreamID)
continue
}
let frame = HTTP2Frame(streamID: nextStreamID, flags: flags, payload: payload)
ctx.write(self.wrapOutboundOut(frame), promise: promise)
didWrite = true
}
return didWrite
}
}
private struct StreamFlowControlState {
let streamID: HTTP2StreamID
var currentWindowSize: Int
var dataBuffer: DataBuffer
var hasPendingData: Bool {
return self.dataBuffer.hasMark && (self.currentWindowSize > 0 || self.dataBuffer.nextWriteIsHeaders)
}
init(streamID: HTTP2StreamID, initialWindowSize: Int) {
self.streamID = streamID
self.currentWindowSize = initialWindowSize
self.dataBuffer = DataBuffer()
}
mutating func nextWrite(maxSize: Int) -> DataBuffer.BufferElement {
assert(maxSize > 0)
let writeSize = min(maxSize, currentWindowSize)
let nextWrite = self.dataBuffer.nextWrite(maxSize: writeSize)
if case .data(let payload) = nextWrite.0 {
self.currentWindowSize -= payload.readableBytes
}
assert(self.currentWindowSize >= 0)
return nextWrite
}
mutating func failAllWrites(error: Error) {
self.dataBuffer.failAllWrites(error: error)
}
}
private struct DataBuffer {
typealias BufferElement = (HTTP2Frame.FramePayload, HTTP2Frame.FrameFlags, EventLoopPromise<Void>?)
private var bufferedChunks: MarkedCircularBuffer<BufferElement>
internal private(set) var flushedBufferedBytes: UInt
var haveBufferedDataFrame: Bool {
return self.bufferedChunks.count > 0
}
var nextWriteIsHeaders: Bool {
if case .some(.headers) = self.bufferedChunks.first?.0 {
return true
} else {
return false
}
}
var hasMark: Bool {
return self.bufferedChunks.hasMark
}
init() {
self.bufferedChunks = MarkedCircularBuffer(initialCapacity: 8)
self.flushedBufferedBytes = 0
}
mutating func bufferWrite(_ write: BufferElement) {
self.bufferedChunks.append(write)
}
/// Marks the current point in the buffer as the place up to which we have flushed.
mutating func markFlushPoint() {
if let markIndex = self.bufferedChunks.markedElementIndex {
for element in self.bufferedChunks.suffix(from: markIndex) {
if case .data(let contents) = element.0 {
self.flushedBufferedBytes += UInt(contents.readableBytes)
}
}
self.bufferedChunks.mark()
} else if self.bufferedChunks.count > 0 {
for element in self.bufferedChunks {
if case .data(let contents) = element.0 {
self.flushedBufferedBytes += UInt(contents.readableBytes)
}
}
self.bufferedChunks.mark()
}
}
mutating func nextWrite(maxSize: Int) -> BufferElement {
assert(maxSize >= 0)
precondition(self.bufferedChunks.count > 0)
let firstElementIndex = self.bufferedChunks.startIndex
// First check that the next write is DATA. If it's not, just pass it on.
guard case .data(var contents) = self.bufferedChunks[firstElementIndex].0 else {
return self.bufferedChunks.removeFirst()
}
// Now check if we have enough space to return the next DATA frame wholesale.
let firstElementReadableBytes = contents.readableBytes
if firstElementReadableBytes <= maxSize {
// Return the whole element.
self.flushedBufferedBytes -= UInt(firstElementReadableBytes)
return self.bufferedChunks.removeFirst()
}
// Here we have too many bytes. So we need to slice out a copy of the data we need
// and leave the rest.
let newElement = contents.slicePrefix(maxSize)
self.flushedBufferedBytes -= UInt(maxSize)
self.bufferedChunks[self.bufferedChunks.startIndex].0 = .data(contents)
return (.data(newElement), HTTP2Frame.FrameFlags(), nil)
}
mutating func failAllWrites(error: Error) {
while self.bufferedChunks.count > 0 {
let (_, _, promise) = self.bufferedChunks.removeFirst()
promise?.fail(error)
}
}
}
private extension IOData {
mutating func slicePrefix(_ length: Int) -> IOData {
assert(length < self.readableBytes)
switch self {
case .byteBuffer(var buf):
// This force-unwrap is safe, as we only invoke this when we have already checked the length.
let newBuf = buf.readSlice(length: length)!
self = .byteBuffer(buf)
return .byteBuffer(newBuf)
case .fileRegion(var region):
let newRegion = FileRegion(fileHandle: region.fileHandle, readerIndex: region.readerIndex, endIndex: region.readerIndex + length)
region.moveReaderIndex(forwardBy: length)
self = .fileRegion(region)
return .fileRegion(newRegion)
}
}
}
private extension Optional where Wrapped == StreamFlowControlState {
// This function exists as a performance optimisation: by mutating the optional returned from Dictionary directly
// inline, we can avoid the dictionary needing to hash the key twice, which it would have to do if we removed the
// value, mutated it, and then re-inserted it.
//
// However, we need to be a bit careful here, as the performance gain from doing this would be completely swamped
// if the Swift compiler failed to inline this method into its caller. This would force the closure to have its
// context heap-allocated, and the cost of doing that is vastly higher than the cost of hashing the key a second
// time. So for this reason we make it clear to the compiler that this method *must* be inlined at the call-site.
// Sorry about doing this!
//
/// Apply a transform to a wrapped DataBuffer.
///
/// - parameters:
/// - body: A block that will modify the contained value in the
/// optional, if there is one present.
/// - returns: Whether the value was present or not.
@inline(__always)
@discardableResult
mutating func apply(_ body: (inout Wrapped) -> Void) -> Bool {
if self == nil {
return false
}
var unwrapped = self!
self = nil
body(&unwrapped)
self = unwrapped
return true
}
// This function exists as a performance optimisation: by mutating the optional returned from Dictionary directly
// inline, we can avoid the dictionary needing to hash the key twice, which it would have to do if we removed the
// value, mutated it, and then re-inserted it.
//
// However, we need to be a bit careful here, as the performance gain from doing this would be completely swamped
// if the Swift compiler failed to inline this method into its caller. This would force the closure to have its
// context heap-allocated, and the cost of doing that is vastly higher than the cost of hashing the key a second
// time. So for this reason we make it clear to the compiler that this method *must* be inlined at the call-site.
// Sorry about doing this!
//
/// Apply a transform to a wrapped DataBuffer and return the result.
///
/// - parameters:
/// - body: A block that will modify the contained value in the
/// optional, if there is one present.
/// - returns: The return value of the modification, or nil if there was no object to modify.
mutating func modify<T>(_ body: (inout Wrapped) -> T) -> T? {
if self == nil {
return nil
}
var unwrapped = self!
self = nil
let r = body(&unwrapped)
self = unwrapped
return r
}
}