Slightly more efficient trampoline, ssl no longer calls fileno needlessly.

This commit is contained in:
Ryan Williams
2010-03-14 23:25:01 -07:00
parent 985f8ca0ac
commit 39b24f7f66
2 changed files with 15 additions and 12 deletions

View File

@@ -71,12 +71,12 @@ class GreenSSLSocket(__ssl.SSLSocket):
return func(*a, **kw)
except SSLError, exc:
if get_errno(exc) == SSL_ERROR_WANT_READ:
trampoline(self.fileno(),
trampoline(self,
read=True,
timeout=self.gettimeout(),
timeout_exc=timeout_exc('timed out'))
elif get_errno(exc) == SSL_ERROR_WANT_WRITE:
trampoline(self.fileno(),
trampoline(self,
write=True,
timeout=self.gettimeout(),
timeout_exc=timeout_exc('timed out'))
@@ -133,7 +133,7 @@ class GreenSSLSocket(__ssl.SSLSocket):
raise ValueError("sendto not allowed on instances of %s" %
self.__class__)
else:
trampoline(self.fileno(), write=True, timeout_exc=timeout_exc('timed out'))
trampoline(self, write=True, timeout_exc=timeout_exc('timed out'))
return socket.sendto(self, data, addr, flags)
def sendall (self, data, flags=0):
@@ -157,7 +157,7 @@ class GreenSSLSocket(__ssl.SSLSocket):
if self.act_non_blocking:
raise
if get_errno(e) == errno.EWOULDBLOCK:
trampoline(self.fileno(), write=True,
trampoline(self, write=True,
timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
if get_errno(e) in SOCKET_CLOSED:
return ''
@@ -180,7 +180,7 @@ class GreenSSLSocket(__ssl.SSLSocket):
if self.act_non_blocking:
raise
if get_errno(e) == errno.EWOULDBLOCK:
trampoline(self.fileno(), read=True,
trampoline(self, read=True,
timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
if get_errno(e) in SOCKET_CLOSED:
return ''
@@ -189,17 +189,17 @@ class GreenSSLSocket(__ssl.SSLSocket):
def recv_into (self, buffer, nbytes=None, flags=0):
if not self.act_non_blocking:
trampoline(self.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
trampoline(self, read=True, timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
return super(GreenSSLSocket, self).recv_into(buffer, nbytes, flags)
def recvfrom (self, addr, buflen=1024, flags=0):
if not self.act_non_blocking:
trampoline(self.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
trampoline(self, read=True, timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
return super(GreenSSLSocket, self).recvfrom(addr, buflen, flags)
def recvfrom_into (self, buffer, nbytes=None, flags=0):
if not self.act_non_blocking:
trampoline(self.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
trampoline(self, read=True, timeout=self.gettimeout(), timeout_exc=timeout_exc('timed out'))
return super(GreenSSLSocket, self).recvfrom_into(buffer, nbytes, flags)
def unwrap(self):
@@ -223,7 +223,7 @@ class GreenSSLSocket(__ssl.SSLSocket):
return real_connect(self, addr)
except orig_socket.error, exc:
if get_errno(exc) in CONNECT_ERR:
trampoline(self.fileno(), write=True)
trampoline(self, write=True)
elif get_errno(exc) in CONNECT_SUCCESS:
return
else:
@@ -235,7 +235,7 @@ class GreenSSLSocket(__ssl.SSLSocket):
real_connect(self, addr)
except orig_socket.error, exc:
if get_errno(exc) in CONNECT_ERR:
trampoline(self.fileno(), write=True,
trampoline(self, write=True,
timeout=end-time.time(), timeout_exc=timeout_exc('timed out'))
elif get_errno(exc) in CONNECT_SUCCESS:
return
@@ -275,7 +275,7 @@ class GreenSSLSocket(__ssl.SSLSocket):
except orig_socket.error, e:
if get_errno(e) != errno.EWOULDBLOCK:
raise
trampoline(self.fileno(), read=True, timeout=self.gettimeout(),
trampoline(self, read=True, timeout=self.gettimeout(),
timeout_exc=timeout_exc('timed out'))
new_ssl = type(self)(newsock,

View File

@@ -110,7 +110,10 @@ def trampoline(fd, read=None, write=None, timeout=None,
current = greenlet.getcurrent()
assert hub.greenlet is not current, 'do not call blocking functions from the mainloop'
assert not (read and write), 'not allowed to trampoline for reading and writing'
fileno = getattr(fd, 'fileno', lambda: fd)()
try:
fileno = fd.fileno()
except AttributeError:
fileno = fd
if timeout is not None:
t = hub.schedule_call_global(timeout, current.throw, timeout_exc)
try: