Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/MongoSwift/ConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class ConnectionPool {
}

/// Closes the pool, cleaning up underlying resources. This method blocks as it sends `endSessions` to the server.
internal func close() {
internal func shutdown() {
switch self.state {
case let .open(pool):
mongoc_client_pool_destroy(pool)
Expand Down
27 changes: 22 additions & 5 deletions Sources/MongoSwift/MongoClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,36 @@ public class MongoClient {
assert(self.isClosed, "MongoClient was not closed before deinitialization")
}

/// Closes this `MongoClient`. Call this method exactly once when you are finished using the client. You must
/// ensure that all operations using the client have completed before calling this. The returned future must be
/// fulfilled before the `EventLoopGroup` provided to this client's constructor is shut down.
public func close() -> EventLoopFuture<Void> {
/// Shuts this `MongoClient` down, closing all connection to the server and cleaning up internal state.
/// Call this method exactly once when you are finished using the client. You must ensure that all operations
/// using the client have completed before calling this. The returned future must be fulfilled before the
/// `EventLoopGroup` provided to this client's constructor is shut down.
public func shutdown() -> EventLoopFuture<Void> {
return self.operationExecutor.execute {
self.connectionPool.close()
self.connectionPool.shutdown()
self.isClosed = true
}
.flatMap {
self.operationExecutor.close()
}
}

/**
* Shuts this `MongoClient` down in a blocking fashion, closing all connections to the server and cleaning up
* internal state.
*
* Call this method exactly once when you are finished using the client. You must ensure that all operations
* using the client have completed before calling this.
*
* This method must complete before the `EventLoopGroup` provided to this client's constructor is shut down.
*/
public func syncShutdown() {
self.connectionPool.shutdown()
self.isClosed = true
// TODO: SWIFT-349 log any errors encountered here.
try? self.operationExecutor.syncClose()
}

/// Starts a new `ClientSession` with the provided options. When you are done using this session, you must call
/// `ClientSession.end()` on it.
public func startSession(options: ClientSessionOptions? = nil) -> ClientSession {
Expand Down
5 changes: 5 additions & 0 deletions Sources/MongoSwift/Operations/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal class OperationExecutor {
return promise.futureResult
}

/// Closes the executor's underlying thread pool synchronously.
internal func syncClose() throws {
try self.threadPool.syncShutdownGracefully()
}

internal func execute<T: Operation>(
_ operation: T,
using connection: Connection? = nil,
Expand Down
2 changes: 1 addition & 1 deletion Sources/MongoSwiftSync/MongoClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class MongoClient {

deinit {
do {
try self.asyncClient.close().wait()
try self.asyncClient.shutdown().wait()
} catch {
assertionFailure("Error closing async client: \(error)")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MongoSwiftTests/AsyncTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension MongoClient {

internal func syncCloseOrFail() {
do {
try self.close().wait()
try self.shutdown().wait()
} catch {
XCTFail("Error closing test client: \(error)")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MongoSwiftTests/MongoClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class MongoClientTests: MongoSwiftTestCase {
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { elg.syncShutdownOrFail() }
let client = try MongoClient(using: elg)
try client.close().wait()
try client.shutdown().wait()
expect(try client.listDatabases().wait()).to(throwError(MongoClient.ClosedClientError))
}

Expand Down