Browse Source

Add timeout for requests.post() operation

Like all network operations, performing the POST request may fail for various
reasons. When no response from the remote end is received, a requests.post()
call may never return, leading to an unbounded increase in the sender queue.

To remedy such situations let's add a timeout argument to the requests call and
make it configurable, to be adaptable for different environments.

For more information see
https://2.python-requests.org/en/master/user/quickstart/#timeouts
opensearch
Oliver Seemann 6 years ago
parent
commit
d06b5a16fb
2 changed files with 9 additions and 4 deletions
  1. +4
    -2
      logzio/handler.py
  2. +5
    -2
      logzio/sender.py

+ 4
- 2
logzio/handler.py View File

@ -17,7 +17,8 @@ class LogzioHandler(logging.Handler):
logs_drain_timeout=3,
url="https://listener.logz.io:8071",
debug=False,
backup_logs=True):
backup_logs=True,
network_timeout=10.0):
if not token:
raise LogzioException('Logz.io Token must be provided')
@ -29,7 +30,8 @@ class LogzioHandler(logging.Handler):
url=url,
logs_drain_timeout=logs_drain_timeout,
debug=debug,
backup_logs=backup_logs)
backup_logs=backup_logs,
network_timeout=network_timeout)
logging.Handler.__init__(self)
def extra_fields(self, message):


+ 5
- 2
logzio/sender.py View File

@ -33,12 +33,14 @@ class LogzioSender:
token, url='https://listener.logz.io:8071',
logs_drain_timeout=5,
debug=False,
backup_logs=True):
backup_logs=True,
network_timeout=10.0):
self.token = token
self.url = '{}/?token={}'.format(url, token)
self.logs_drain_timeout = logs_drain_timeout
self.logger = get_logger(debug)
self.backup_logs = backup_logs
self.network_timeout = network_timeout
# Function to see if the main thread is alive
self.is_main_thread_active = lambda: any(
@ -104,7 +106,8 @@ class LogzioSender:
should_retry = False
try:
response = requests.post(
self.url, headers=headers, data='\n'.join(logs_list))
self.url, headers=headers, data='\n'.join(logs_list),
timeout=self.network_timeout)
if response.status_code != 200:
if response.status_code == 400:
self.logger.info(


Loading…
Cancel
Save