-
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 waiter box for the connection pool
- Loading branch information
1 parent
bccb075
commit 843ceee
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 Waiter { | ||
struct ID: Hashable { | ||
private let objectIdentifier: ObjectIdentifier | ||
|
||
init(_ request: HTTPScheduledRequest) { | ||
self.objectIdentifier = ObjectIdentifier(request) | ||
} | ||
} | ||
|
||
let id: ID | ||
let request: HTTPScheduledRequest | ||
let eventLoopRequirement: EventLoop? | ||
|
||
init(request: HTTPScheduledRequest, eventLoopRequirement: EventLoop?) { | ||
self.id = ID(request) | ||
self.request = request | ||
self.eventLoopRequirement = eventLoopRequirement | ||
} | ||
|
||
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), | ||
] | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: theRightEL) | ||
|
||
XCTAssertTrue(waiter.canBeRun(on: theRightEL)) | ||
XCTAssertFalse(waiter.canBeRun(on: theFalseEL)) | ||
} | ||
|
||
func testCanBeRunIfNoEventLoopIsSpecified() { | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2) | ||
|
||
let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: nil) | ||
|
||
for el in eventLoopGroup.makeIterator() { | ||
XCTAssertTrue(waiter.canBeRun(on: el)) | ||
} | ||
} | ||
} | ||
|
||
private class MockScheduledRequest: HTTPScheduledRequest { | ||
init() {} | ||
|
||
var logger: Logger { preconditionFailure("Unimplemented") } | ||
var connectionDeadline: NIODeadline { preconditionFailure("Unimplemented") } | ||
var eventLoopPreference: HTTPClient.EventLoopPreference { preconditionFailure("Unimplemented") } | ||
|
||
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