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

Method get_connection

redis/asyncio/connection.py:1814–1854  ·  view source on GitHub ↗

Gets a connection from the pool, blocking until one is available

(self, command_name=None, *keys, **options)

Source from the content-addressed store, hash-verified

1812 version="5.3.0",
1813 )
1814 async def get_connection(self, command_name=None, *keys, **options):
1815 """Gets a connection from the pool, blocking until one is available"""
1816 # Start timing for wait time observability
1817 start_time_acquired = time.monotonic()
1818
1819 try:
1820 async with self._condition:
1821 async with async_timeout(self.timeout):
1822 await self._condition.wait_for(self.can_get_connection)
1823 # Track connection count before to detect if a new connection is created
1824 connections_before = len(self._available_connections) + len(
1825 self._in_use_connections
1826 )
1827 start_time_created = time.monotonic()
1828 connection = super().get_available_connection()
1829 connections_after = len(self._available_connections) + len(
1830 self._in_use_connections
1831 )
1832 is_created = connections_after > connections_before
1833 except asyncio.TimeoutError as err:
1834 raise ConnectionError("No connection available.") from err
1835
1836 # We now perform the connection check outside of the lock.
1837 try:
1838 await self.ensure_connection(connection)
1839
1840 if is_created:
1841 await record_connection_create_time(
1842 connection_pool=self,
1843 duration_seconds=time.monotonic() - start_time_created,
1844 )
1845
1846 await record_connection_wait_time(
1847 pool_name=get_pool_name(self),
1848 duration_seconds=time.monotonic() - start_time_acquired,
1849 )
1850
1851 return connection
1852 except BaseException:
1853 await self.release(connection)
1854 raise
1855
1856 async def release(self, connection: AbstractConnection):
1857 """Releases the connection back to the pool."""

Callers

nothing calls this directly

Calls 8

releaseMethod · 0.95
ConnectionErrorClass · 0.90
get_pool_nameFunction · 0.85
monotonicMethod · 0.80
ensure_connectionMethod · 0.80

Tested by

no test coverage detected