@@ -176,11 +176,11 @@ public class HTTPClient {
176
176
///
177
177
/// - parameters:
178
178
/// - request: HTTP request to execute.
179
- /// - eventLoop: NIO Event Loop to use .
179
+ /// - eventLoop: NIO Event Loop preferrence .
180
180
/// - 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 > {
182
182
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
184
184
}
185
185
186
186
/// Execute arbitrary HTTP request and handle response processing using provided delegate.
@@ -199,9 +199,19 @@ public class HTTPClient {
199
199
/// - parameters:
200
200
/// - request: HTTP request to execute.
201
201
/// - delegate: Delegate to process response parts.
202
- /// - eventLoop: NIO Event Loop to use .
202
+ /// - eventLoop: NIO Event Loop preferrence .
203
203
/// - 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 > {
205
215
let redirectHandler : RedirectHandler < T . Response > ?
206
216
if self . configuration. followRedirects {
207
217
redirectHandler = RedirectHandler < T . Response > ( request: request) { newRequest in
@@ -333,6 +343,22 @@ public class HTTPClient {
333
343
case createNew
334
344
}
335
345
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
+
336
362
/// Timeout configuration
337
363
public struct Timeout {
338
364
/// Specifies connect timeout.
0 commit comments