Releases the connection back to the pool
(self, connection: "Connection")
| 3226 | return connection |
| 3227 | |
| 3228 | def release(self, connection: "Connection") -> None: |
| 3229 | "Releases the connection back to the pool" |
| 3230 | self._checkpid() |
| 3231 | with self._lock: |
| 3232 | try: |
| 3233 | self._in_use_connections.remove(connection) |
| 3234 | except KeyError: |
| 3235 | # Gracefully fail when a connection is returned to this pool |
| 3236 | # that the pool doesn't actually own |
| 3237 | return |
| 3238 | |
| 3239 | if self.owns_connection(connection): |
| 3240 | if connection.should_reconnect(): |
| 3241 | connection.disconnect() |
| 3242 | self._available_connections.append(connection) |
| 3243 | self._event_dispatcher.dispatch( |
| 3244 | AfterConnectionReleasedEvent(connection) |
| 3245 | ) |
| 3246 | |
| 3247 | # Record state transition: USED -> IDLE |
| 3248 | pool_name = get_pool_name(self) |
| 3249 | record_connection_count( |
| 3250 | pool_name=pool_name, |
| 3251 | connection_state=ConnectionState.USED, |
| 3252 | counter=-1, |
| 3253 | ) |
| 3254 | record_connection_count( |
| 3255 | pool_name=pool_name, |
| 3256 | connection_state=ConnectionState.IDLE, |
| 3257 | counter=1, |
| 3258 | ) |
| 3259 | else: |
| 3260 | # Pool doesn't own this connection, do not add it back |
| 3261 | # to the pool. |
| 3262 | # The created connections count should not be changed, |
| 3263 | # because the connection was not created by the pool. |
| 3264 | # Still need to decrement USED since it was counted in get_connection() |
| 3265 | connection.disconnect() |
| 3266 | record_connection_count( |
| 3267 | pool_name="unknown_pool", |
| 3268 | connection_state=ConnectionState.USED, |
| 3269 | counter=-1, |
| 3270 | ) |
| 3271 | return |
| 3272 | |
| 3273 | def owns_connection(self, connection: "Connection") -> int: |
| 3274 | return connection.pid == self.pid |