From f591bc3c7ae5e8cbb339b514c0f72d1ac86b93e0 Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Sun, 22 Oct 2017 21:17:54 -0700 Subject: [PATCH] Allow to pass an instance of ClientSession as an argument. Refs #22 --- CHANGES | 5 +++++ raven_aiohttp.py | 26 +++++++++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index 06ca84b..9e51301 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +X.X.X +----- + +- Allow to pass an instance of ClientSession as an argument + 0.6.0 ----- diff --git a/raven_aiohttp.py b/raven_aiohttp.py index 06ad36c..0063ebc 100644 --- a/raven_aiohttp.py +++ b/raven_aiohttp.py @@ -36,7 +36,7 @@ class AioHttpTransportBase( def __init__(self, parsed_url=None, *, verify_ssl=True, resolve=True, timeout=defaults.TIMEOUT, - keepalive=True, family=socket.AF_INET, loop=None): + keepalive=True, family=socket.AF_INET, client_session=None, loop=None): self._resolve = resolve self._keepalive = keepalive self._family = family @@ -53,7 +53,9 @@ def __init__(self, parsed_url=None, *, verify_ssl=True, resolve=True, else: super().__init__(parsed_url, timeout, verify_ssl) - if self.keepalive: + if client_session: + self._client_session = client_session + else: self._client_session = self._client_session_factory() self._closing = False @@ -72,23 +74,18 @@ def family(self): def _client_session_factory(self): connector = aiohttp.TCPConnector(verify_ssl=self.verify_ssl, - resolve=self.resolve, - family=self.family, - loop=self._loop) + resolve=self.resolve, + family=self.family, + loop=self._loop) return aiohttp.ClientSession(connector=connector, - loop=self._loop) + loop=self._loop) @asyncio.coroutine def _do_send(self, url, data, headers, success_cb, failure_cb): - if self.keepalive: - session = self._client_session - else: - session = self._client_session_factory() - resp = None try: - resp = yield from session.post( + resp = yield from self._client_session.post( url, data=data, compress=False, @@ -118,8 +115,6 @@ def _do_send(self, url, data, headers, success_cb, failure_cb): finally: if resp is not None: resp.release() - if not self.keepalive: - yield from session.close() @abc.abstractmethod def _async_send(self, url, data, headers, success_cb, failure_cb): # pragma: no cover @@ -146,8 +141,9 @@ def _close_coro(self, *, timeout=None): except asyncio.TimeoutError: pass finally: - if self.keepalive: + if self._client_session: yield from self._client_session.close() + self._client_session = None def close(self, *, timeout=None): if self._closing: