-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a request waiter box for the connection pool (#397)
- Loading branch information
1 parent
ed44283
commit 7311c0e
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Waiter.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,57 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 NIO | ||
|
||
extension HTTPConnectionPool { | ||
struct RequestID: Hashable { | ||
private let objectIdentifier: ObjectIdentifier | ||
|
||
init(_ request: HTTPScheduledRequest) { | ||
self.objectIdentifier = ObjectIdentifier(request) | ||
} | ||
} | ||
|
||
struct Waiter { | ||
var requestID: RequestID { | ||
RequestID(self.request) | ||
} | ||
|
||
var request: HTTPScheduledRequest | ||
|
||
private var eventLoopRequirement: EventLoop? { | ||
switch self.request.eventLoopPreference.preference { | ||
case .delegateAndChannel(on: let eventLoop), | ||
.testOnly_exact(channelOn: let eventLoop, delegateOn: _): | ||
return eventLoop | ||
case .delegate(on: _), | ||
.indifferent: | ||
return nil | ||
} | ||
} | ||
|
||
init(request: HTTPScheduledRequest) { | ||
self.request = request | ||
} | ||
|
||
func canBeRun(on option: EventLoop) -> Bool { | ||
guard let requirement = self.eventLoopRequirement else { | ||
// if no requirement exists we can run on any EventLoop | ||
return true | ||
} | ||
|
||
return requirement === option | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Tests/AsyncHTTPClientTests/HTTPConnectionPool+WaiterTests+XCTest.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,32 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2018-2019 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// HTTPConnectionPool+WaiterTests+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension HTTPConnectionPool_WaiterTests { | ||
static var allTests: [(String, (HTTPConnectionPool_WaiterTests) -> () throws -> Void)] { | ||
return [ | ||
("testCanBeRunIfEventLoopIsSpecified", testCanBeRunIfEventLoopIsSpecified), | ||
("testCanBeRunIfNoEventLoopIsSpecified", testCanBeRunIfNoEventLoopIsSpecified), | ||
] | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
Tests/AsyncHTTPClientTests/HTTPConnectionPool+WaiterTests.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,63 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@testable import AsyncHTTPClient | ||
import Logging | ||
import NIO | ||
import XCTest | ||
|
||
class HTTPConnectionPool_WaiterTests: XCTestCase { | ||
func testCanBeRunIfEventLoopIsSpecified() { | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2) | ||
|
||
let theRightEL = eventLoopGroup.next() | ||
let theFalseEL = eventLoopGroup.next() | ||
|
||
let mockRequest = MockScheduledRequest(eventLoopPreference: .init(.testOnly_exact(channelOn: theRightEL, delegateOn: theFalseEL))) | ||
|
||
let waiter = HTTPConnectionPool.Waiter(request: mockRequest) | ||
|
||
XCTAssertTrue(waiter.canBeRun(on: theRightEL)) | ||
XCTAssertFalse(waiter.canBeRun(on: theFalseEL)) | ||
} | ||
|
||
func testCanBeRunIfNoEventLoopIsSpecified() { | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2) | ||
|
||
let mockRequest = MockScheduledRequest(eventLoopPreference: .indifferent) | ||
let waiter = HTTPConnectionPool.Waiter(request: mockRequest) | ||
|
||
for el in eventLoopGroup.makeIterator() { | ||
XCTAssertTrue(waiter.canBeRun(on: el)) | ||
} | ||
} | ||
} | ||
|
||
private class MockScheduledRequest: HTTPScheduledRequest { | ||
init(eventLoopPreference: HTTPClient.EventLoopPreference) { | ||
self.eventLoopPreference = eventLoopPreference | ||
} | ||
|
||
var logger: Logger { preconditionFailure("Unimplemented") } | ||
var connectionDeadline: NIODeadline { preconditionFailure("Unimplemented") } | ||
let eventLoopPreference: HTTPClient.EventLoopPreference | ||
|
||
func requestWasQueued(_: HTTPRequestScheduler) { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func fail(_: Error) { | ||
preconditionFailure("Unimplemented") | ||
} | ||
} |
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