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

View File

@@ -110,7 +110,10 @@ def trampoline(fd, read=None, write=None, timeout=None,
current = greenlet.getcurrent() current = greenlet.getcurrent()
assert hub.greenlet is not current, 'do not call blocking functions from the mainloop' 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' 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: if timeout is not None:
t = hub.schedule_call_global(timeout, current.throw, timeout_exc) t = hub.schedule_call_global(timeout, current.throw, timeout_exc)
try: try: