Skip to content

Commit 4d91264

Browse files
committed
Cythonize asyncio's sslproto.py
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.
1 parent 878e416 commit 4d91264

File tree

7 files changed

+719
-8
lines changed

7 files changed

+719
-8
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _default: compile
99

1010
clean:
1111
rm -fr dist/ doc/_build/ *.egg-info uvloop/loop.*.pyd
12-
rm -fr build/lib.* build/temp.* build/libuv
12+
#rm -fr build/lib.* build/temp.* build/libuv
1313
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so
1414
rm -fr uvloop/handles/*.html uvloop/includes/*.html
1515
find . -name '__pycache__' | xargs rm -rf

tests/test_tcp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def client():
913913
tr, _ = f.result()
914914

915915
if server_ssl:
916-
self.assertIsInstance(tr, asyncio.sslproto._SSLProtocolTransport)
916+
self.assertIn('SSL', tr.__class__.__name__)
917917

918918
tr.close()
919919
# let it close

uvloop/handles/streamserver.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ cdef class UVStreamServer(UVSocketHandle):
5454
else:
5555
waiter = self._loop._new_future()
5656

57-
ssl_protocol = aio_SSLProtocol(
57+
ssl_protocol = SSLProtocol(
5858
self._loop, protocol, self.ssl,
5959
waiter,
6060
True, # server_side

uvloop/includes/consts.pxi

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ DEF __PROCESS_DEBUG_SLEEP_AFTER_FORK = 1
1212

1313

1414
DEF LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
15+
16+
17+
# Number of seconds to wait for SSL handshake to complete
18+
DEF SSL_HANDSHAKE_TIMEOUT = 10.0

uvloop/includes/stdlib.pxi

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio, asyncio.log, asyncio.base_events, \
22
asyncio.sslproto, asyncio.coroutines, \
3-
asyncio.futures
3+
asyncio.futures, asyncio.transports
44
import collections.abc
55
import concurrent.futures
66
import errno
@@ -37,12 +37,13 @@ cdef aio_iscoroutine = asyncio.iscoroutine
3737
cdef aio_iscoroutinefunction = asyncio.iscoroutinefunction
3838
cdef aio_BaseProtocol = asyncio.BaseProtocol
3939
cdef aio_Protocol = asyncio.Protocol
40-
cdef aio_SSLProtocol = asyncio.sslproto.SSLProtocol
4140
cdef aio_isfuture = getattr(asyncio, 'isfuture', None)
4241
cdef aio_get_running_loop = getattr(asyncio, '_get_running_loop', None)
4342
cdef aio_set_running_loop = getattr(asyncio, '_set_running_loop', None)
4443
cdef aio_debug_wrapper = getattr(asyncio.coroutines, 'debug_wrapper', None)
4544
cdef aio_AbstractChildWatcher = asyncio.AbstractChildWatcher
45+
cdef aio_Transport = asyncio.Transport
46+
cdef aio_FlowControlMixin = asyncio.transports._FlowControlMixin
4647

4748
cdef col_deque = collections.deque
4849
cdef col_Iterable = collections.abc.Iterable
@@ -116,6 +117,13 @@ cdef sys_getframe = sys._getframe
116117
cdef sys_version_info = sys.version_info
117118

118119
cdef ssl_SSLContext = ssl.SSLContext
120+
cdef ssl_MemoryBIO = ssl.MemoryBIO
121+
cdef ssl_create_default_context = ssl.create_default_context
122+
cdef ssl_SSLError = ssl.SSLError
123+
cdef ssl_CertificateError = ssl.CertificateError
124+
cdef int ssl_SSL_ERROR_WANT_READ = ssl.SSL_ERROR_WANT_READ
125+
cdef int ssl_SSL_ERROR_WANT_WRITE = ssl.SSL_ERROR_WANT_WRITE
126+
cdef int ssl_SSL_ERROR_SYSCALL = ssl.SSL_ERROR_SYSCALL
119127

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

uvloop/loop.pyx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ cdef class Loop:
16781678

16791679
ssl_waiter = self._new_future()
16801680
sslcontext = None if isinstance(ssl, bool) else ssl
1681-
protocol = aio_SSLProtocol(
1681+
protocol = SSLProtocol(
16821682
self, app_protocol, sslcontext, ssl_waiter,
16831683
False, server_hostname)
16841684
else:
@@ -1965,7 +1965,7 @@ cdef class Loop:
19651965

19661966
ssl_waiter = self._new_future()
19671967
sslcontext = None if isinstance(ssl, bool) else ssl
1968-
protocol = aio_SSLProtocol(
1968+
protocol = SSLProtocol(
19691969
self, app_protocol, sslcontext, ssl_waiter,
19701970
False, server_hostname)
19711971
else:
@@ -2348,7 +2348,7 @@ cdef class Loop:
23482348
protocol = app_protocol
23492349
transport_waiter = waiter
23502350
else:
2351-
protocol = aio_SSLProtocol(
2351+
protocol = SSLProtocol(
23522352
self, app_protocol, ssl, waiter,
23532353
True, # server_side
23542354
None) # server_hostname
@@ -2870,6 +2870,7 @@ include "handles/process.pyx"
28702870

28712871
include "request.pyx"
28722872
include "dns.pyx"
2873+
include "sslproto.pyx"
28732874

28742875
include "handles/udp.pyx"
28752876

0 commit comments

Comments
 (0)