( session: ClientSession, options?: EndSessionOptions )
| 981 | } |
| 982 | |
| 983 | export function maybeClearPinnedConnection( |
| 984 | session: ClientSession, |
| 985 | options?: EndSessionOptions |
| 986 | ): void { |
| 987 | // unpin a connection if it has been pinned |
| 988 | const conn = session.pinnedConnection; |
| 989 | const error = options?.error; |
| 990 | |
| 991 | if ( |
| 992 | session.inTransaction() && |
| 993 | error && |
| 994 | error instanceof MongoError && |
| 995 | error.hasErrorLabel(MongoErrorLabel.TransientTransactionError) |
| 996 | ) { |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | const topology = session.client.topology; |
| 1001 | // NOTE: the spec talks about what to do on a network error only, but the tests seem to |
| 1002 | // to validate that we don't unpin on _all_ errors? |
| 1003 | if (conn && topology != null) { |
| 1004 | const servers = Array.from(topology.s.servers.values()); |
| 1005 | const loadBalancer = servers[0]; |
| 1006 | |
| 1007 | if (options?.error == null || options?.force) { |
| 1008 | loadBalancer.pool.checkIn(conn); |
| 1009 | session.pinnedConnection = undefined; |
| 1010 | conn.emit( |
| 1011 | UNPINNED, |
| 1012 | session.transaction.state !== TxnState.NO_TRANSACTION |
| 1013 | ? ConnectionPoolMetrics.TXN |
| 1014 | : ConnectionPoolMetrics.CURSOR |
| 1015 | ); |
| 1016 | |
| 1017 | if (options?.forceClear) { |
| 1018 | loadBalancer.pool.clear({ serviceId: conn.serviceId }); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | function isMaxTimeMSExpiredError(err: MongoError): boolean { |
| 1025 | if (err == null || !(err instanceof MongoServerError)) { |
no test coverage detected