Skip to content

Commit

Permalink
publicize client's elg (AsyncHttpClient#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Apr 26, 2019
1 parent 92d4e9c commit b9c5535
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Sources/NIOHTTPClient/SwiftNIOHTTP.swift
Expand Up @@ -24,19 +24,18 @@ public enum EventLoopGroupProvider {
}

public class HTTPClient {
public let eventLoopGroup: EventLoopGroup
let eventLoopGroupProvider: EventLoopGroupProvider
let group: EventLoopGroup
let configuration: Configuration

let isShutdown = Atomic<Bool>(value: false)

public init(eventLoopGroupProvider: EventLoopGroupProvider, configuration: Configuration = Configuration()) {
self.eventLoopGroupProvider = eventLoopGroupProvider
switch self.eventLoopGroupProvider {
case .shared(let group):
self.group = group
self.eventLoopGroup = group
case .createNew:
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
}
self.configuration = configuration
}
Expand All @@ -57,7 +56,7 @@ public class HTTPClient {
return
case .createNew:
if self.isShutdown.compareAndExchange(expected: false, desired: true) {
try self.group.syncShutdownGracefully()
try self.eventLoopGroup.syncShutdownGracefully()
} else {
throw HTTPClientError.alreadyShutdown
}
Expand All @@ -69,7 +68,7 @@ public class HTTPClient {
let request = try Request(url: url, method: .GET)
return self.execute(request: request)
} catch {
return self.group.next().makeFailedFuture(error)
return self.eventLoopGroup.next().makeFailedFuture(error)
}
}

Expand All @@ -78,7 +77,7 @@ public class HTTPClient {
let request = try HTTPClient.Request(url: url, method: .POST, body: body)
return self.execute(request: request)
} catch {
return self.group.next().makeFailedFuture(error)
return self.eventLoopGroup.next().makeFailedFuture(error)
}
}

Expand All @@ -87,7 +86,7 @@ public class HTTPClient {
let request = try HTTPClient.Request(url: url, method: .PATCH, body: body)
return self.execute(request: request)
} catch {
return self.group.next().makeFailedFuture(error)
return self.eventLoopGroup.next().makeFailedFuture(error)
}
}

Expand All @@ -96,7 +95,7 @@ public class HTTPClient {
let request = try HTTPClient.Request(url: url, method: .PUT, body: body)
return self.execute(request: request)
} catch {
return self.group.next().makeFailedFuture(error)
return self.eventLoopGroup.next().makeFailedFuture(error)
}
}

Expand All @@ -105,7 +104,7 @@ public class HTTPClient {
let request = try Request(url: url, method: .DELETE)
return self.execute(request: request)
} catch {
return self.group.next().makeFailedFuture(error)
return self.eventLoopGroup.next().makeFailedFuture(error)
}
}

Expand All @@ -117,7 +116,7 @@ public class HTTPClient {
public func execute<T: HTTPClientResponseDelegate>(request: Request, delegate: T, timeout: Timeout? = nil) -> Task<T.Response> {
let timeout = timeout ?? configuration.timeout

let promise: EventLoopPromise<T.Response> = group.next().makePromise()
let promise: EventLoopPromise<T.Response> = self.eventLoopGroup.next().makePromise()

let redirectHandler: RedirectHandler<T.Response>?
if self.configuration.followRedirects {
Expand All @@ -130,7 +129,7 @@ public class HTTPClient {

let task = Task(future: promise.futureResult)

var bootstrap = ClientBootstrap(group: group)
var bootstrap = ClientBootstrap(group: self.eventLoopGroup)
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
.channelInitializer { channel in
let encoder = HTTPRequestEncoder()
Expand Down

0 comments on commit b9c5535

Please sign in to comment.