From d3656b543f1f3567cc4e55335c0a112f0e9b3a61 Mon Sep 17 00:00:00 2001 From: marcsello Date: Wed, 22 Apr 2020 01:48:29 +0200 Subject: [PATCH] Added timeout for requests --- communicator.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/communicator.py b/communicator.py index c3f8298..33bcf2b 100644 --- a/communicator.py +++ b/communicator.py @@ -6,6 +6,7 @@ Communicator module import logging import requests +import requests.exceptions __author__ = "@tormakris" __copyright__ = "Copyright 2020, GoldenPogácsa Team" @@ -38,8 +39,11 @@ class Communicator: """ currentconsumer=self.currenctconsumer LOGGER.info(f"Sending message to {currentconsumer}") - postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message}) - LOGGER.debug(f"Message status code is:{postresponse.status_code}") + try: + postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message}, timeout=5) + LOGGER.debug(f"Message status code is:{postresponse.status_code}") + except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e: + LOGGER.exception(e) # Fun fact: ez azt jelenti, hogy elveszett az üzenet... ide valami retry kellene inkább más consumerek felé... def discoveravailableconsumers(self) -> list: """Get the list of available consumer from the current primary consumer. Logs the received list. @@ -48,7 +52,7 @@ class Communicator: """ try: currentconsumer = self.currenctconsumer - response = requests.get(f'http://{currentconsumer}/consumers') + response = requests.get(f'http://{currentconsumer}/consumers', timeout=5) json = response.json() LOGGER.info(f"List of currently available consumers: {json}") return json @@ -63,7 +67,7 @@ class Communicator: """ currentconsumer = self.currenctconsumer try: - response = requests.get(f'http://{currentconsumer}/consumers') + response = requests.get(f'http://{currentconsumer}/consumers', timeout=5) isavailable = response.status_code == 200 except Exception as e: LOGGER.exception(e) @@ -78,7 +82,7 @@ class Communicator: :return: True if available, False otherwise """ try: - response = requests.get(f'http://{consumer}/consumers') + response = requests.get(f'http://{consumer}/consumers', timeout=5) isavailable = response.status_code == 200 except Exception as e: LOGGER.exception(e)