-
Notifications
You must be signed in to change notification settings - Fork 125
cache NIOSSLContext (saves 27k allocs per conn) #362
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
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ import NIO | |
import NIOConcurrencyHelpers | ||
import NIOHTTP1 | ||
import NIOHTTPCompression | ||
import NIOSSL | ||
import NIOTLS | ||
import NIOTransportServices | ||
|
||
|
@@ -41,6 +42,8 @@ final class ConnectionPool { | |
|
||
private let backgroundActivityLogger: Logger | ||
|
||
let sslContextCache = SSLContextCache() | ||
|
||
init(configuration: HTTPClient.Configuration, backgroundActivityLogger: Logger) { | ||
self.configuration = configuration | ||
self.backgroundActivityLogger = backgroundActivityLogger | ||
|
@@ -106,6 +109,8 @@ final class ConnectionPool { | |
self.providers.values | ||
} | ||
|
||
self.sslContextCache.shutdown() | ||
|
||
return EventLoopFuture.reduce(true, providers.map { $0.close() }, on: eventLoop) { $0 && $1 } | ||
} | ||
|
||
|
@@ -148,7 +153,7 @@ final class ConnectionPool { | |
var host: String | ||
var port: Int | ||
var unixPath: String | ||
var tlsConfiguration: BestEffortHashableTLSConfiguration? | ||
private var tlsConfiguration: BestEffortHashableTLSConfiguration? | ||
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. note: make this private because nobody accesses it from the outside and if not private, then there are places where we have both a |
||
|
||
enum Scheme: Hashable { | ||
case http | ||
|
@@ -249,14 +254,15 @@ class HTTP1ConnectionProvider { | |
} else { | ||
logger.trace("opening fresh connection (found matching but inactive connection)", | ||
metadata: ["ahc-dead-connection": "\(connection)"]) | ||
self.makeChannel(preference: waiter.preference).whenComplete { result in | ||
self.makeChannel(preference: waiter.preference, | ||
logger: logger).whenComplete { result in | ||
self.connect(result, waiter: waiter, logger: logger) | ||
} | ||
} | ||
} | ||
case .create(let waiter): | ||
logger.trace("opening fresh connection (no connections to reuse available)") | ||
self.makeChannel(preference: waiter.preference).whenComplete { result in | ||
self.makeChannel(preference: waiter.preference, logger: logger).whenComplete { result in | ||
self.connect(result, waiter: waiter, logger: logger) | ||
} | ||
case .replace(let connection, let waiter): | ||
|
@@ -266,7 +272,7 @@ class HTTP1ConnectionProvider { | |
logger.trace("opening fresh connection (replacing exising connection)", | ||
metadata: ["ahc-old-connection": "\(connection)", | ||
"ahc-waiter": "\(waiter)"]) | ||
self.makeChannel(preference: waiter.preference).whenComplete { result in | ||
self.makeChannel(preference: waiter.preference, logger: logger).whenComplete { result in | ||
self.connect(result, waiter: waiter, logger: logger) | ||
} | ||
} | ||
|
@@ -434,8 +440,14 @@ class HTTP1ConnectionProvider { | |
return self.closePromise.futureResult.map { true } | ||
} | ||
|
||
private func makeChannel(preference: HTTPClient.EventLoopPreference) -> EventLoopFuture<Channel> { | ||
return NIOClientTCPBootstrap.makeHTTP1Channel(destination: self.key, eventLoop: self.eventLoop, configuration: self.configuration, preference: preference) | ||
private func makeChannel(preference: HTTPClient.EventLoopPreference, | ||
logger: Logger) -> EventLoopFuture<Channel> { | ||
return NIOClientTCPBootstrap.makeHTTP1Channel(destination: self.key, | ||
eventLoop: self.eventLoop, | ||
configuration: self.configuration, | ||
sslContextCache: self.pool.sslContextCache, | ||
preference: preference, | ||
logger: logger) | ||
} | ||
|
||
/// A `Waiter` represents a request that waits for a connection when none is | ||
|
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
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,104 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct LRUCache<Key: Equatable, Value> { | ||
private typealias Generation = UInt64 | ||
private struct Element { | ||
var generation: Generation | ||
var key: Key | ||
var value: Value | ||
} | ||
|
||
private let capacity: Int | ||
private var generation: Generation = 0 | ||
private var elements: [Element] | ||
|
||
init(capacity: Int = 8) { | ||
weissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
precondition(capacity > 0, "capacity needs to be > 0") | ||
self.capacity = capacity | ||
self.elements = [] | ||
self.elements.reserveCapacity(capacity) | ||
} | ||
|
||
private mutating func bumpGenerationAndFindIndex(key: Key) -> Int? { | ||
self.generation += 1 | ||
Lukasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let found = self.elements.firstIndex { element in | ||
element.key == key | ||
} | ||
|
||
return found | ||
} | ||
|
||
mutating func find(key: Key) -> Value? { | ||
if let found = self.bumpGenerationAndFindIndex(key: key) { | ||
self.elements[found].generation = self.generation | ||
return self.elements[found].value | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
@discardableResult | ||
mutating func append(key: Key, value: Value) -> Value { | ||
let newElement = Element(generation: self.generation, | ||
key: key, | ||
value: value) | ||
if let found = self.bumpGenerationAndFindIndex(key: key) { | ||
self.elements[found] = newElement | ||
return value | ||
} | ||
|
||
if self.elements.count < self.capacity { | ||
self.elements.append(newElement) | ||
return value | ||
} | ||
assert(self.elements.count == self.capacity) | ||
assert(self.elements.count > 0) | ||
|
||
let minIndex = self.elements.minIndex { l, r in | ||
l.generation < r.generation | ||
}! | ||
|
||
self.elements.swapAt(minIndex, self.elements.endIndex - 1) | ||
self.elements.removeLast() | ||
self.elements.append(newElement) | ||
|
||
return value | ||
} | ||
|
||
mutating func findOrAppend(key: Key, _ valueGenerator: (Key) -> Value) -> Value { | ||
if let found = self.find(key: key) { | ||
return found | ||
} | ||
|
||
return self.append(key: key, value: valueGenerator(key)) | ||
} | ||
} | ||
|
||
extension Array { | ||
func minIndex(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> Index? { | ||
guard var minSoFar: (Index, Element) = self.first.map({ (0, $0) }) else { | ||
return nil | ||
} | ||
|
||
for indexElement in self.enumerated() { | ||
if try areInIncreasingOrder(indexElement.1, minSoFar.1) { | ||
minSoFar = indexElement | ||
} | ||
weissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return minSoFar.0 | ||
} | ||
} |
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
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,104 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Logging | ||
import NIO | ||
import NIOConcurrencyHelpers | ||
import NIOSSL | ||
|
||
class SSLContextCache { | ||
Lukasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private var state = State.activeNoThread | ||
private let lock = Lock() | ||
private var sslContextCache = LRUCache<BestEffortHashableTLSConfiguration, NIOSSLContext>() | ||
private let threadPool = NIOThreadPool(numberOfThreads: 1) | ||
|
||
enum State { | ||
case activeNoThread | ||
case active | ||
case shutDown | ||
weissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
init() {} | ||
|
||
weissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func shutdown() { | ||
self.lock.withLock { () -> Void in | ||
switch self.state { | ||
case .activeNoThread: | ||
self.state = .shutDown | ||
case .active: | ||
self.state = .shutDown | ||
self.threadPool.shutdownGracefully { maybeError in | ||
precondition(maybeError == nil, "\(maybeError!)") | ||
} | ||
case .shutDown: | ||
preconditionFailure("SSLContextCache shut down twice") | ||
} | ||
} | ||
} | ||
|
||
deinit { | ||
assert(self.state == .shutDown) | ||
} | ||
} | ||
|
||
extension SSLContextCache { | ||
private struct SSLContextCacheShutdownError: Error {} | ||
|
||
func sslContext(tlsConfiguration: TLSConfiguration, | ||
eventLoop: EventLoop, | ||
logger: Logger) -> EventLoopFuture<NIOSSLContext> { | ||
let earlyExitError: Error? = self.lock.withLock { () -> Error? in | ||
switch self.state { | ||
case .activeNoThread: | ||
self.state = .active | ||
self.threadPool.start() | ||
return nil | ||
case .active: | ||
return nil | ||
case .shutDown: | ||
return SSLContextCacheShutdownError() | ||
} | ||
} | ||
|
||
if let error = earlyExitError { | ||
return eventLoop.makeFailedFuture(error) | ||
} | ||
|
||
let eqTLSConfiguration = BestEffortHashableTLSConfiguration(wrapping: tlsConfiguration) | ||
let sslContext = self.lock.withLock { | ||
self.sslContextCache.find(key: eqTLSConfiguration) | ||
} | ||
|
||
if let sslContext = sslContext { | ||
logger.debug("found SSL context in cache", | ||
metadata: ["ahc-tls-config": "\(tlsConfiguration)"]) | ||
return eventLoop.makeSucceededFuture(sslContext) | ||
} | ||
|
||
logger.debug("creating new SSL context", | ||
metadata: ["ahc-tls-config": "\(tlsConfiguration)"]) | ||
let newSSLContext = self.threadPool.runIfActive(eventLoop: eventLoop) { | ||
try NIOSSLContext(configuration: tlsConfiguration) | ||
} | ||
|
||
newSSLContext.whenSuccess { (newSSLContext: NIOSSLContext) -> Void in | ||
self.lock.withLock { () -> Void in | ||
self.sslContextCache.append(key: eqTLSConfiguration, | ||
value: newSSLContext) | ||
} | ||
} | ||
|
||
return newSSLContext | ||
} | ||
} |
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.