-
Notifications
You must be signed in to change notification settings - Fork 84
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
Reduce allocations when peeking pseudo-headers. #246
Merged
Merged
Changes from all commits
Commits
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
114 changes: 114 additions & 0 deletions
114
...sts_01_allocation_counters/test_01_resources/test_client_server_h1_request_response.swift
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,114 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2020 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 | ||
import NIOHPACK | ||
import NIOHTTP1 | ||
import NIOHTTP2 | ||
|
||
/// Have two `EmbeddedChannel` objects send and receive data from each other until | ||
/// they make no forward progress. | ||
func interactInMemory(_ first: EmbeddedChannel, _ second: EmbeddedChannel) throws { | ||
var operated: Bool | ||
|
||
func readBytesFromChannel(_ channel: EmbeddedChannel) throws -> ByteBuffer? { | ||
return try channel.readOutbound(as: ByteBuffer.self) | ||
} | ||
|
||
repeat { | ||
operated = false | ||
first.embeddedEventLoop.run() | ||
|
||
if let data = try readBytesFromChannel(first) { | ||
operated = true | ||
try second.writeInbound(data) | ||
} | ||
if let data = try readBytesFromChannel(second) { | ||
operated = true | ||
try first.writeInbound(data) | ||
} | ||
} while operated | ||
} | ||
|
||
final class ServerHandler: ChannelInboundHandler { | ||
typealias InboundIn = HTTPServerRequestPart | ||
typealias OutboundOut = HTTPServerResponsePart | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let data = self.unwrapInboundIn(data) | ||
switch data { | ||
case .head, .body: | ||
// Ignore this | ||
return | ||
case .end: | ||
break | ||
} | ||
|
||
// We got .end. Let's send a response. | ||
let head = HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok) | ||
context.write(self.wrapOutboundOut(.head(head)), promise: nil) | ||
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) | ||
} | ||
} | ||
|
||
|
||
final class ClientHandler: ChannelInboundHandler { | ||
typealias InboundIn = HTTPClientResponsePart | ||
typealias OutboundOut = HTTPClientRequestPart | ||
|
||
func channelActive(context: ChannelHandlerContext) { | ||
// Send a request. | ||
let head = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .GET, uri: "/", headers: HTTPHeaders([("host", "localhost")])) | ||
context.write(self.wrapOutboundOut(.head(head)), promise: nil) | ||
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) | ||
} | ||
} | ||
|
||
func run(identifier: String) { | ||
let loop = EmbeddedEventLoop() | ||
|
||
measure(identifier: identifier) { | ||
var sumOfStreamIDs = 0 | ||
|
||
for _ in 0..<1000 { | ||
let clientChannel = EmbeddedChannel(loop: loop) | ||
let serverChannel = EmbeddedChannel(loop: loop) | ||
|
||
let clientMultiplexer = try! clientChannel.configureHTTP2Pipeline(mode: .client).wait() | ||
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server) { channel in | ||
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()]) | ||
}.wait() | ||
try! clientChannel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait() | ||
try! serverChannel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait() | ||
|
||
let promise = clientChannel.eventLoop.makePromise(of: Channel.self) | ||
clientMultiplexer.createStreamChannel(promise: promise) { channel in | ||
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https), ClientHandler()]) | ||
} | ||
clientChannel.embeddedEventLoop.run() | ||
let child = try! promise.futureResult.wait() | ||
let streamID = try! Int(child.getOption(HTTP2StreamChannelOptions.streamID).wait()) | ||
|
||
sumOfStreamIDs += streamID | ||
try! interactInMemory(clientChannel, serverChannel) | ||
try! child.closeFuture.wait() | ||
|
||
try! clientChannel.close().wait() | ||
try! serverChannel.close().wait() | ||
} | ||
|
||
return sumOfStreamIDs | ||
} | ||
} | ||
|
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
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.
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.
The subscript does a case insensitive check, I assume we don't need to do that here because this is only ever called internally and we know that the headers must be lowercase so we'll always provide a lowercased
name
?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.
Pseudo-headers are required to be lower-case. This is a nice perf win here: if the weren't lowercase, the request was malformed anyway.