1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
"""Kazoo handler helpers"""
from collections import defaultdict
import errno
import functools
import select
import selectors
import ssl
import socket
import time
HAS_FNCTL = True
try:
import fcntl
except ImportError: # pragma: nocover
HAS_FNCTL = False
# sentinel objects
_NONE = object()
class AsyncResult(object):
"""A one-time event that stores a value or an exception"""
def __init__(self, handler, condition_factory, timeout_factory):
self._handler = handler
self._exception = _NONE
self._condition = condition_factory()
self._callbacks = []
self._timeout_factory = timeout_factory
self.value = None
def ready(self):
"""Return true if and only if it holds a value or an
exception"""
return self._exception is not _NONE
def successful(self):
"""Return true if and only if it is ready and holds a value"""
return self._exception is None
@property
def exception(self):
if self._exception is not _NONE:
return self._exception
def set(self, value=None):
"""Store the value. Wake up the waiters."""
with self._condition:
self.value = value
self._exception = None
self._do_callbacks()
self._condition.notify_all()
def set_exception(self, exception):
"""Store the exception. Wake up the waiters."""
with self._condition:
self._exception = exception
self._do_callbacks()
self._condition.notify_all()
def get(self, block=True, timeout=None):
"""Return the stored value or raise the exception.
If there is no value raises TimeoutError.
"""
with self._condition:
if self._exception is not _NONE:
if self._exception is None:
return self.value
raise self._exception
elif block:
self._condition.wait(timeout)
if self._exception is not _NONE:
if self._exception is None:
return self.value
raise self._exception
# if we get to this point we timeout
raise self._timeout_factory()
def get_nowait(self):
"""Return the value or raise the exception without blocking.
If nothing is available, raises TimeoutError
"""
return self.get(block=False)
def wait(self, timeout=None):
"""Block until the instance is ready."""
with self._condition:
if not self.ready():
self._condition.wait(timeout)
return self._exception is not _NONE
def rawlink(self, callback):
"""Register a callback to call when a value or an exception is
set"""
with self._condition:
if callback not in self._callbacks:
self._callbacks.append(callback)
# Are we already set? Dispatch it now
if self.ready():
self._do_callbacks()
def unlink(self, callback):
"""Remove the callback set by :meth:`rawlink`"""
with self._condition:
if self.ready():
# Already triggered, ignore
return
if callback in self._callbacks:
self._callbacks.remove(callback)
def _do_callbacks(self):
"""Execute the callbacks that were registered by :meth:`rawlink`.
If the handler is in running state this method only schedules
the calls to be performed by the handler. If it's stopped,
the callbacks are called right away."""
for callback in self._callbacks:
if self._handler.running:
self._handler.completion_queue.put(
functools.partial(callback, self)
)
else:
functools.partial(callback, self)()
def _set_fd_cloexec(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
def _set_default_tcpsock_options(module, sock):
sock.setsockopt(module.IPPROTO_TCP, module.TCP_NODELAY, 1)
if HAS_FNCTL:
_set_fd_cloexec(sock)
return sock
def create_socket_pair(module, port=0):
"""Create socket pair.
If socket.socketpair isn't available, we emulate it.
"""
# See if socketpair() is available.
have_socketpair = hasattr(module, "socketpair")
if have_socketpair:
client_sock, srv_sock = module.socketpair()
return client_sock, srv_sock
# Create a non-blocking temporary server socket
temp_srv_sock = module.socket()
temp_srv_sock.setblocking(False)
temp_srv_sock.bind(("", port))
port = temp_srv_sock.getsockname()[1]
temp_srv_sock.listen(1)
# Create non-blocking client socket
client_sock = module.socket()
client_sock.setblocking(False)
try:
client_sock.connect(("localhost", port))
except module.error as err:
# EWOULDBLOCK is not an error, as the socket is non-blocking
if err.errno != errno.EWOULDBLOCK:
raise
# Use select to wait for connect() to succeed.
timeout = 1
readable = select.select([temp_srv_sock], [], [], timeout)[0]
if temp_srv_sock not in readable:
raise Exception(
"Client socket not connected in %s" " second(s)" % (timeout)
)
srv_sock, _ = temp_srv_sock.accept()
return client_sock, srv_sock
def create_tcp_socket(module):
"""Create a TCP socket with the CLOEXEC flag set."""
type_ = module.SOCK_STREAM
if hasattr(module, "SOCK_CLOEXEC"): # pragma: nocover
# if available, set cloexec flag during socket creation
type_ |= module.SOCK_CLOEXEC
sock = module.socket(module.AF_INET, type_)
_set_default_tcpsock_options(module, sock)
return sock
def create_tcp_connection(
module,
address,
timeout=None,
use_ssl=False,
ca=None,
certfile=None,
keyfile=None,
keyfile_password=None,
verify_certs=True,
options=None,
ciphers=None,
):
end = None
if timeout is None:
# thanks to create_connection() developers for
# this ugliness...
timeout = module.getdefaulttimeout()
if timeout is not None:
end = time.time() + timeout
sock = None
while True:
timeout_at = end if end is None else end - time.time()
# The condition is not '< 0' here because socket.settimeout treats 0 as
# a special case to put the socket in non-blocking mode.
if timeout_at is not None and timeout_at <= 0:
break
if use_ssl:
# Disallow use of SSLv2 and V3 (meaning we require TLSv1.0+)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
if options is not None:
context.options = options
else:
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
if ciphers:
context.set_ciphers(ciphers)
# Load default CA certs
context.load_default_certs(ssl.Purpose.SERVER_AUTH)
# We must set check_hostname to False prior to setting
# verify_mode to CERT_NONE.
# TODO: Make hostname verification configurable as some users may
# elect to use it.
context.check_hostname = False
context.verify_mode = (
ssl.CERT_REQUIRED if verify_certs else ssl.CERT_NONE
)
if ca:
context.load_verify_locations(ca)
if certfile and keyfile:
context.load_cert_chain(
certfile=certfile,
keyfile=keyfile,
password=keyfile_password,
)
try:
# Query the address to get back it's address family
addrs = socket.getaddrinfo(
address[0], address[1], 0, socket.SOCK_STREAM
)
conn = context.wrap_socket(module.socket(addrs[0][0]))
conn.settimeout(timeout_at)
conn.connect(address)
sock = conn
break
except ssl.SSLError:
raise
else:
try:
# if we got a timeout, lets ensure that we decrement the time
# otherwise there is no timeout set and we'll call it as such
sock = module.create_connection(address, timeout_at)
break
except Exception as ex:
errnum = ex.errno if isinstance(ex, OSError) else ex[0]
if errnum == errno.EINTR:
continue
raise
if sock is None:
raise module.error
_set_default_tcpsock_options(module, sock)
return sock
def capture_exceptions(async_result):
"""Return a new decorated function that propagates the exceptions of the
wrapped function to an async_result.
:param async_result: An async result implementing :class:`IAsyncResult`
"""
def capture(function):
@functools.wraps(function)
def captured_function(*args, **kwargs):
try:
return function(*args, **kwargs)
except Exception as exc:
async_result.set_exception(exc)
return captured_function
return capture
def wrap(async_result):
"""Return a new decorated function that propagates the return value or
exception of wrapped function to an async_result. NOTE: Only propagates a
non-None return value.
:param async_result: An async result implementing :class:`IAsyncResult`
"""
def capture(function):
@capture_exceptions(async_result)
def captured_function(*args, **kwargs):
value = function(*args, **kwargs)
if value is not None:
async_result.set(value)
return value
return captured_function
return capture
def fileobj_to_fd(fileobj):
"""Return a file descriptor from a file object.
Parameters:
fileobj -- file object or file descriptor
Returns:
corresponding file descriptor
Raises:
TypeError if the object is invalid
"""
if isinstance(fileobj, int):
fd = fileobj
else:
try:
fd = int(fileobj.fileno())
except (AttributeError, TypeError, ValueError):
raise TypeError("Invalid file object: " "{!r}".format(fileobj))
if fd < 0:
raise TypeError("Invalid file descriptor: {}".format(fd))
return fd
def selector_select(
rlist, wlist, xlist, timeout=None, selectors_module=selectors
):
"""Selector-based drop-in replacement for select to overcome select
limitation on a maximum filehandle value.
"""
if timeout is not None:
if not isinstance(timeout, (int, float)):
raise TypeError("timeout must be a number")
if timeout < 0:
raise ValueError("timeout must be non-negative")
events_mapping = {
selectors_module.EVENT_READ: rlist,
selectors_module.EVENT_WRITE: wlist,
}
fd_events = defaultdict(int)
fd_fileobjs = defaultdict(list)
for event, fileobjs in events_mapping.items():
for fileobj in fileobjs:
fd = fileobj_to_fd(fileobj)
fd_events[fd] |= event
fd_fileobjs[fd].append(fileobj)
selector = selectors_module.DefaultSelector()
for fd, events in fd_events.items():
try:
selector.register(fd, events)
except (ValueError, OSError) as e:
# gevent can raise OSError
raise ValueError("Invalid event mask or fd") from e
revents, wevents, xevents = [], [], []
try:
ready = selector.select(timeout)
finally:
selector.close()
for info in ready:
k, events = info
if events & selectors_module.EVENT_READ:
revents.extend(fd_fileobjs[k.fd])
elif events & selectors_module.EVENT_WRITE:
wevents.extend(fd_fileobjs[k.fd])
return revents, wevents, xevents
|