-
Notifications
You must be signed in to change notification settings - Fork 117
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 3980b20
Showing
2 changed files
with
104 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 | ||
} | ||
} | ||
} |
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") | ||
} | ||
} |