Skip to content

Commit db26532

Browse files
committed
review fix: add event loop preference argument instead of eventloop
1 parent b7078e8 commit db26532

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ public class HTTPClient {
176176
///
177177
/// - parameters:
178178
/// - request: HTTP request to execute.
179-
/// - eventLoop: NIO Event Loop to use.
179+
/// - eventLoop: NIO Event Loop preferrence.
180180
/// - deadline: Point in time by which the request must complete.
181-
public func execute(request: Request, eventLoop: EventLoop, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
181+
public func execute(request: Request, eventLoopPreferrence: EventLoop, deadline: NIODeadline? = nil) -> EventLoopFuture<Response> {
182182
let accumulator = ResponseAccumulator(request: request)
183-
return self.execute(request: request, delegate: accumulator, eventLoop: eventLoop, deadline: deadline).futureResult
183+
return self.execute(request: request, delegate: accumulator, eventLoop: eventLoopPreferrence, deadline: deadline).futureResult
184184
}
185185

186186
/// Execute arbitrary HTTP request and handle response processing using provided delegate.
@@ -199,9 +199,19 @@ public class HTTPClient {
199199
/// - parameters:
200200
/// - request: HTTP request to execute.
201201
/// - delegate: Delegate to process response parts.
202-
/// - eventLoop: NIO Event Loop to use.
202+
/// - eventLoop: NIO Event Loop preferrence.
203203
/// - deadline: Point in time by which the request must complete.
204-
public func execute<T: HTTPClientResponseDelegate>(request: Request, delegate: T, eventLoop: EventLoop, deadline: NIODeadline? = nil) -> Task<T.Response> {
204+
public func execute<T: HTTPClientResponseDelegate>(request: Request, delegate: T, eventLoop: EventLoopPreferrence, deadline: NIODeadline? = nil) -> Task<T.Response> {
205+
switch eventLoop.preferrence {
206+
case .indifferent:
207+
return self.execute(request: request, delegate: delegate, eventLoop: self.eventLoopGroup.next(), deadline: deadline)
208+
case .prefers(let preferred):
209+
return self.execute(request: request, delegate: delegate, eventLoop: preferred, deadline: deadline)
210+
}
211+
212+
}
213+
214+
private func execute<T: HTTPClientResponseDelegate>(request: Request, delegate: T, eventLoop: EventLoop, deadline: NIODeadline? = nil) -> Task<T.Response> {
205215
let redirectHandler: RedirectHandler<T.Response>?
206216
if self.configuration.followRedirects {
207217
redirectHandler = RedirectHandler<T.Response>(request: request) { newRequest in
@@ -333,6 +343,22 @@ public class HTTPClient {
333343
case createNew
334344
}
335345

346+
/// Specifies how the library will treat event loop passed by the user.
347+
public struct EventLoopPreferrence {
348+
enum Preferrence {
349+
/// Event Loop will be selected by the library.
350+
case indifferent
351+
/// Library will try to use provided event loop if possible.
352+
case prefers(EventLoop)
353+
}
354+
var preferrence: Preferrence
355+
356+
/// Event Loop will be selected by the library.
357+
public static let indifferent = EventLoopPreferrence(preferrence: .indifferent)
358+
/// Library will try to use provided event loop if possible.
359+
public static func prefers(_ eventLoop: EventLoop) -> EventLoopPreferrence { EventLoopPreferrence(preferrence: .prefers(eventLoop)) }
360+
}
361+
336362
/// Timeout configuration
337363
public struct Timeout {
338364
/// Specifies connect timeout.

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ class HTTPClientTests: XCTestCase {
517517
let eventLoop = eventLoopGroup.next()
518518
let delegate = EventLoopValidatingDelegate(eventLoop: eventLoop)
519519
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/get")
520-
let response = try httpClient.execute(request: request, delegate: delegate, eventLoop: eventLoop).wait()
520+
let response = try httpClient.execute(request: request, delegate: delegate, eventLoop: .prefers(eventLoop)).wait()
521521

522522
XCTAssertEqual(true, response)
523523
}

0 commit comments

Comments
 (0)