Skip to content

Commit

Permalink
Allow to pass an instance of ClientSession as an argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kentzo committed Oct 23, 2017
1 parent 182c427 commit f591bc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
X.X.X
-----

- Allow to pass an instance of ClientSession as an argument

0.6.0
-----

Expand Down
26 changes: 11 additions & 15 deletions raven_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit f591bc3

Please sign in to comment.