From 01d4213af0e5d937f786f8b99586b35ad6bf51d7 Mon Sep 17 00:00:00 2001 From: Nikolay Osaulenko Date: Tue, 19 Nov 2019 17:16:37 +0300 Subject: [PATCH] Expose aiohttp client session --- consul/aio.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/consul/aio.py b/consul/aio.py index 5e347f53..af88f937 100644 --- a/consul/aio.py +++ b/consul/aio.py @@ -14,12 +14,15 @@ class HTTPClient(base.HTTPClient): """Asyncio adapter for python consul using aiohttp library""" - def __init__(self, *args, loop=None, **kwargs): + def __init__(self, *args, loop=None, client_session=None, **kwargs): super(HTTPClient, self).__init__(*args, **kwargs) self._loop = loop or asyncio.get_event_loop() - connector = aiohttp.TCPConnector(loop=self._loop, - verify_ssl=self.verify) - self._session = aiohttp.ClientSession(connector=connector) + if client_session: + self._session = client_session + else: + connector = aiohttp.TCPConnector(loop=self._loop, + verify_ssl=self.verify) + self._session = aiohttp.ClientSession(connector=connector) @asyncio.coroutine def _request(self, callback, method, uri, data=None): @@ -60,13 +63,15 @@ def close(self): class Consul(base.Consul): - def __init__(self, *args, loop=None, **kwargs): + def __init__(self, *args, loop=None, client_session=None, **kwargs): self._loop = loop or asyncio.get_event_loop() + self._client_session = client_session super().__init__(*args, **kwargs) def connect(self, host, port, scheme, verify=True, cert=None): return HTTPClient(host, port, scheme, loop=self._loop, - verify=verify, cert=None) + verify=verify, cert=None, + client_session=self._client_session) def close(self): """Close all opened http connections"""