Skip to content

Commit

Permalink
Added httpClientProvider createNewWithEventLoopGroup (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Oct 30, 2020
1 parent 809e6cb commit 6f7661b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
14 changes: 7 additions & 7 deletions Sources/SotoCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public final class AWSClient {
/// HTTP Client will be provided by the user. Owner of this group is responsible for its lifecycle. Any HTTPClient that conforms to
/// `AWSHTTPClient` can be specified here including AsyncHTTPClient
case shared(AWSHTTPClient)
/// HTTP Client will be created by the client using provided EventLoopGroup. When `shutdown` is called, created `HTTPClient`
/// will be shut down as well.
case createNewWithEventLoopGroup(EventLoopGroup)
/// HTTP Client will be created by the client. When `shutdown` is called, created `HTTPClient` will be shut down as well.
case createNew
}
Expand Down Expand Up @@ -97,8 +100,10 @@ public final class AWSClient {
switch httpClientProvider {
case .shared(let providedHTTPClient):
self.httpClient = providedHTTPClient
case .createNewWithEventLoopGroup(let elg):
self.httpClient = AsyncHTTPClient.HTTPClient(eventLoopGroupProvider: .shared(elg))
case .createNew:
self.httpClient = AWSClient.createHTTPClient()
self.httpClient = AsyncHTTPClient.HTTPClient(eventLoopGroupProvider: .createNew)
}

self.credentialProvider = credentialProviderFactory.createProvider(context: .init(
Expand Down Expand Up @@ -159,7 +164,7 @@ public final class AWSClient {
credentialProvider.shutdown(on: eventLoop).whenComplete { _ in
// if httpClient was created by AWSClient then it is required to shutdown the httpClient.
switch self.httpClientProvider {
case .createNew:
case .createNew, .createNewWithEventLoopGroup:
self.httpClient.shutdown(queue: queue) { error in
if let error = error {
self.clientLogger.error("Error shutting down HTTP client", metadata: [
Expand Down Expand Up @@ -218,11 +223,6 @@ extension AWSClient {

return promise.futureResult
}

/// create HTTPClient
fileprivate static func createHTTPClient() -> AWSHTTPClient {
return AsyncHTTPClient.HTTPClient(eventLoopGroupProvider: .createNew)
}
}

// public facing apis
Expand Down
30 changes: 25 additions & 5 deletions Tests/SotoCoreTests/AWSClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,27 @@ class AWSClientTests: XCTestCase {
}

func testShutdown() {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let eventLoop = eventLoopGroup.next()
// currently only testing with httpClientProvider: .shared(httpClient)

let client = createAWSClient(httpClientProvider: .shared(httpClient))
let promise: EventLoopPromise<Void> = httpClient.eventLoopGroup.next().makePromise()
client.shutdown { error in
if let error = error {
promise.completeWith(.failure(error))
} else {
promise.completeWith(.success(()))
}
}
XCTAssertNoThrow(try promise.futureResult.wait())
}

func testShutdownWithEventLoop() {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let eventLoop = eventLoopGroup.next()

let client = createAWSClient(httpClientProvider: .createNewWithEventLoopGroup(eventLoopGroup))
let promise: EventLoopPromise<Void> = eventLoop.makePromise()
client.shutdown { error in
if let error = error {
Expand Down Expand Up @@ -173,9 +187,15 @@ class AWSClientTests: XCTestCase {
let i: Int64
}
do {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let awsServer = AWSTestServer(serviceProtocol: .json)
let config = createServiceConfig(serviceProtocol: .json(version: "1.1"), endpoint: awsServer.address)
let client = createAWSClient(credentialProvider: .empty, middlewares: [AWSLoggingMiddleware()])
let client = createAWSClient(
credentialProvider: .empty,
middlewares: [AWSLoggingMiddleware()],
httpClientProvider: .createNewWithEventLoopGroup(eventLoopGroup)
)
defer {
XCTAssertNoThrow(try client.syncShutdown())
XCTAssertNoThrow(try awsServer.stop())
Expand Down

0 comments on commit 6f7661b

Please sign in to comment.