async def test(): pool = connpool.Pool( connect=self.make_fake_connect(), disconnect=self.make_fake_disconnect(), max_capacity=5, ) conn1 = await pool.acquire("block_a") args = await logger.logs.get() self.assertIn("established", args) self.assertIn("block_a", args) conn2 = await pool.acquire("block_b") start = time.monotonic() args = await logger.logs.get() self.assertIn("established", args) self.assertIn("block_b", args) self.assertLess(time.monotonic() - start, 0.2) pool.release("block_a", conn1, discard=True) start = time.monotonic() args = await logger.logs.get() self.assertIn("1 were discarded", args) self.assertIn("block_a", args) self.assertGreater(time.monotonic() - start, 0.2) pool.release("block_b", conn2, discard=True) start = time.monotonic() args = await logger.logs.get() self.assertIn("discarded", args) self.assertIn("block_b", args) self.assertLess(time.monotonic() - start, 0.2)
async def test(): pool = connpool.Pool( connect=fake_connect, disconnect=fake_disconnect, max_capacity=5, ) with self.assertRaises(error_type): await pool.acquire("block_a") self.assertEqual(connect_called_num, expected_connects) self.assertEqual(disconnect_called_num, 0) with self.assertRaises(error_type): await pool.acquire("block_a") self.assertEqual(connect_called_num, expected_connects + 1) self.assertEqual(disconnect_called_num, 0)
async def test(delay: float): event = asyncio.Event() pool = connpool.Pool( connect=self.make_fake_connect(), disconnect=self.make_fake_disconnect(), max_capacity=10, ) async with taskgroup.TaskGroup() as g: g.create_task(q0(pool, event)) await asyncio.sleep(delay) g.create_task(q1(pool, event)) await asyncio.sleep(delay) g.create_task(q2(pool, event))
async def test(delay: float): e1 = asyncio.Event() e2 = asyncio.Event() e3 = asyncio.Event() pool = connpool.Pool( connect=self.make_fake_connect(), disconnect=self.make_fake_disconnect(), max_capacity=5, ) async with taskgroup.TaskGroup() as g: for _ in range(4): g.create_task(q('A', pool, wait_event=e1)) await asyncio.sleep(0.1) g.create_task(q('B', pool, set_event=e2, wait_event=e3)) await e2.wait() g.create_task(q('B', pool, set_event=e3)) await asyncio.sleep(0.1) e1.set()
async def test(): pool = connpool.Pool( connect=fake_connect, disconnect=self.make_fake_disconnect(), max_capacity=2, ) # fill the pool conn1 = await pool.acquire("block_a") self.assertEqual(await logger.logs.get(), ('established', 'block_a')) conn2 = await pool.acquire("block_a") self.assertEqual(await logger.logs.get(), ('established', 'block_a')) pool.release("block_a", conn1) pool.release("block_a", conn2) # steal a connection from block_a, with retries await pool.acquire("block_b") logs = [await logger.logs.get() for i in range(3)] self.assertIn(('transferred out', 'block_a'), logs) self.assertIn(('discarded', 'block_a'), logs) self.assertIn(('transferred in', 'block_b'), logs)
def __init__( self, *, loop, cluster, runstate_dir, internal_runstate_dir, max_backend_connections, compiler_pool_size, nethost, netport, auto_shutdown: bool = False, echo_runtime_info: bool = False, status_sink: Optional[Callable[[str], None]] = None, startup_script: Optional[srvargs.StartupScript] = None, ): self._loop = loop # Used to tag PG notifications to later disambiguate them. self._server_id = str(uuid.uuid4()) self._serving = False self._initing = False self._cluster = cluster self._pg_addr = self._get_pgaddr() inst_params = cluster.get_runtime_params().instance_params self._tenant_id = inst_params.tenant_id # 1 connection is reserved for the system DB pool_capacity = max_backend_connections - 1 self._pg_pool = connpool.Pool( connect=self._pg_connect, disconnect=self._pg_disconnect, max_capacity=pool_capacity, ) # DB state will be initialized in init(). self._dbindex = None self._runstate_dir = runstate_dir self._internal_runstate_dir = internal_runstate_dir self._max_backend_connections = max_backend_connections self._compiler_pool_size = compiler_pool_size self._listen_host = nethost self._listen_port = netport self._sys_auth: Tuple[Any, ...] = tuple() # Shutdown the server after the last management # connection has disconnected self._auto_shutdown = auto_shutdown self._echo_runtime_info = echo_runtime_info self._status_sink = status_sink self._startup_script = startup_script # Never use `self.__sys_pgcon` directly; get it via # `await self._acquire_sys_pgcon()`. self.__sys_pgcon = None self._roles = immutables.Map() self._instance_data = immutables.Map() self._sys_queries = immutables.Map() self._devmode = devmode.is_in_dev_mode() self._binary_proto_id_counter = 0 self._binary_proto_num_connections = 0 self._accepting_connections = False self._servers = [] self._http_query_cache = cache.StatementsCache( maxsize=defines.HTTP_PORT_QUERY_CACHE_SIZE) self._http_last_minute_requests = windowedsum.WindowedSum() self._http_request_logger = None
def __init__( self, *, loop, cluster, runstate_dir, internal_runstate_dir, max_backend_connections, nethost, netport, auto_shutdown: bool=False, echo_runtime_info: bool = False, max_protocol: Tuple[int, int], startup_script: Optional[StartupScript] = None, ): self._loop = loop self._serving = False self._cluster = cluster self._pg_addr = self._get_pgaddr() # 1 connection is reserved for the system DB pool_capacity = max_backend_connections - 1 self._pg_pool = connpool.Pool( connect=self._pg_connect, disconnect=self._pg_disconnect, max_capacity=pool_capacity, ) # DB state will be initialized in init(). self._dbindex = None self._runstate_dir = runstate_dir self._internal_runstate_dir = internal_runstate_dir self._max_backend_connections = max_backend_connections self._mgmt_port = None self._mgmt_host_addr = nethost self._mgmt_port_no = netport self._mgmt_protocol_max = max_protocol self._ports = [] self._sys_conf_ports = {} self._sys_auth: Tuple[Any, ...] = tuple() # Shutdown the server after the last management # connection has disconnected self._auto_shutdown = auto_shutdown self._echo_runtime_info = echo_runtime_info self._startup_script = startup_script # Never use `self.__sys_pgcon` directly; get it via # `await self._acquire_sys_pgcon()`. self.__sys_pgcon = None self._sys_pgcon_waiters = None self._roles = immutables.Map() self._instance_data = immutables.Map() self._sys_queries = immutables.Map()