MCPcopy
hub / github.com/redis/redis-py / ensure_connection

Method ensure_connection

redis/asyncio/connection.py:1630–1644  ·  view source on GitHub ↗

Ensure that the connection object is connected and valid

(self, connection: AbstractConnection)

Source from the content-addressed store, hash-verified

1628 return self.connection_class(**self.connection_kwargs)
1629
1630 async def ensure_connection(self, connection: AbstractConnection):
1631 """Ensure that the connection object is connected and valid"""
1632 await connection.connect()
1633 # connections that the pool provides should be ready to send
1634 # a command. if not, the connection was either returned to the
1635 # pool before all data has been read or the socket has been
1636 # closed. either way, reconnect and verify everything is good.
1637 try:
1638 if await connection.can_read():
1639 raise ConnectionError("Connection has data") from None
1640 except (ConnectionError, TimeoutError, OSError):
1641 await connection.disconnect()
1642 await connection.connect()
1643 if await connection.can_read():
1644 raise ConnectionError("Connection not ready") from None
1645
1646 async def release(self, connection: AbstractConnection):
1647 """Releases the connection back to the pool"""

Callers 2

get_connectionMethod · 0.95
get_connectionMethod · 0.80

Calls 4

ConnectionErrorClass · 0.90
connectMethod · 0.45
can_readMethod · 0.45
disconnectMethod · 0.45

Tested by

no test coverage detected