Skip to content

Commit 4068ee5

Browse files
authored
EventLoop preferences (#415)
1 parent 7d3c578 commit 4068ee5

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

‎Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool.swift‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,15 @@ extension HTTPConnectionPool{
149149
}
150150

151151
varrequiredEventLoop:EventLoop?{
152-
switchself.req.eventLoopPreference.preference {
153-
case.indifferent,.delegate:
154-
returnnil
155-
case.delegateAndChannel(on:let eventLoop),.testOnly_exact(channelOn:let eventLoop, delegateOn: _):
156-
return eventLoop
157-
}
152+
self.req.requiredEventLoop
153+
}
154+
155+
varpreferredEventLoop:EventLoop{
156+
self.req.preferredEventLoop
157+
}
158+
159+
varconnectionDeadline:NIODeadline?{
160+
self.req.connectionDeadline
158161
}
159162

160163
func __testOnly_wrapped_request()->HTTPSchedulableRequest{

‎Sources/AsyncHTTPClient/ConnectionPool/HTTPExecutableRequest.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ protocol HTTPSchedulableRequest: HTTPExecutableRequest{
146146
/// A connection to run this task on needs to be found before this deadline!
147147
varconnectionDeadline:NIODeadline{get}
148148

149-
/// The task's `EventLoop` preference
150-
vareventLoopPreference:HTTPClient.EventLoopPreference{get}
149+
/// The user has expressed an intent for this request to be executed on this EventLoop. If a
150+
/// connection is available on another one, just use the one handy.
151+
varpreferredEventLoop:EventLoop{get}
152+
153+
/// The user required the request to be executed on a connection that is handled by this EventLoop.
154+
varrequiredEventLoop:EventLoop?{get}
151155

152156
/// Informs the task, that it was queued for execution
153157
///

‎Sources/AsyncHTTPClient/RequestBag.swift‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,26 @@ extension RequestBag: HTTPSchedulableRequest{
335335
}
336336

337337
extensionRequestBag:HTTPExecutableRequest{
338+
varrequiredEventLoop:EventLoop?{
339+
switchself.eventLoopPreference.preference {
340+
case.indifferent,.delegate:
341+
returnnil
342+
case.delegateAndChannel(on:let eventLoop),.testOnly_exact(channelOn:let eventLoop, delegateOn: _):
343+
return eventLoop
344+
}
345+
}
346+
347+
varpreferredEventLoop:EventLoop{
348+
switchself.eventLoopPreference.preference {
349+
case.indifferent:
350+
returnself.task.eventLoop
351+
case.delegate(let eventLoop),
352+
.delegateAndChannel(on:let eventLoop),
353+
.testOnly_exact(channelOn:let eventLoop, delegateOn: _):
354+
return eventLoop
355+
}
356+
}
357+
338358
func willExecuteRequest(_ executor:HTTPRequestExecutor){
339359
ifself.task.eventLoop.inEventLoop {
340360
self.willExecuteRequest0(executor)

‎Tests/AsyncHTTPClientTests/HTTPConnectionPool+RequestQueueTests.swift‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class HTTPConnectionPool_RequestQueueTests: XCTestCase{
2424
varqueue=HTTPConnectionPool.RequestQueue()
2525
XCTAssertTrue(queue.isEmpty)
2626
XCTAssertEqual(queue.count,0)
27-
letreq1=MockScheduledRequest(eventLoopPreference:.indifferent)
27+
letreq1=MockScheduledRequest(requiredEventLoop:nil)
2828
letreq1ID= queue.push(.init(req1))
2929
XCTAssertFalse(queue.isEmpty)
3030
XCTAssertFalse(queue.isEmpty(for:nil))
3131
XCTAssertEqual(queue.count,1)
3232
XCTAssertEqual(queue.count(for:nil),1)
3333

34-
letreq2=MockScheduledRequest(eventLoopPreference:.indifferent)
34+
letreq2=MockScheduledRequest(requiredEventLoop:nil)
3535
letreq2ID= queue.push(.init(req2))
3636
XCTAssertEqual(queue.count,2)
3737

@@ -47,7 +47,7 @@ class HTTPConnectionPool_RequestQueueTests: XCTestCase{
4747

4848
XCTAssertTrue(queue.isEmpty(for: eventLoop))
4949
XCTAssertEqual(queue.count(for: eventLoop),0)
50-
letreq3=MockScheduledRequest(eventLoopPreference:.delegateAndChannel(on:eventLoop))
50+
letreq3=MockScheduledRequest(requiredEventLoop:eventLoop)
5151
letreq3ID= queue.push(.init(req3))
5252
XCTAssertFalse(queue.isEmpty(for: eventLoop))
5353
XCTAssertEqual(queue.count(for: eventLoop),1)
@@ -60,13 +60,13 @@ class HTTPConnectionPool_RequestQueueTests: XCTestCase{
6060
XCTAssertTrue(queue.isEmpty)
6161
XCTAssertEqual(queue.count,0)
6262

63-
letreq4=MockScheduledRequest(eventLoopPreference:.delegateAndChannel(on:eventLoop))
63+
letreq4=MockScheduledRequest(requiredEventLoop:eventLoop)
6464
letreq4ID= queue.push(.init(req4))
6565
XCTAssert(queue.remove(req4ID)?.__testOnly_wrapped_request()=== req4)
6666

67-
letreq5=MockScheduledRequest(eventLoopPreference:.indifferent)
67+
letreq5=MockScheduledRequest(requiredEventLoop:nil)
6868
queue.push(.init(req5))
69-
letreq6=MockScheduledRequest(eventLoopPreference:.delegateAndChannel(on:eventLoop))
69+
letreq6=MockScheduledRequest(requiredEventLoop:eventLoop)
7070
queue.push(.init(req6))
7171
letall= queue.removeAll()
7272
lettestSet= all.map{ $0.__testOnly_wrapped_request()}
@@ -82,13 +82,15 @@ class HTTPConnectionPool_RequestQueueTests: XCTestCase{
8282
}
8383

8484
privateclassMockScheduledRequest:HTTPSchedulableRequest{
85-
init(eventLoopPreference:HTTPClient.EventLoopPreference){
86-
self.eventLoopPreference = eventLoopPreference
85+
letrequiredEventLoop:EventLoop?
86+
87+
init(requiredEventLoop:EventLoop?){
88+
self.requiredEventLoop = requiredEventLoop
8789
}
8890

8991
varlogger:Logger{preconditionFailure("Unimplemented")}
9092
varconnectionDeadline:NIODeadline{preconditionFailure("Unimplemented")}
91-
leteventLoopPreference:HTTPClient.EventLoopPreference
93+
varpreferredEventLoop:EventLoop{preconditionFailure("Unimplemented")}
9294

9395
func requestWasQueued(_:HTTPRequestScheduler){
9496
preconditionFailure("Unimplemented")

0 commit comments

Comments
(0)