Skip to content

Commit 597197a

Browse files
authored
SWIFT-717 Add syncShutdown method to MongoClient (#402)
1 parent fcb54e2 commit 597197a

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

Sources/MongoSwift/ConnectionPool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal class ConnectionPool {
5959
}
6060

6161
/// Closes the pool, cleaning up underlying resources. This method blocks as it sends `endSessions` to the server.
62-
internal func close() {
62+
internal func shutdown() {
6363
switch self.state {
6464
case let .open(pool):
6565
mongoc_client_pool_destroy(pool)

Sources/MongoSwift/MongoClient.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,36 @@ public class MongoClient {
288288
assert(self.isClosed, "MongoClient was not closed before deinitialization")
289289
}
290290

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

305+
/**
306+
* Shuts this `MongoClient` down in a blocking fashion, closing all connections to the server and cleaning up
307+
* internal state.
308+
*
309+
* Call this method exactly once when you are finished using the client. You must ensure that all operations
310+
* using the client have completed before calling this.
311+
*
312+
* This method must complete before the `EventLoopGroup` provided to this client's constructor is shut down.
313+
*/
314+
public func syncShutdown() {
315+
self.connectionPool.shutdown()
316+
self.isClosed = true
317+
// TODO: SWIFT-349 log any errors encountered here.
318+
try? self.operationExecutor.syncClose()
319+
}
320+
304321
/// Starts a new `ClientSession` with the provided options. When you are done using this session, you must call
305322
/// `ClientSession.end()` on it.
306323
public func startSession(options: ClientSessionOptions? = nil) -> ClientSession {

Sources/MongoSwift/Operations/Operation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ internal class OperationExecutor {
3737
return promise.futureResult
3838
}
3939

40+
/// Closes the executor's underlying thread pool synchronously.
41+
internal func syncClose() throws {
42+
try self.threadPool.syncShutdownGracefully()
43+
}
44+
4045
internal func execute<T: Operation>(
4146
_ operation: T,
4247
using connection: Connection? = nil,

Sources/MongoSwiftSync/MongoClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class MongoClient {
5353

5454
deinit {
5555
do {
56-
try self.asyncClient.close().wait()
56+
try self.asyncClient.shutdown().wait()
5757
} catch {
5858
assertionFailure("Error closing async client: \(error)")
5959
}

Tests/MongoSwiftTests/AsyncTestUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension MongoClient {
2222

2323
internal func syncCloseOrFail() {
2424
do {
25-
try self.close().wait()
25+
try self.shutdown().wait()
2626
} catch {
2727
XCTFail("Error closing test client: \(error)")
2828
}

Tests/MongoSwiftTests/MongoClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class MongoClientTests: MongoSwiftTestCase {
88
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
99
defer { elg.syncShutdownOrFail() }
1010
let client = try MongoClient(using: elg)
11-
try client.close().wait()
11+
try client.shutdown().wait()
1212
expect(try client.listDatabases().wait()).to(throwError(MongoClient.ClosedClientError))
1313
}
1414

0 commit comments

Comments
 (0)