-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aiohttp2 support+ssl fix #1
Changes from 3 commits
493526c
2c08fb6
1511bf9
151822d
966386b
12d60ca
5559e0f
6ce95ce
6948ee8
b3ef896
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import asyncio | ||
import ssl | ||
|
||
import aiohttp | ||
from aiohttp.errors import ClientError, FingerprintMismatch | ||
from elasticsearch.connection import Connection | ||
from elasticsearch.exceptions import (ConnectionError, ConnectionTimeout, | ||
|
||
from .compat import AIOHTTP_2 # isort:skip | ||
|
||
if AIOHTTP_2: | ||
from aiohttp import ClientError | ||
else: | ||
from aiohttp.errors import ClientError | ||
|
||
from elasticsearch.connection import Connection # noqa # isort:skip | ||
from elasticsearch.exceptions import (ConnectionError, ConnectionTimeout, # noqa # isort:skip | ||
SSLError) | ||
from yarl import URL | ||
from yarl import URL # noqa # isort:skip | ||
|
||
|
||
class AIOHttpConnection(Connection): | ||
|
@@ -16,6 +24,7 @@ def __init__( | |
port=9200, | ||
http_auth=None, | ||
use_ssl=False, | ||
ssl_context=None, | ||
verify_certs=False, | ||
maxsize=10, | ||
headers=None, | ||
|
@@ -49,15 +58,23 @@ def __init__( | |
|
||
self.session = kwargs.get('session') | ||
if self.session is None: | ||
connector_kwargs = { | ||
'limit': maxsize, | ||
'use_dns_cache': kwargs.get('use_dns_cache', False), | ||
'ssl_context': ssl_context, | ||
'verify_ssl': self.verify_certs, | ||
'loop': self.loop, | ||
} | ||
session_kwargs = {'auth': self.http_auth} | ||
|
||
if AIOHTTP_2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как я токошо узнал начиная с 1.3 (или мб раньше) оно и так None по дефолту, а дефалтный в 5 минут ставится в request |
||
session_kwargs['conn_timeout'] = None | ||
else: | ||
connector_kwargs['conn_timeout'] = None | ||
|
||
self.session = aiohttp.ClientSession( | ||
auth=self.http_auth, | ||
connector=aiohttp.TCPConnector( | ||
limit=maxsize, | ||
use_dns_cache=kwargs.get('use_dns_cache', False), | ||
verify_ssl=self.verify_certs, | ||
conn_timeout=None, | ||
loop=self.loop, | ||
), | ||
connector=aiohttp.TCPConnector(**connector_kwargs), | ||
**session_kwargs | ||
) | ||
|
||
def close(self): | ||
|
@@ -78,17 +95,22 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign | |
|
||
duration = self.loop.time() - start | ||
|
||
except asyncio.TimeoutError as exc: | ||
except ssl.CertificateError as exc: | ||
self.log_request_fail(method, url, url_path, body, self.loop.time() - start, exception=exc) # noqa | ||
raise ConnectionTimeout('TIMEOUT', str(exc), exc) | ||
raise SSLError('N/A', str(exc), exc) | ||
|
||
except FingerprintMismatch as exc: | ||
except asyncio.TimeoutError as exc: | ||
self.log_request_fail(method, url, url_path, body, self.loop.time() - start, exception=exc) # noqa | ||
raise SSLError('N/A', str(exc), exc) | ||
raise ConnectionTimeout('TIMEOUT', str(exc), exc) | ||
|
||
except ClientError as exc: | ||
self.log_request_fail(method, url, url_path, body, self.loop.time() - start, exception=exc) # noqa | ||
raise ConnectionError('N/A', str(exc), exc) | ||
_exc = str(exc) | ||
# aiohttp wraps ssl error | ||
if 'SSL: CERTIFICATE_VERIFY_FAILED' in _exc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No other way to check it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its just a message, so no. |
||
raise SSLError('N/A', _exc, exc) | ||
|
||
raise ConnectionError('N/A', _exc, exc) | ||
|
||
finally: | ||
if response is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
aiohttp==1.3.5 | ||
aiohttp==2.0.5 | ||
appdirs==1.4.3 | ||
appnope==0.1.0 | ||
async-timeout==1.2.0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module level import not on the top because of if statement for aiohttp