Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #221 from tbetbetbe/development-ssl-connection-err…
Browse files Browse the repository at this point in the history
…or-reduction

Improve handling of socket errors
  • Loading branch information
Lukasa committed Apr 13, 2016
2 parents 0837669 + 1d1d981 commit 9a9b048
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Cory Benfield
Copyright (c) 2014 Cory Benfield, Google Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 21 additions & 2 deletions hyper/http20/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import h2.events
import h2.settings

from ..compat import ssl
from ..tls import wrap_socket, H2_NPN_PROTOCOLS, H2C_PROTOCOL
from ..common.exceptions import ConnectionResetError
from ..common.bufsocket import BufferedSocket
Expand Down Expand Up @@ -237,7 +238,7 @@ def connect(self):
host = self.proxy_host
port = self.proxy_port

sock = socket.create_connection((host, port), 5)
sock = socket.create_connection((host, port))

if self.secure:
assert not self.proxy_host, "Using a proxy with HTTPS not yet supported."
Expand Down Expand Up @@ -522,13 +523,31 @@ def _recv_cb(self):
# TODO: Re-evaluate this.
self._single_read()
count = 9
retry_wait = 0.05 # can improve responsiveness to delay the retry

while count and self._sock is not None and self._sock.can_read:
# If the connection has been closed, bail out.
# If the connection has been closed, bail out, but retry
# on transient errors.
try:
self._single_read()
except ConnectionResetError:
break
except ssl.SSLError as e: # pragma: no cover
# these are transient errors that can occur while reading from
# ssl connections.
if e.args[0] in (ssl.SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE):
continue
else:
raise
except socket.error as e: # pragma: no cover
if e.errno in (errno.EINTR, errno.EAGAIN):
# if 'interrupted' or 'try again', continue
sleep(retry_wait)
continue
elif e.errno == errno.ECONNRESET:
break
else:
raise

count -= 1

Expand Down

0 comments on commit 9a9b048

Please sign in to comment.