Sergey Shepelev
2016-12-22 04:35:14 +03:00
parent 3a8115c560
commit 6359fd3cf1
8 changed files with 107 additions and 46 deletions

View File

@@ -23,6 +23,8 @@ if os.environ.get('EVENTLET_IMPORT_VERSION_ONLY') != '1':
Timeout = timeout.Timeout
with_timeout = timeout.with_timeout
wrap_is_timeout = timeout.wrap_is_timeout
is_timeout = timeout.is_timeout
GreenPool = greenpool.GreenPool
GreenPile = greenpool.GreenPile

View File

@@ -1,17 +1,19 @@
__socket = __import__('socket')
__all__ = __socket.__all__
__patched__ = ['fromfd', 'socketpair', 'ssl', 'socket']
__patched__ = ['fromfd', 'socketpair', 'ssl', 'socket', 'timeout']
from eventlet.patcher import slurp_properties
slurp_properties(__socket, globals(),
ignore=__patched__, srckeys=dir(__socket))
import eventlet.patcher
eventlet.patcher.slurp_properties(__socket, globals(), ignore=__patched__, srckeys=dir(__socket))
os = __import__('os')
import sys
from eventlet.hubs import get_hub
from eventlet.greenio import GreenSocket as socket
from eventlet.greenio import _GLOBAL_DEFAULT_TIMEOUT
from eventlet import greenio
socket = greenio.GreenSocket
_GLOBAL_DEFAULT_TIMEOUT = greenio._GLOBAL_DEFAULT_TIMEOUT
timeout = greenio.socket_timeout
try:
__original_fromfd__ = __socket.fromfd

View File

