-
Notifications
You must be signed in to change notification settings - Fork 125
Add a RequestQueue #412
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
Add a RequestQueue #412
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
57 changes: 0 additions & 57 deletions
57
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Waiter.swift
This file was deleted.
Oops, something went wrong.
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
119 changes: 119 additions & 0 deletions
119
Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+RequestQueue.swift
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,119 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 NIOCore | ||
|
||
extension HTTPConnectionPool { | ||
/// A struct to store all queued requests. | ||
struct RequestQueue { | ||
private var generalPurposeQueue: CircularBuffer<Request> | ||
private var eventLoopQueues: [EventLoopID: CircularBuffer<Request>] | ||
glbrntt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
init() { | ||
self.generalPurposeQueue = CircularBuffer(initialCapacity: 32) | ||
self.eventLoopQueues = [:] | ||
} | ||
|
||
var count: Int { | ||
self.generalPurposeQueue.count + self.eventLoopQueues.reduce(0) { $0 + $1.value.count } | ||
} | ||
|
||
var isEmpty: Bool { | ||
self.count == 0 | ||
} | ||
|
||
func count(for eventLoop: EventLoop?) -> Int { | ||
if let eventLoop = eventLoop { | ||
return self.withEventLoopQueueIfAvailable(for: eventLoop.id) { $0.count } ?? 0 | ||
} | ||
return self.generalPurposeQueue.count | ||
} | ||
|
||
func isEmpty(for eventLoop: EventLoop?) -> Bool { | ||
if let eventLoop = eventLoop { | ||
return self.withEventLoopQueueIfAvailable(for: eventLoop.id) { $0.isEmpty } ?? true | ||
} | ||
return self.generalPurposeQueue.isEmpty | ||
} | ||
|
||
@discardableResult | ||
mutating func push(_ request: Request) -> Request.ID { | ||
if let eventLoop = request.requiredEventLoop { | ||
self.withEventLoopQueue(for: eventLoop.id) { queue in | ||
queue.append(request) | ||
} | ||
} else { | ||
self.generalPurposeQueue.append(request) | ||
} | ||
return request.id | ||
} | ||
|
||
mutating func popFirst(for eventLoop: EventLoop? = nil) -> Request? { | ||
if let eventLoop = eventLoop { | ||
return self.withEventLoopQueue(for: eventLoop.id) { queue in | ||
queue.popFirst() | ||
} | ||
} else { | ||
return self.generalPurposeQueue.popFirst() | ||
} | ||
} | ||
|
||
mutating func remove(_ requestID: Request.ID) -> Request? { | ||
if let eventLoopID = requestID.eventLoopID { | ||
return self.withEventLoopQueue(for: eventLoopID) { queue in | ||
guard let index = queue.firstIndex(where: { $0.id == requestID }) else { | ||
return nil | ||
} | ||
return queue.remove(at: index) | ||
} | ||
} else { | ||
if let index = self.generalPurposeQueue.firstIndex(where: { $0.id == requestID }) { | ||
// TBD: This is slow. Do we maybe want something more sophisticated here? | ||
return self.generalPurposeQueue.remove(at: index) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
mutating func removeAll() -> [Request] { | ||
var result = [Request]() | ||
result = self.eventLoopQueues.flatMap { $0.value } | ||
result.append(contentsOf: self.generalPurposeQueue) | ||
|
||
self.eventLoopQueues.removeAll() | ||
self.generalPurposeQueue.removeAll() | ||
return result | ||
} | ||
|
||
private mutating func withEventLoopQueue<Result>( | ||
for eventLoopID: EventLoopID, | ||
_ closure: (inout CircularBuffer<Request>) -> Result | ||
) -> Result { | ||
if self.eventLoopQueues[eventLoopID] == nil { | ||
self.eventLoopQueues[eventLoopID] = CircularBuffer(initialCapacity: 32) | ||
} | ||
return closure(&self.eventLoopQueues[eventLoopID]!) | ||
} | ||
|
||
private func withEventLoopQueueIfAvailable<Result>( | ||
for eventLoopID: EventLoopID, | ||
_ closure: (CircularBuffer<Request>) -> Result | ||
) -> Result? { | ||
if let queue = self.eventLoopQueues[eventLoopID] { | ||
return closure(queue) | ||
} | ||
return nil | ||
} | ||
} | ||
} |
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
134 changes: 134 additions & 0 deletions
134
Tests/AsyncHTTPClientTests/HTTPConnectionPool+RequestQueueTests.swift
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,134 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 NIOCore | ||
import NIOEmbedded | ||
import NIOHTTP1 | ||
import XCTest | ||
|
||
class HTTPConnectionPool_RequestQueueTests: XCTestCase { | ||
func testCountAndIsEmptyWorks() { | ||
var queue = HTTPConnectionPool.RequestQueue() | ||
XCTAssertTrue(queue.isEmpty) | ||
XCTAssertEqual(queue.count, 0) | ||
let req1 = MockScheduledRequest(eventLoopPreference: .indifferent) | ||
let req1ID = queue.push(.init(req1)) | ||
XCTAssertFalse(queue.isEmpty) | ||
XCTAssertFalse(queue.isEmpty(for: nil)) | ||
XCTAssertEqual(queue.count, 1) | ||
XCTAssertEqual(queue.count(for: nil), 1) | ||
|
||
let req2 = MockScheduledRequest(eventLoopPreference: .indifferent) | ||
let req2ID = queue.push(.init(req2)) | ||
XCTAssertEqual(queue.count, 2) | ||
|
||
XCTAssert(queue.popFirst()?.__testOnly_wrapped_request() === req1) | ||
XCTAssertEqual(queue.count, 1) | ||
XCTAssertFalse(queue.isEmpty) | ||
XCTAssert(queue.remove(req2ID)?.__testOnly_wrapped_request() === req2) | ||
XCTAssertNil(queue.remove(req1ID)) | ||
XCTAssertEqual(queue.count, 0) | ||
XCTAssertTrue(queue.isEmpty) | ||
|
||
let eventLoop = EmbeddedEventLoop() | ||
|
||
XCTAssertTrue(queue.isEmpty(for: eventLoop)) | ||
XCTAssertEqual(queue.count(for: eventLoop), 0) | ||
let req3 = MockScheduledRequest(eventLoopPreference: .delegateAndChannel(on: eventLoop)) | ||
let req3ID = queue.push(.init(req3)) | ||
XCTAssertFalse(queue.isEmpty(for: eventLoop)) | ||
XCTAssertEqual(queue.count(for: eventLoop), 1) | ||
XCTAssertFalse(queue.isEmpty) | ||
XCTAssertEqual(queue.count, 1) | ||
XCTAssert(queue.popFirst(for: eventLoop)?.__testOnly_wrapped_request() === req3) | ||
XCTAssertNil(queue.remove(req3ID)) | ||
XCTAssertTrue(queue.isEmpty(for: eventLoop)) | ||
XCTAssertEqual(queue.count(for: eventLoop), 0) | ||
XCTAssertTrue(queue.isEmpty) | ||
XCTAssertEqual(queue.count, 0) | ||
|
||
let req4 = MockScheduledRequest(eventLoopPreference: .delegateAndChannel(on: eventLoop)) | ||
let req4ID = queue.push(.init(req4)) | ||
XCTAssert(queue.remove(req4ID)?.__testOnly_wrapped_request() === req4) | ||
|
||
let req5 = MockScheduledRequest(eventLoopPreference: .indifferent) | ||
queue.push(.init(req5)) | ||
let req6 = MockScheduledRequest(eventLoopPreference: .delegateAndChannel(on: eventLoop)) | ||
queue.push(.init(req6)) | ||
let all = queue.removeAll() | ||
let testSet = all.map { $0.__testOnly_wrapped_request() } | ||
XCTAssertEqual(testSet.count, 2) | ||
XCTAssertTrue(testSet.contains(where: { $0 === req5 })) | ||
XCTAssertTrue(testSet.contains(where: { $0 === req6 })) | ||
XCTAssertFalse(testSet.contains(where: { $0 === req4 })) | ||
XCTAssertTrue(queue.isEmpty(for: eventLoop)) | ||
XCTAssertEqual(queue.count(for: eventLoop), 0) | ||
XCTAssertTrue(queue.isEmpty) | ||
XCTAssertEqual(queue.count, 0) | ||
} | ||
} | ||
|
||
private class MockScheduledRequest: HTTPSchedulableRequest { | ||
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") | ||
} | ||
|
||
// MARK: HTTPExecutableRequest | ||
|
||
var requestHead: HTTPRequestHead { preconditionFailure("Unimplemented") } | ||
var requestFramingMetadata: RequestFramingMetadata { preconditionFailure("Unimplemented") } | ||
var idleReadTimeout: TimeAmount? { preconditionFailure("Unimplemented") } | ||
|
||
func willExecuteRequest(_: HTTPRequestExecutor) { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func requestHeadSent() { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func resumeRequestBodyStream() { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func pauseRequestBodyStream() { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func receiveResponseHead(_: HTTPResponseHead) { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func receiveResponseBodyParts(_: CircularBuffer<ByteBuffer>) { | ||
preconditionFailure("Unimplemented") | ||
} | ||
|
||
func succeedRequest(_: CircularBuffer<ByteBuffer>?) { | ||
preconditionFailure("Unimplemented") | ||
} | ||
} |
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.