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

Method startFindOptional

src/Worker/WorkerManager.py:277–402  ·  view source on GitHub ↗
(self, reset_task=False, find_more=False, high_priority=False)

Source from the content-addressed store, hash-verified

275 # Start find peers for optional files
276 @util.Noparallel(blocking=False, ignore_args=True)
277 def startFindOptional(self, reset_task=False, find_more=False, high_priority=False):
278 # Wait for more file requests
279 if len(self.tasks) < 20 or high_priority:
280 time.sleep(0.01)
281 elif len(self.tasks) > 90:
282 time.sleep(5)
283 else:
284 time.sleep(0.5)
285
286 optional_tasks = [task for task in self.tasks if task["optional_hash_id"]]
287 if not optional_tasks:
288 return False
289 optional_hash_ids = set([task["optional_hash_id"] for task in optional_tasks])
290 time_tasks = self.time_task_added
291
292 self.log.debug(
293 "Finding peers for optional files: %s (reset_task: %s, find_more: %s)" %
294 (optional_hash_ids, reset_task, find_more)
295 )
296 found = self.findOptionalTasks(optional_tasks, reset_task=reset_task)
297
298 if found:
299 found_peers = set([peer for peers in list(found.values()) for peer in peers])
300 self.startWorkers(found_peers, force_num=3, reason="Optional found in local peers")
301
302 if len(found) < len(optional_hash_ids) or find_more or (high_priority and any(len(peers) < 10 for peers in found.values())):
303 self.log.debug("No local result for optional files: %s" % (optional_hash_ids - set(found)))
304
305 # Query hashfield from connected peers
306 threads = []
307 peers = self.site.getConnectedPeers()
308 if not peers:
309 peers = self.site.getConnectablePeers()
310 for peer in peers:
311 threads.append(gevent.spawn(peer.updateHashfield, force=find_more))
312 gevent.joinall(threads, timeout=5)
313
314 if time_tasks != self.time_task_added: # New task added since start
315 optional_tasks = [task for task in self.tasks if task["optional_hash_id"]]
316 optional_hash_ids = set([task["optional_hash_id"] for task in optional_tasks])
317
318 found = self.findOptionalTasks(optional_tasks)
319 self.log.debug("Found optional files after query hashtable connected peers: %s/%s" % (
320 len(found), len(optional_hash_ids)
321 ))
322
323 if found:
324 found_peers = set([peer for hash_id_peers in list(found.values()) for peer in hash_id_peers])
325 self.startWorkers(found_peers, force_num=3, reason="Optional found in connected peers")
326
327 if len(found) < len(optional_hash_ids) or find_more:
328 self.log.debug(
329 "No connected hashtable result for optional files: %s (asked: %s)" %
330 (optional_hash_ids - set(found), len(self.asked_peers))
331 )
332 if not self.tasks:
333 self.log.debug("No tasks, stopping finding optional peers")
334 return

Callers 3

checkTasksMethod · 0.95
removeWorkerMethod · 0.95
addTaskMethod · 0.95

Calls 6

findOptionalTasksMethod · 0.95
startWorkersMethod · 0.95
addOptionalPeersMethod · 0.95
valuesMethod · 0.80
getConnectedPeersMethod · 0.80
getConnectablePeersMethod · 0.80

Tested by

no test coverage detected