-
Notifications
You must be signed in to change notification settings - Fork 1
/
sockettimeout.repy
301 lines (217 loc) · 7.48 KB
/
sockettimeout.repy
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
"""
<Author>
Justin Cappos, Armon Dadgar
This is a rewrite of the previous version by Richard Jordan
<Start Date>
26 Aug 2009
<Description>
A library that causes sockets to timeout if a recv / send call would
block for more than an allotted amount of time.
"""
class SocketTimeoutError(Exception):
"""The socket timed out before receiving a response"""
class _timeout_socket():
"""
<Purpose>
Provides a socket like object which supports custom timeouts
for send() and recv().
"""
# Initialize with the socket object and a default timeout
def __init__(self,socket,timeout=10, checkintv='fibonacci'):
"""
<Purpose>
Initializes a timeout socket object.
<Arguments>
socket:
A socket like object to wrap. Must support send,recv,close, and willblock.
timeout:
The default timeout for send() and recv().
checkintv:
How often socket operations (send,recv) should check if
they can run. The smaller the interval the more time is
spent busy waiting.
"""
# Store the socket, timeout and check interval
self.socket = socket
self.timeout = timeout
self.checkintv = checkintv
# Allow changing the default timeout
def settimeout(self,timeout=10):
"""
<Purpose>
Allows changing the default timeout interval.
<Arguments>
timeout:
The new default timeout interval. Defaults to 10.
Use 0 for no timeout. Given in seconds.
"""
# Update
self.timeout = timeout
# Wrap willblock
def willblock(self):
"""
See socket.willblock()
"""
return self.socket.willblock()
# Wrap close
def close(self):
"""
See socket.close()
"""
return self.socket.close()
# Provide a recv() implementation
def recv(self,bytes,timeout=None):
"""
<Purpose>
Allows receiving data from the socket object with a custom timeout.
<Arguments>
bytes:
The maximum amount of bytes to read
timeout:
(Optional) Defaults to the value given at initialization, or by settimeout.
If provided, the socket operation will timeout after this amount of time (sec).
Use 0 for no timeout.
<Exceptions>
As with socket.recv(), socket.willblock(). Additionally, SocketTimeoutError is
raised if the operation times out.
<Returns>
The data received from the socket.
"""
# It's worth noting that this fibonacci backoff begins with a 2ms poll rate, and
# provides a simple exponential backoff scheme.
fibonacci_backoff = False
backoff_cap = 100 # Never use more than 100ms poll rate.
pre_value = 1.0 # Our iterators for Fibonacci sequence.
pre_pre_value = 1.0 #
# Since we want to be able to initialize with static poll rates (backwards
# compatibility) we specify a string if we're using the fibonacci backoff.
if type(self.checkintv) is str:
if self.checkintv == 'fibonacci':
fibonacci_backoff = True
# Set the timeout if None
if timeout is None:
timeout = self.timeout
# Get the start time
starttime = getruntime()
# Block until we can read
rblock, wblock = self.socket.willblock()
while rblock:
# Check if we should break
if timeout > 0:
# Get the elapsed time
diff = getruntime() - starttime
# Raise an exception
if diff > timeout:
raise SocketTimeoutError,"recv() timed out!"
if fibonacci_backoff:
# Iterate the sequence once
sleep_length = pre_value + pre_pre_value
pre_pre_value = pre_value
pre_value = sleep_length
# Make sure we don't exceed maximum backoff.
if sleep_length > backoff_cap:
sleep_length = backoff_cap
# Unit conversion to seconds
sleep_length = sleep_length / 1000.0
# Sleep
sleep(sleep_length)
else: # Classic functionality.
# Sleep
try:
sleep(float(self.checkintv))
except:
sleep(0.1)
# If available, move to the next value of checkintv.
# Update rblock
rblock, wblock = self.socket.willblock()
# Do the recv
return self.socket.recv(bytes)
# Provide a send() implementation
def send(self,data,timeout=None):
"""
<Purpose>
Allows sending data with the socket object with a custom timeout.
<Arguments>
data:
The data to send
timeout:
(Optional) Defaults to the value given at initialization, or by settimeout.
If provided, the socket operation will timeout after this amount of time (sec).
Use 0 for no timeout.
<Exceptions>
As with socket.send(), socket.willblock(). Additionally, SocketTimeoutError is
raised if the operation times out.
<Returns>
The number of bytes sent.
"""
# Set the timeout if None
if timeout is None:
timeout = self.timeout
# Get the start time
starttime = getruntime()
# Block until we can write
rblock, wblock = self.socket.willblock()
while wblock:
# Check if we should break
if timeout > 0:
# Get the elapsed time
diff = getruntime() - starttime
# Raise an exception
if diff > timeout:
raise SocketTimeoutError,"send() timed out!"
# Sleep
# Since switching to the fibonacci backoff, the nature of
# this field has changed. Rather than implement the backoff
# for checking block status (seems wasteful) we'll just use
# a constant value. Ten ms seems appropriate.
sleep(0.010)
# Update rblock
rblock, wblock = self.socket.willblock()
# Do the recv
return self.socket.send(data)
def timeout_openconn(desthost, destport, localip=None, localport=None, timeout=5):
"""
<Purpose>
Wrapper for openconn. Very, very similar
<Args>
Same as Repy openconn
<Exception>
Raises the same exceptions as openconn.
<Side Effects>
Creates a socket object for the user
<Returns>
socket obj on success
"""
realsocketlikeobject = openconn(desthost, destport, localip, localport, timeout)
thissocketlikeobject = _timeout_socket(realsocketlikeobject, timeout)
return thissocketlikeobject
def timeout_waitforconn(localip, localport, function, timeout=5):
"""
<Purpose>
Wrapper for waitforconn. Essentially does the same thing...
<Args>
Same as Repy waitforconn with the addition of a timeout argument.
<Exceptions>
Same as Repy waitforconn
<Side Effects>
Sets up event listener which calls function on messages.
<Returns>
Handle to listener.
"""
# We use a closure for the callback we pass to waitforconn so that we don't
# have to map mainch's to callback functions or deal with potential race
# conditions if we did maintain such a mapping.
def _timeout_waitforconn_callback(localip, localport, sockobj, ch, mainch):
# 'timeout' is the free variable 'timeout' that was the argument to
# timeout_waitforconn.
thissocketlikeobject = _timeout_socket(sockobj, timeout)
# 'function' is the free variable 'function' that was the argument to
# timeout_waitforconn.
return function(localip, localport, thissocketlikeobject, ch, mainch)
return waitforconn(localip, localport, _timeout_waitforconn_callback)
# a wrapper for stopcomm
def timeout_stopcomm(commhandle):
"""
Wrapper for stopcomm. Does the same thing...
"""
return stopcomm(commhandle)