From 99e2307f0934cb7df5b667d65a13619905ac7b3b Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Mon, 6 Jun 2022 23:54:46 +0200 Subject: [PATCH] Harden network --- config.py | 2 ++ networking/Networking.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index 9adcac9..508825c 100644 --- a/config.py +++ b/config.py @@ -26,3 +26,5 @@ ORANGE_SPECIAL = SpecialEvent.points ORANGE_SPECIAL_BANK = ORANGE_SPECIAL_BANK_OPTIONS["BOTH"] ## Tech config WAIT_TIME_TO_RESET_SECONDS = 0.6 +MAX_NETWORK_RETRIES = 10 +NETWORK_SLEEP_TIME_SECONDS = 0.05 diff --git a/networking/Networking.py b/networking/Networking.py index b2b19f3..ada291c 100644 --- a/networking/Networking.py +++ b/networking/Networking.py @@ -1,11 +1,13 @@ import requests_unixsocket as req - +import config +from time import sleep import socket import json - +import logging class Networking: def __init__(self, output_server_address, input_socket_address): + self.retries = 0 self.server_address = "" self.input_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.output_session = req.Session() @@ -16,7 +18,16 @@ class Networking: print("Connected to " + input_socket_address + " and started server " + self.server_address) def get(self, path): - response = self.output_session.get(self.server_address + path) + if self.retries < config.MAX_NETWORK_RETRIES: + logging.error("Network exception. Could not get " + path) + + try: + response = self.output_session.get(self.server_address + path) + except Exception: + self.retries += 1 + sleep(config.NETWORK_SLEEP_TIME) + return self.get(path) + assert response.status_code == 200 return response.content