@@ -12,30 +12,18 @@ internal struct Connection {
1212
1313/// A pool of one or more connections.
1414internal class ConnectionPool {
15- /// Represents the mode of a `ConnectionPool`.
16- internal enum Mode {
17- /// Indicates that we are in single-client mode using the associated pointer to a `mongoc_client_t`.
18- case single( client: OpaquePointer )
19- /// Indicates that we are in pooled mode using the associated pointer to a `mongoc_client_pool_t`.
20- case pooled( pool: OpaquePointer )
15+ /// Represents the state of a `ConnectionPool`.
16+ internal enum State {
17+ /// Indicates that the `ConnectionPool` is open and using the associated pointer to a `mongoc_client_pool_t`.
18+ case open( pool: OpaquePointer )
2119 /// Indicates that the `ConnectionPool` has been closed and contains no connections.
22- case none
20+ case closed
2321 }
2422
25- /// The mode of this `ConnectionPool`.
26- internal private( set) var mode : Mode
23+ /// The state of this `ConnectionPool`.
24+ internal private( set) var state : State
2725
28- /// Initializes the pool in single mode using the provided pointer to a `mongoc_client_t`.
29- internal init ( stealing pointer: OpaquePointer ) {
30- self . mode = . single( client: pointer)
31-
32- // This call may fail, and if it does, either the error api version was already set or the client was derived
33- // from a pool. In either case, the error handling in MongoSwift will be incorrect unless the correct api
34- // version was set by the caller.
35- mongoc_client_set_error_api ( pointer, MONGOC_ERROR_API_VERSION_2)
36- }
37-
38- /// Initializes the pool in pooled mode using the provided `ConnectionString`.
26+ /// Initializes the pool using the provided `ConnectionString`.
3927 internal init ( from connString: ConnectionString , options: TLSOptions ? = nil ) throws {
4028 guard let pool = mongoc_client_pool_new ( connString. _uri) else {
4129 throw UserError . invalidArgumentError ( message: " libmongoc not built with TLS support " )
@@ -45,7 +33,7 @@ internal class ConnectionPool {
4533 throw RuntimeError . internalError ( message: " Could not configure error handling on client pool " )
4634 }
4735
48- self . mode = . pooled ( pool: pool)
36+ self . state = . open ( pool: pool)
4937 if let options = options {
5038 try self . setTLSOptions ( options)
5139 }
@@ -58,37 +46,31 @@ internal class ConnectionPool {
5846
5947 /// Closes the pool, cleaning up underlying resources.
6048 internal func close( ) {
61- switch self . mode {
62- case let . single( clientHandle) :
63- mongoc_client_destroy ( clientHandle)
64- case let . pooled( pool) :
49+ switch self . state {
50+ case let . open( pool) :
6551 mongoc_client_pool_destroy ( pool)
66- case . none :
52+ case . closed :
6753 return
6854 }
69- self . mode = . none
55+ self . state = . closed
7056 }
7157
7258 /// Checks out a connection. This connection must be returned to the pool via `checkIn`.
7359 internal func checkOut( ) throws -> Connection {
74- switch self . mode {
75- case let . single( clientHandle) :
76- return Connection ( clientHandle)
77- case let . pooled( pool) :
60+ switch self . state {
61+ case let . open( pool) :
7862 return Connection ( mongoc_client_pool_pop ( pool) )
79- case . none :
63+ case . closed :
8064 throw RuntimeError . internalError ( message: " ConnectionPool was already closed " )
8165 }
8266 }
8367
8468 /// Returns a connection to the pool.
8569 internal func checkIn( _ connection: Connection ) {
86- switch self . mode {
87- case . single:
88- return
89- case let . pooled( pool) :
70+ switch self . state {
71+ case let . open( pool) :
9072 mongoc_client_pool_push ( pool, connection. clientHandle)
91- case . none :
73+ case . closed :
9274 fatalError ( " ConnectionPool was already closed " )
9375 }
9476 }
@@ -128,12 +110,10 @@ internal class ConnectionPool {
128110 if let invalidHosts = options. allowInvalidHostnames {
129111 opts. allow_invalid_hostname = invalidHosts
130112 }
131- switch self . mode {
132- case let . single( clientHandle) :
133- mongoc_client_set_ssl_opts ( clientHandle, & opts)
134- case let . pooled( pool) :
113+ switch self . state {
114+ case let . open( pool) :
135115 mongoc_client_pool_set_ssl_opts ( pool, & opts)
136- case . none :
116+ case . closed :
137117 throw RuntimeError . internalError ( message: " ConnectionPool was already closed " )
138118 }
139119 }
0 commit comments