MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / checkConnections

Method checkConnections

src/Connection/ConnectionServer.py:239–320  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

237 self.connections.remove(connection)
238
239 def checkConnections(self):
240 run_i = 0
241 while self.running:
242 run_i += 1
243 time.sleep(15) # Check every minute
244 self.ip_incoming = {} # Reset connected ips counter
245 last_message_time = 0
246 s = time.time()
247 for connection in self.connections[:]: # Make a copy
248 if connection.ip.endswith(".onion") or config.tor == "always":
249 timeout_multipler = 2
250 else:
251 timeout_multipler = 1
252
253 idle = time.time() - max(connection.last_recv_time, connection.start_time, connection.last_message_time)
254 if connection.last_message_time > last_message_time and not connection.is_private_ip:
255 # Message from local IPs does not means internet connection
256 last_message_time = connection.last_message_time
257
258 if connection.unpacker and idle > 30:
259 # Delete the unpacker if not needed
260 del connection.unpacker
261 connection.unpacker = None
262
263 elif connection.last_cmd_sent == "announce" and idle > 20: # Bootstrapper connection close after 20 sec
264 connection.close("[Cleanup] Tracker connection, idle: %.3fs" % idle)
265
266 if idle > 60 * 60:
267 # Wake up after 1h
268 connection.close("[Cleanup] After wakeup, idle: %.3fs" % idle)
269
270 elif idle > 20 * 60 and connection.last_send_time < time.time() - 10:
271 # Idle more than 20 min and we have not sent request in last 10 sec
272 if not connection.ping():
273 connection.close("[Cleanup] Ping timeout")
274
275 elif idle > 10 * timeout_multipler and connection.incomplete_buff_recv > 0:
276 # Incomplete data with more than 10 sec idle
277 connection.close("[Cleanup] Connection buff stalled")
278
279 elif idle > 10 * timeout_multipler and connection.protocol == "?": # No connection after 10 sec
280 connection.close(
281 "[Cleanup] Connect timeout: %.3fs" % idle
282 )
283
284 elif idle > 10 * timeout_multipler and connection.waiting_requests and time.time() - connection.last_send_time > 10 * timeout_multipler:
285 # Sent command and no response in 10 sec
286 connection.close(
287 "[Cleanup] Command %s timeout: %.3fs" % (connection.last_cmd_sent, time.time() - connection.last_send_time)
288 )
289
290 elif idle < 60 and connection.bad_actions > 40:
291 connection.close(
292 "[Cleanup] Too many bad actions: %s" % connection.bad_actions
293 )
294
295 elif idle > 5 * 60 and connection.sites == 0:
296 connection.close(

Callers

nothing calls this directly

Calls 5

onInternetOfflineMethod · 0.95
onInternetOnlineMethod · 0.95
getTimecorrectionMethod · 0.95
closeMethod · 0.45
pingMethod · 0.45

Tested by

no test coverage detected