MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _perished

Method _perished

pymongo/asynchronous/pool.py:1405–1440  ·  view source on GitHub ↗

Return True and close the connection if it is "perished". This side-effecty function checks if this socket has been idle for for longer than the max idle time, or if the socket has been closed by some external network error, or if the socket's generation is outdated.

(self, conn: AsyncConnection)

Source from the content-addressed store, hash-verified

1403 self.size_cond.notify()
1404
1405 async def _perished(self, conn: AsyncConnection) -> bool:
1406 """Return True and close the connection if it is "perished".
1407
1408 This side-effecty function checks if this socket has been idle for
1409 for longer than the max idle time, or if the socket has been closed by
1410 some external network error, or if the socket's generation is outdated.
1411
1412 Checking sockets lets us avoid seeing *some*
1413 :class:`~pymongo.errors.AutoReconnect` exceptions on server
1414 hiccups, etc. We only check if the socket was closed by an external
1415 error if it has been > 1 second since the socket was checked into the
1416 pool to keep performance reasonable -
1417 we can't avoid AutoReconnects completely anyway.
1418 """
1419 idle_time_seconds = conn.idle_time_seconds()
1420 # If socket is idle, open a new one.
1421 if (
1422 self.opts.max_idle_time_seconds is not None
1423 and idle_time_seconds > self.opts.max_idle_time_seconds
1424 ):
1425 await conn.close_conn(ConnectionClosedReason.IDLE)
1426 return True
1427
1428 check_interval_seconds = self._check_interval_seconds
1429 if check_interval_seconds is not None and (
1430 check_interval_seconds == 0 or idle_time_seconds > check_interval_seconds
1431 ):
1432 if conn.conn_closed():
1433 await conn.close_conn(ConnectionClosedReason.ERROR)
1434 return True
1435
1436 if self.stale_generation(conn.generation, conn.service_id):
1437 await conn.close_conn(ConnectionClosedReason.STALE)
1438 return True
1439
1440 return False
1441
1442 def _raise_wait_queue_timeout(self, checkout_started_time: float) -> NoReturn:
1443 listeners = self.opts._event_listeners

Callers 1

_get_connMethod · 0.95

Calls 4

stale_generationMethod · 0.95
idle_time_secondsMethod · 0.45
close_connMethod · 0.45
conn_closedMethod · 0.45

Tested by

no test coverage detected