plugin - redesigns retrier using backoff

This commit is contained in:
Sergi Delgado Segura
2020-04-29 13:08:49 +02:00
parent 390dc3e090
commit 8575bc6209
2 changed files with 56 additions and 46 deletions

View File

@@ -4,3 +4,4 @@ coincurve
cryptography==2.8 cryptography==2.8
pyzbase32 pyzbase32
plyvel plyvel
backoff

View File

@@ -1,59 +1,68 @@
import backoff
from threading import Thread
from tower_info import TowerInfo from tower_info import TowerInfo
from net.http import send_appointment from net.http import add_appointment
from exceptions import TowerConnectionError, TowerResponseError
class Retrier: MAX_RETRIES = None
def __init__(self, retry_delta, max_retries, temp_unreachable_towers):
self.retry_delta = retry_delta
self.max_retries = max_retries
self.temp_unreachable_towers = temp_unreachable_towers
self.retry_count = {}
def do_retry(self, plugin):
while True:
tower_id = self.temp_unreachable_towers.get()
tower_info = TowerInfo.from_dict(plugin.wt_client.db_manager.load_tower_record(tower_id))
try: def on_backoff(details):
for appointment_dict, signature in plugin.wt_client.towers[tower_id]["pending_appointments"]: plugin = details.get("args")[1]
plugin.log("Retrying: sending appointment to {}".format(tower_id)) tower_id = details.get("args")[2]
response = send_appointment(tower_id, tower_info, appointment_dict, signature) plugin.log("Retry {} failed for tower {}, backing off".format(details.get("tries"), tower_id))
plugin.log("Appointment accepted and signed by {})".format(tower_id))
plugin.log("Remaining slots: {}".format(response.get("available_slots")))
tower_info.appointments[appointment_dict.get("locator")] = response.get("signature")
tower_info.available_slots = response.get("available_slots")
# Update memory and TowersDB def on_giveup(details):
tower_info.pending_appointments.remove([appointment_dict, signature]) plugin = details.get("args")[1]
plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info) tower_id = details.get("args")[2]
plugin.wt_client.towers[tower_id] = tower_info.get_summary() tower_info = details.get("args")[3]
if tower_id in self.retry_count:
self.retry_count.pop(tower_id)
tower_info.status = "reachable"
plugin.wt_client.towers[tower_id]["status"] = "reachable"
plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info)
except TowerConnectionError:
if tower_id not in self.retry_count:
self.retry_count[tower_id] = 1
else:
plugin.log("Retry {} failed for tower {}, backing off".format(self.retry_count[tower_id], tower_id))
self.retry_count[tower_id] += 1
if self.retry_count[tower_id] <= self.max_retries:
self.temp_unreachable_towers.put(tower_id)
else:
plugin.log("Max retries reached, abandoning tower {}".format(tower_id)) plugin.log("Max retries reached, abandoning tower {}".format(tower_id))
self.retry_count.pop(tower_id)
tower_info.status = "unreachable" tower_info.status = "unreachable"
plugin.wt_client.towers[tower_id]["status"] = "unreachable" plugin.wt_client.towers[tower_id]["status"] = "unreachable"
plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info) plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info)
except TowerResponseError as e:
# FIXME: deal with tower errors, such as no available slots def set_max_retries(max_retries):
plugin.log(str(e)) global MAX_RETRIES
MAX_RETRIES = max_retries
def max_retries():
return MAX_RETRIES
class Retrier:
def __init__(self, max_retries, temp_unreachable_towers):
self.temp_unreachable_towers = temp_unreachable_towers
set_max_retries(max_retries)
def manage_retry(self, plugin):
while True:
tower_id = self.temp_unreachable_towers.get()
tower_info = TowerInfo.from_dict(plugin.wt_client.db_manager.load_tower_record(tower_id))
Thread(target=self.do_retry, args=[plugin, tower_id, tower_info], daemon=True).start()
@backoff.on_predicate(
backoff.expo,
lambda x: x == "temporarily unreachable",
max_tries=max_retries,
on_backoff=on_backoff,
on_giveup=on_giveup,
)
def do_retry(self, plugin, tower_id, tower_info):
for appointment_dict, signature in plugin.wt_client.towers[tower_id]["pending_appointments"]:
status = add_appointment(plugin, tower_id, tower_info, appointment_dict, signature)
if status in ["reachable", "misbehaving"]:
tower_info.pending_appointments.remove([appointment_dict, signature])
# Update memory and TowersDB
plugin.wt_client.update_tower_state(tower_id, tower_info)
else:
return status