2017-09-30 13:42:04 +02:00
|
|
|
from random import choice
|
2017-09-25 01:17:04 +02:00
|
|
|
from threading import RLock
|
2017-10-06 12:19:34 +02:00
|
|
|
from time import time
|
2017-09-25 01:17:04 +02:00
|
|
|
|
2017-10-06 12:19:34 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2017-09-25 01:17:04 +02:00
|
|
|
from singleton import Singleton
|
|
|
|
|
|
|
|
# randomise routes after 600 seconds
|
|
|
|
REASSIGN_INTERVAL = 600
|
|
|
|
FLUFF_TRIGGER_TIMEOUT = 300
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class DandelionStems():
|
|
|
|
def __init__(self):
|
|
|
|
self.stem = {}
|
2017-09-30 13:42:04 +02:00
|
|
|
self.source = {}
|
2017-09-25 01:17:04 +02:00
|
|
|
self.timeouts = {}
|
|
|
|
self.lock = RLock()
|
|
|
|
|
2017-09-30 13:42:04 +02:00
|
|
|
def add(self, hashId, source, stems):
|
2017-10-06 12:19:34 +02:00
|
|
|
if BMConfigParser().safeGetInt('network', 'dandelion') == 0:
|
|
|
|
return
|
2017-09-25 01:17:04 +02:00
|
|
|
with self.lock:
|
2017-10-06 12:19:34 +02:00
|
|
|
try:
|
|
|
|
self.stem[hashId] = choice(stems)
|
|
|
|
except IndexError:
|
|
|
|
self.stem = None
|
2017-09-30 13:42:04 +02:00
|
|
|
self.source[hashId] = source
|
2017-10-06 12:19:34 +02:00
|
|
|
self.timeouts[hashId] = time()
|
2017-09-25 01:17:04 +02:00
|
|
|
|
|
|
|
def remove(self, hashId):
|
|
|
|
with self.lock:
|
|
|
|
try:
|
|
|
|
del self.stem[hashId]
|
2017-09-30 13:42:04 +02:00
|
|
|
del self.source[hashId]
|
2017-09-25 01:17:04 +02:00
|
|
|
del self.timeouts[hashId]
|
|
|
|
except KeyError:
|
|
|
|
pass
|