Skip to content

Commit

Permalink
namespace ChannelOptions & fix deprecations (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
weissi authored Oct 28, 2019
1 parent 97e8bbb commit 994c319
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let package = Package(
.library(name: "NIOHTTP2", targets: ["NIOHTTP2"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.3.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.9.0"),
],
targets: [
.target(name: "NIOHTTP2Server",
Expand All @@ -35,7 +35,7 @@ let package = Package(
.target(name: "NIOHPACK",
dependencies: ["NIO", "NIOConcurrencyHelpers", "NIOHTTP1"]),
.testTarget(name: "NIOHTTP2Tests",
dependencies: ["NIO", "NIOHTTP1", "NIOHTTP2"]),
dependencies: ["NIO", "NIOHTTP1", "NIOHTTP2", "NIOFoundationCompat"]),
.testTarget(name: "NIOHPACKTests",
dependencies: ["NIOHPACK"])
]
Expand Down
39 changes: 24 additions & 15 deletions Sources/NIOHTTP2/HTTP2StreamChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,35 @@
import NIO
import NIOConcurrencyHelpers

/// `StreamIDOption` allows users to query the stream ID for a given `HTTP2StreamChannel`.
///
/// On active `HTTP2StreamChannel`s, it is possible that a channel handler or user may need to know which
/// stream ID the channel owns. This channel option allows that query. Please note that this channel option
/// is *get-only*: that is, it cannot be used with `setOption`. The stream ID for a given `HTTP2StreamChannel`
/// is immutable.
public struct StreamIDOption: ChannelOption {
public typealias Value = HTTP2StreamID

public init() { }
}

/// The various channel options specific to `HTTP2StreamChannel`s.
///
/// Please note that some of NIO's regular `ChannelOptions` are valid on `HTTP2StreamChannel`s.
public struct HTTP2StreamChannelOptions {
/// - seealso: `StreamIDOption`.
public static let streamID: StreamIDOption = StreamIDOption()
public static let streamID: HTTP2StreamChannelOptions.Types.StreamIDOption = .init()
}

extension HTTP2StreamChannelOptions {
public enum Types {}
}

@available(*, deprecated, renamed: "HTTP2StreamChannelOptions.Types.StreamIDOption")
public typealias StreamIDOption = HTTP2StreamChannelOptions.Types.StreamIDOption

extension HTTP2StreamChannelOptions.Types {
/// `StreamIDOption` allows users to query the stream ID for a given `HTTP2StreamChannel`.
///
/// On active `HTTP2StreamChannel`s, it is possible that a channel handler or user may need to know which
/// stream ID the channel owns. This channel option allows that query. Please note that this channel option
/// is *get-only*: that is, it cannot be used with `setOption`. The stream ID for a given `HTTP2StreamChannel`
/// is immutable.
public struct StreamIDOption: ChannelOption {
public typealias Value = HTTP2StreamID

public init() { }
}
}

/// The current state of a stream channel.
private enum StreamChannelState {
Expand Down Expand Up @@ -269,7 +278,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
assert(eventLoop.inEventLoop)

switch option {
case _ as AutoReadOption:
case _ as ChannelOptions.Types.AutoReadOption:
self.autoRead = value as! Bool
default:
fatalError("setting option \(option) on HTTP2StreamChannel not supported")
Expand All @@ -280,9 +289,9 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
assert(eventLoop.inEventLoop)

switch option {
case _ as StreamIDOption:
case _ as HTTP2StreamChannelOptions.Types.StreamIDOption:
return self.streamID as! Option.Value
case _ as AutoReadOption:
case _ as ChannelOptions.Types.AutoReadOption:
return self.autoRead as! Option.Value
default:
fatalError("option \(option) not supported on HTTP2StreamChannel")
Expand Down
17 changes: 6 additions & 11 deletions Tests/NIOHPACKTests/HuffmanCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import XCTest
import NIO
import NIOFoundationCompat
import Foundation
@testable import NIOHPACK

Expand Down Expand Up @@ -136,13 +137,10 @@ class HuffmanCodingTests: XCTestCase {
}

var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeWithUnsafeMutableBytes { ptr in
let bytePtr = ptr.bindMemory(to: UInt8.self)
return data.copyBytes(to: bytePtr)
}

buffer.writeBytes(data)

// warm up the decoder
_ = try! buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes)
XCTAssertNoThrow(try buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes))

self.measureMetrics(HuffmanCodingTests.defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
startMeasuring()
Expand Down Expand Up @@ -183,13 +181,10 @@ class HuffmanCodingTests: XCTestCase {
}

var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeWithUnsafeMutableBytes { ptr in
let bytePtr = ptr.bindMemory(to: UInt8.self)
return data.copyBytes(to: bytePtr)
}
buffer.writeBytes(data)

// ensure the decoder table has been loaded
_ = try! buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes)
XCTAssertNoThrow(try buffer.getHuffmanEncodedString(at: buffer.readerIndex, length: buffer.readableBytes))

self.measureMetrics(HuffmanCodingTests.defaultPerformanceMetrics, automaticallyStartMeasuring: false) {
startMeasuring()
Expand Down
2 changes: 1 addition & 1 deletion Tests/NIOHTTP2Tests/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func openTemporaryFile() -> (CInt, String) {
extension FileRegion {
func asByteBuffer(allocator: ByteBufferAllocator) -> ByteBuffer {
var fileBuffer = allocator.buffer(capacity: self.readableBytes)
fileBuffer.writeWithUnsafeMutableBytes { ptr in
fileBuffer.writeWithUnsafeMutableBytes(minimumWritableBytes: self.readableBytes) { ptr in
let rc = try! self.fileHandle.withUnsafeFileDescriptor { fd -> Int in
lseek(fd, off_t(self.readerIndex), SEEK_SET)
return read(fd, ptr.baseAddress!, self.readableBytes)
Expand Down
4 changes: 2 additions & 2 deletions docker/docker-compose.1804.50.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=72010
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=378000
- MAX_ALLOCS_ALLOWED_client_server_request_response=384810

performance-test:
image: swift-nio-http2:18.04-5.0
Expand All @@ -31,7 +31,7 @@ services:
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=72010
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=378000
- MAX_ALLOCS_ALLOWED_client_server_request_response=384810

shell:
image: swift-nio-http2:18.04-5.0

0 comments on commit 994c319

Please sign in to comment.