@@ -13,6 +13,7 @@ __all__ = [
'GreenSocket', '_GLOBAL_DEFAULT_TIMEOUT', 'set_nonblocking',
'SOCKET_BLOCKING', 'SOCKET_CLOSED', 'CONNECT_ERR', 'CONNECT_SUCCESS',
'shutdown_safe', 'SSL',
'socket_timeout',
]
BUFFER_SIZE = 4096
@@ -27,6 +28,11 @@ if six.PY2:
_original_socket = eventlet.patcher.original('socket').socket
socket_timeout = eventlet.timeout.wrap_is_timeout(socket.timeout)
# Global timeout exception instance - less allocations.
_timeout_exc = socket_timeout('timed out')
def socket_connect(descriptor, address):
"""
Attempts to connect to the address, returns the descriptor if it succeeds,
@@ -216,8 +222,7 @@ class GreenSocket(object):
client, addr = res
set_nonblocking(client)
return type(self)(client), addr
self._trampoline(fd, read=True, timeout=self.gettimeout(),
timeout_exc=socket.timeout("timed out"))
self._trampoline(fd, read=True, timeout=self.gettimeout(), timeout_exc=_timeout_exc)
def _mark_as_closed(self):
""" Mark this socket as being closed """
@@ -246,10 +251,10 @@ class GreenSocket(object):
if socket_connect(fd, address):
return
if time.time() >= end:
raise socket.timeout("timed out")
raise _timeout_exc
timeout = end - time.time()
try:
self._trampoline(fd, write=True, timeout=end - time.time(),
timeout_exc=socket.timeout("timed out"))
self._trampoline(fd, write=True, timeout=timeout, timeout_exc=_timeout_exc)
except IOClosed:
# ... we need some workable errno here.
raise socket.error(errno.EBADFD)
@@ -270,14 +275,15 @@ class GreenSocket(object):
return errno.EBADFD
else:
end = time.time() + self.gettimeout()
timeout_exc = socket.timeout(errno.EAGAIN)
while True:
try:
if socket_connect(fd, address):
return 0
if time.time() >= end:
raise socket.timeout(errno.EAGAIN)
raise timeout_exc
self._trampoline(fd, write=True, timeout=end - time.time(),
timeout_exc=socket.timeout(errno.EAGAIN))
timeout_exc=timeout_exc)
socket_checkerr(fd)
except socket.error as ex:
return get_errno(ex)
@@ -316,7 +322,7 @@ class GreenSocket(object):
self.fd,
read=True,
timeout=self.gettimeout(),
timeout_exc=socket.timeout("timed out"))
timeout_exc=_timeout_exc)
def _recv_loop(self, recv_meth, empty_val, *args):
fd = self.fd
@@ -376,7 +382,7 @@ class GreenSocket(object):
try:
self._trampoline(self.fd, write=True, timeout=self.gettimeout(),
timeout_exc=socket.timeout("timed out"))
timeout_exc=_timeout_exc)
except IOClosed:
raise socket.error(errno.ECONNRESET, 'Connection closed by another thread')

View File

@@ -20,13 +20,15 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.from eventlet.support import greenlets as greenlet
import functools
import inspect
from eventlet.support import greenlets as greenlet
from eventlet.hubs import get_hub
__all__ = ['Timeout',
'with_timeout']
__all__ = ['Timeout', 'with_timeout', 'wrap_is_timeout', 'is_timeout']
_NONE = object()
_MISSING = object()
# deriving from BaseException so that "except Exception as e" doesn't catch
# Timeout exceptions.
@@ -128,20 +130,57 @@ class Timeout(BaseException):
if value is self and self.exception is False:
return True
@property
def is_timeout(self):
return True
def with_timeout(seconds, function, *args, **kwds):
"""Wrap a call to some (yielding) function with a timeout; if the called
function fails to return before the timeout, cancel it and return a flag
value.
"""
timeout_value = kwds.pop("timeout_value", _NONE)
timeout_value = kwds.pop("timeout_value", _MISSING)
timeout = Timeout(seconds)
try:
try:
return function(*args, **kwds)
except Timeout as ex:
if ex is timeout and timeout_value is not _NONE:
if ex is timeout and timeout_value is not _MISSING:
return timeout_value
raise
finally:
timeout.cancel()
def wrap_is_timeout(base):
'''Adds `.is_timeout=True` attribute to objects returned by `base()`.
When `base` is class, it returns a subclass with read-only property.
Otherwise, it returns a function that sets attribute on result of `base()` call.
Wrappers make best effort to be transparent.
'''
if inspect.isclass(base):
class wrapped(base):
is_timeout = property(lambda _: True)
for k in functools.WRAPPER_ASSIGNMENTS:
v = getattr(base, k, _MISSING)
if v is not _MISSING:
try:
setattr(wrapped, k, v)
except AttributeError:
pass
return wrapped
@functools.wraps(base)
def fun(*args, **kwargs):
ex = base(*args, **kwargs)
ex.is_timeout = True
return ex
return fun
def is_timeout(obj):
return bool(getattr(obj, 'is_timeout', False))

View File

@@ -346,6 +346,11 @@ def run_isolated(path, prefix='tests/isolated/', env=None, args=None, timeout=No
assert ok, 'Expected single line "pass" in stdout'
def check_is_timeout(obj):
value_text = getattr(obj, 'is_timeout', '(missing)')
assert obj.is_timeout, 'type={0} str={1} .is_timeout={2}'.format(type(obj), str(obj), value_text)
certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt')
private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key')

View File

@@ -1,12 +1,7 @@
import os
from unittest import TestCase, main
from nose.tools import eq_
import eventlet
from eventlet import greenio, hubs, greenthread, spawn
from eventlet import greenio, hubs, greenthread
from eventlet.green import ssl
from tests import skip_if_no_ssl
import tests
def check_hub():
@@ -21,10 +16,7 @@ def check_hub():
assert not hub.running
class TestApi(TestCase):
certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt')
private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key')
class TestApi(tests.LimitedTestCase):
def test_tcp_listener(self):
socket = eventlet.listen(('0.0.0.0', 0))
@@ -50,13 +42,13 @@ class TestApi(TestCase):
client = eventlet.connect(('127.0.0.1', server.getsockname()[1]))
fd = client.makefile('rb')
client.close()
eq_(fd.readline(), b'hello\n')
eq_(fd.read(), b'')
assert fd.readline() == b'hello\n'
assert fd.read() == b''
fd.close()
check_hub()
@skip_if_no_ssl
@tests.skip_if_no_ssl
def test_connect_ssl(self):
def accept_once(listenfd):
try:
@@ -70,8 +62,8 @@ class TestApi(TestCase):
server = eventlet.wrap_ssl(
eventlet.listen(('0.0.0.0', 0)),
self.private_key_file,
self.certificate_file,
tests.private_key_file,
tests.certificate_file,
server_side=True
)
eventlet.spawn_n(accept_once, server)
@@ -98,7 +90,7 @@ class TestApi(TestCase):
def server(sock):
client, addr = sock.accept()
eventlet.sleep(0.1)
server_evt = spawn(server, server_sock)
server_evt = eventlet.spawn(server, server_sock)
eventlet.sleep(0)
try:
desc = eventlet.connect(('127.0.0.1', bound_port))
@@ -179,9 +171,9 @@ class TestApi(TestCase):
pass
class Foo(object):
pass
def test_wrap_is_timeout():
class A(object):
pass
if __name__ == '__main__':
main()
obj = eventlet.wrap_is_timeout(A)()
tests.check_is_timeout(obj)

View File

@@ -91,3 +91,14 @@ def test_getaddrinfo_ipv6_scope():
if not socket.has_ipv6:
return
socket.getaddrinfo('::1%2', 80, socket.AF_INET6)
def test_error_is_timeout():
s1, _ = socket.socketpair()
s1.settimeout(0.01)
try:
s1.recv(1)
except socket.error as e:
tests.check_is_timeout(e)
else:
assert False, 'No timeout, socket.error was not raised'

View File

@@ -1,12 +1,12 @@
import eventlet
from tests import LimitedTestCase
import tests
DELAY = 0.01
class TestDirectRaise(LimitedTestCase):
class TestDirectRaise(tests.LimitedTestCase):
def test_direct_raise_class(self):
try:
raise eventlet.Timeout
@@ -36,7 +36,7 @@ class TestDirectRaise(LimitedTestCase):
str(tm)
class TestWithTimeout(LimitedTestCase):
class TestWithTimeout(tests.LimitedTestCase):
def test_with_timeout(self):
self.assertRaises(eventlet.Timeout, eventlet.with_timeout, DELAY, eventlet.sleep, DELAY * 10)
X = object()
@@ -53,3 +53,7 @@ class TestWithTimeout(LimitedTestCase):
eventlet.Timeout,
eventlet.with_timeout,
DELAY, longer_timeout)
def test_is_timeout_attribute():
tests.check_is_timeout(eventlet.Timeout())