-
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
Add a RequestQueue #412
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 = [:] | ||
self.count = 0 | ||
} | ||
|
||
private(set) var count: Int | ||
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. How often is this used externally? If it's not used frequently then dynamically computing this would be better I think... |
||
|
||
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 { | ||
self.count += 1 | ||
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? { | ||
let request: Request? | ||
if let eventLoop = eventLoop { | ||
request = self.withEventLoopQueue(for: eventLoop.id) { queue in | ||
queue.popFirst() | ||
} | ||
} else { | ||
request = self.generalPurposeQueue.popFirst() | ||
} | ||
if request != nil { | ||
self.count -= 1 | ||
} | ||
return request | ||
} | ||
|
||
mutating func remove(_ requestID: Request.ID) -> Request? { | ||
let request: Request? | ||
if let eventLoopID = requestID.eventLoopID { | ||
request = 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? | ||
request = self.generalPurposeQueue.remove(at: index) | ||
} else { | ||
request = nil | ||
} | ||
} | ||
if request != nil { | ||
self.count -= 1 | ||
} | ||
return request | ||
} | ||
|
||
mutating func removeAll() -> [Request] { | ||
var result = [Request]() | ||
result = self.eventLoopQueues.flatMap { $0.value } | ||
result.append(contentsOf: self.generalPurposeQueue) | ||
|
||
self.eventLoopQueues.removeAll() | ||
self.generalPurposeQueue.removeAll() | ||
self.count = 0 | ||
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 self.eventLoopQueues[eventLoopID] != nil { | ||
return closure(self.eventLoopQueues[eventLoopID]!) | ||
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. nit: avoid hashing twice here (also we can avoid the bang) |
||
} | ||
return nil | ||
} | ||
} | ||
} |
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") | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.