Skip to content

Commit

Permalink
Cythonize asyncio's sslproto.py
Browse files Browse the repository at this point in the history
The performance boost is 10-15%.  The main motivation, though, is
to backport new SSL features to 3.5 and 3.6 and to have a consistent
behaviour in uvloop.
  • Loading branch information
1st1 committed May 26, 2018
1 parent 878e416 commit 4d91264
Show file tree
Hide file tree
Showing 7 changed files with 719 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _default: compile

clean:
rm -fr dist/ doc/_build/ *.egg-info uvloop/loop.*.pyd
rm -fr build/lib.* build/temp.* build/libuv
#rm -fr build/lib.* build/temp.* build/libuv
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so
rm -fr uvloop/handles/*.html uvloop/includes/*.html
find . -name '__pycache__' | xargs rm -rf
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ def client():
tr, _ = f.result()

if server_ssl:
self.assertIsInstance(tr, asyncio.sslproto._SSLProtocolTransport)
self.assertIn('SSL', tr.__class__.__name__)

tr.close()
# let it close
Expand Down
2 changes: 1 addition & 1 deletion uvloop/handles/streamserver.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cdef class UVStreamServer(UVSocketHandle):
else:
waiter = self._loop._new_future()

ssl_protocol = aio_SSLProtocol(
ssl_protocol = SSLProtocol(
self._loop, protocol, self.ssl,
waiter,
True, # server_side
Expand Down
4 changes: 4 additions & 0 deletions uvloop/includes/consts.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ DEF __PROCESS_DEBUG_SLEEP_AFTER_FORK = 1


DEF LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5


# Number of seconds to wait for SSL handshake to complete
DEF SSL_HANDSHAKE_TIMEOUT = 10.0
12 changes: 10 additions & 2 deletions uvloop/includes/stdlib.pxi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio, asyncio.log, asyncio.base_events, \
asyncio.sslproto, asyncio.coroutines, \
asyncio.futures
asyncio.futures, asyncio.transports
import collections.abc
import concurrent.futures
import errno
Expand Down Expand Up @@ -37,12 +37,13 @@ cdef aio_iscoroutine = asyncio.iscoroutine
cdef aio_iscoroutinefunction = asyncio.iscoroutinefunction
cdef aio_BaseProtocol = asyncio.BaseProtocol
cdef aio_Protocol = asyncio.Protocol
cdef aio_SSLProtocol = asyncio.sslproto.SSLProtocol
cdef aio_isfuture = getattr(asyncio, 'isfuture', None)
cdef aio_get_running_loop = getattr(asyncio, '_get_running_loop', None)
cdef aio_set_running_loop = getattr(asyncio, '_set_running_loop', None)
cdef aio_debug_wrapper = getattr(asyncio.coroutines, 'debug_wrapper', None)
cdef aio_AbstractChildWatcher = asyncio.AbstractChildWatcher
cdef aio_Transport = asyncio.Transport
cdef aio_FlowControlMixin = asyncio.transports._FlowControlMixin

cdef col_deque = collections.deque
cdef col_Iterable = collections.abc.Iterable
Expand Down Expand Up @@ -116,6 +117,13 @@ cdef sys_getframe = sys._getframe
cdef sys_version_info = sys.version_info

cdef ssl_SSLContext = ssl.SSLContext
cdef ssl_MemoryBIO = ssl.MemoryBIO
cdef ssl_create_default_context = ssl.create_default_context
cdef ssl_SSLError = ssl.SSLError
cdef ssl_CertificateError = ssl.CertificateError
cdef int ssl_SSL_ERROR_WANT_READ = ssl.SSL_ERROR_WANT_READ
cdef int ssl_SSL_ERROR_WANT_WRITE = ssl.SSL_ERROR_WANT_WRITE
cdef int ssl_SSL_ERROR_SYSCALL = ssl.SSL_ERROR_SYSCALL

cdef long MAIN_THREAD_ID = <long>threading.main_thread().ident

Expand Down
7 changes: 4 additions & 3 deletions uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ cdef class Loop:

ssl_waiter = self._new_future()
sslcontext = None if isinstance(ssl, bool) else ssl
protocol = aio_SSLProtocol(
protocol = SSLProtocol(
self, app_protocol, sslcontext, ssl_waiter,
False, server_hostname)
else:
Expand Down Expand Up @@ -1965,7 +1965,7 @@ cdef class Loop:

ssl_waiter = self._new_future()
sslcontext = None if isinstance(ssl, bool) else ssl
protocol = aio_SSLProtocol(
protocol = SSLProtocol(
self, app_protocol, sslcontext, ssl_waiter,
False, server_hostname)
else:
Expand Down Expand Up @@ -2348,7 +2348,7 @@ cdef class Loop:
protocol = app_protocol
transport_waiter = waiter
else:
protocol = aio_SSLProtocol(
protocol = SSLProtocol(
self, app_protocol, ssl, waiter,
True, # server_side
None) # server_hostname
Expand Down Expand Up @@ -2870,6 +2870,7 @@ include "handles/process.pyx"

include "request.pyx"
include "dns.pyx"
include "sslproto.pyx"

include "handles/udp.pyx"

Expand Down
Loading

0 comments on commit 4d91264

Please sign in to comment.