diff --git a/cr8/clients.py b/cr8/clients.py index adc2429..3a259fb 100644 --- a/cr8/clients.py +++ b/cr8/clients.py @@ -320,12 +320,11 @@ class HttpClient: def __init__(self, hosts, conn_pool_limit=25, session_settings=None): self.hosts = hosts self.urls = itertools.cycle(list(map(_append_sql, hosts))) - self.conn_pool_limit = conn_pool_limit; + self.conn_pool_limit = conn_pool_limit self.is_cratedb = True self._pool = [] self.session_settings = session_settings or {} - @property async def _session(self): if not self._pool: @@ -344,8 +343,8 @@ async def _session(self): dumps(payload, cls=CrateJsonEncoder) ) self._pool.append(session) - - return random.choice(self._pool) + + return self._pool.pop() async def execute(self, stmt, args=None): @@ -353,11 +352,13 @@ async def execute(self, stmt, args=None): if args: payload['args'] = _plain_or_callable(args) session = await self._session - return await _exec( + result = await _exec( session, next(self.urls), dumps(payload, cls=CrateJsonEncoder) ) + self._pool.append(session) + return result async def execute_many(self, stmt, bulk_args): data = dumps(dict( @@ -365,7 +366,9 @@ async def execute_many(self, stmt, bulk_args): bulk_args=_plain_or_callable(bulk_args) ), cls=CrateJsonEncoder) session = await self._session - return await _exec(session, next(self.urls), data) + result = await _exec(session, next(self.urls), data) + self._pool.append(session) + return result async def get_server_version(self): session = await self._session @@ -374,11 +377,13 @@ async def get_server_version(self): async with session.get(url) as resp: r = await resp.json() version = r['version'] - return { + result = { 'hash': version['build_hash'], 'number': version['number'], 'date': _date_or_none(version['build_timestamp'][:10]) } + self._pool.append(session) + return result async def _close(self): for session in self._pool: