/
io.py
306 lines (262 loc) · 9.58 KB
/
io.py
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
"""AMQPStorm Connection.IO."""
import logging
import select
import socket
import threading
from errno import EAGAIN
from errno import EINTR
from errno import EWOULDBLOCK
from amqpstorm import compatibility
from amqpstorm.base import MAX_FRAME_SIZE
from amqpstorm.compatibility import ssl
from amqpstorm.exception import AMQPConnectionError
EMPTY_BUFFER = bytes()
LOGGER = logging.getLogger(__name__)
POLL_TIMEOUT = 1.0
class Poller(object):
"""Socket Read Poller."""
def __init__(self, fileno, exceptions, timeout=5):
self.select = select
self._fileno = fileno
self._exceptions = exceptions
self.timeout = timeout
@property
def fileno(self):
"""Socket Fileno.
:return:
"""
return self._fileno
@property
def is_ready(self):
"""Is Socket Ready.
:rtype: tuple
"""
try:
ready, _, _ = self.select.select([self.fileno], [], [],
POLL_TIMEOUT)
return bool(ready)
except self.select.error as why:
if why.args[0] != EINTR:
self._exceptions.append(AMQPConnectionError(why))
return False
class IO(object):
"""Internal Input/Output handler."""
def __init__(self, parameters, exceptions=None, on_read_impl=None):
self._exceptions = exceptions
self._wr_lock = threading.Lock()
self._rd_lock = threading.Lock()
self._inbound_thread = None
self._on_read_impl = on_read_impl
self._running = threading.Event()
self._parameters = parameters
self.data_in = EMPTY_BUFFER
self.poller = None
self.socket = None
self.use_ssl = self._parameters['ssl']
def close(self):
"""Close Socket.
:return:
"""
self._wr_lock.acquire()
self._rd_lock.acquire()
try:
self._running.clear()
self._close_socket()
finally:
self._wr_lock.release()
self._rd_lock.release()
if self._inbound_thread:
self._inbound_thread.join(timeout=self._parameters['timeout'])
self.socket = None
self.poller = None
self._inbound_thread = None
def open(self):
"""Open Socket and establish a connection.
:raises AMQPConnectionError: Raises if the connection
encountered an error.
:return:
"""
self._wr_lock.acquire()
self._rd_lock.acquire()
try:
self.data_in = EMPTY_BUFFER
self._running.set()
sock_addresses = self._get_socket_addresses()
self.socket = self._find_address_and_connect(sock_addresses)
self.poller = Poller(self.socket.fileno(), self._exceptions,
timeout=self._parameters['timeout'])
self._inbound_thread = self._create_inbound_thread()
finally:
self._wr_lock.release()
self._rd_lock.release()
def write_to_socket(self, frame_data):
"""Write data to the socket.
:param str frame_data:
:return:
"""
self._wr_lock.acquire()
try:
total_bytes_written = 0
bytes_to_send = len(frame_data)
while total_bytes_written < bytes_to_send:
try:
if not self.socket:
raise socket.error('connection/socket error')
bytes_written = (
self.socket.send(frame_data[total_bytes_written:])
)
if bytes_written == 0:
raise socket.error('connection/socket error')
total_bytes_written += bytes_written
except socket.timeout:
pass
except socket.error as why:
if why.args[0] in (EWOULDBLOCK, EAGAIN):
continue
self._exceptions.append(AMQPConnectionError(why))
return
finally:
self._wr_lock.release()
def _close_socket(self):
"""Shutdown and close the Socket.
:return:
"""
if not self.socket:
return
try:
if self.use_ssl:
self.socket.unwrap()
self.socket.shutdown(socket.SHUT_RDWR)
except (OSError, socket.error, ValueError):
pass
self.socket.close()
def _get_socket_addresses(self):
"""Get Socket address information.
:rtype: list
"""
family = socket.AF_UNSPEC
if not socket.has_ipv6:
family = socket.AF_INET
try:
addresses = socket.getaddrinfo(self._parameters['hostname'],
self._parameters['port'], family,
socket.SOCK_STREAM)
except socket.gaierror as why:
raise AMQPConnectionError(why)
return addresses
def _find_address_and_connect(self, addresses):
"""Find and connect to the appropriate address.
:param addresses:
:raises AMQPConnectionError: Raises if the connection
encountered an error.
:rtype: socket.socket
"""
error_message = None
for address in addresses:
sock = self._create_socket(socket_family=address[0])
try:
sock.connect(address[4])
except (IOError, OSError) as why:
error_message = why.strerror
continue
return sock
raise AMQPConnectionError(
'Could not connect to %s:%d error: %s' % (
self._parameters['hostname'], self._parameters['port'],
error_message
)
)
def _create_socket(self, socket_family):
"""Create Socket.
:param int socket_family:
:rtype: socket.socket
"""
sock = socket.socket(socket_family, socket.SOCK_STREAM, 0)
sock.settimeout(self._parameters['timeout'] or None)
if self.use_ssl:
if not compatibility.SSL_SUPPORTED:
raise AMQPConnectionError(
'Python not compiled with support for TLSv1 or higher'
)
sock = self._ssl_wrap_socket(sock)
return sock
def _ssl_wrap_socket(self, sock):
"""Wrap SSLSocket around the Socket.
:param socket.socket sock:
:rtype: SSLSocket
"""
context = self._parameters['ssl_options'].get('context')
if context is not None:
hostname = self._parameters['ssl_options'].get('server_hostname')
return context.wrap_socket(
sock, do_handshake_on_connect=True,
server_hostname=hostname
)
hostname = self._parameters['hostname']
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
mode = self._parameters['ssl_options'].get('verify_mode', 'none')
if mode.lower() == 'required':
context.verify_mode = ssl.CERT_REQUIRED
else:
context.verify_mode = ssl.CERT_NONE
check = self._parameters['ssl_options'].get('check_hostname', False)
context.check_hostname = check
context.load_default_certs()
return context.wrap_socket(sock, do_handshake_on_connect=True,
server_hostname=hostname)
def _create_inbound_thread(self):
"""Internal Thread that handles all incoming traffic.
:rtype: threading.Thread
"""
inbound_thread = threading.Thread(target=self._process_incoming_data,
name=__name__)
inbound_thread.daemon = True
inbound_thread.start()
return inbound_thread
def _process_incoming_data(self):
"""Retrieve and process any incoming data.
:return:
"""
while self._running.is_set():
if self.poller.is_ready:
self.data_in += self._receive()
self.data_in = self._on_read_impl(self.data_in)
def _receive(self):
"""Receive any incoming socket data.
If an error is thrown, handle it and return an empty string.
:return: data_in
:rtype: bytes
"""
data_in = EMPTY_BUFFER
try:
data_in = self._read_from_socket()
if len(data_in) == 0:
raise socket.error("connection closed by server")
except socket.timeout:
pass
except compatibility.SSLWantReadError:
# NOTE(visobet): Retry if the non-blocking socket does not
# have any meaningful data ready.
pass
except (IOError, OSError, ValueError) as why:
if why.args[0] not in (EWOULDBLOCK, EAGAIN):
self._exceptions.append(AMQPConnectionError(why))
if self._running.is_set():
LOGGER.warning(
"Stopping inbound thread due to %s", why,
exc_info=True
)
self._running.clear()
return data_in
def _read_from_socket(self):
"""Read data from the socket.
:rtype: bytes
"""
if not self.use_ssl:
if not self.socket:
raise socket.error('connection/socket error')
return self.socket.recv(MAX_FRAME_SIZE)
with self._rd_lock:
if not self.socket:
raise socket.error('connection/socket error')
return self.socket.read(MAX_FRAME_SIZE)