Skip to content

Commit

Permalink
Server timeout exception fix (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
saimonation committed Aug 9, 2022
1 parent 15b1722 commit e84b06f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions cterasdk/client/http.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import urllib.parse
import logging
import time

import requests
import requests.exceptions as requests_exceptions
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor

# from .ssl import CertificateServices
from ..convert import fromxmlstr
from ..convert import fromxmlstr, ParseException
from ..common import Object, merge
from .. import config
from ..exception import SSLException, HostUnreachable, ExhaustedException
Expand All @@ -20,7 +21,10 @@ def __init__(self, http_error):
self.response = Object()
self.response.code = http_error.response.status_code
self.response.reason = http_error.response.reason
self.response.body = fromxmlstr(http_error.response.text)
try:
self.response.body = fromxmlstr(http_error.response.text)
except ParseException:
self.response.body = ''
if config.http['verbose']:
self.response.headers = http_error.response.headers
self.request = HTTPException._parse_request(http_error.request)
Expand Down Expand Up @@ -91,7 +95,10 @@ def dispatch(self, ctera_request):
try:
return self._do_dispatch(ctera_request)
except requests_exceptions.HTTPError as error:
raise HTTPException(error)
http_exception = HTTPException(error)
if http_exception.response.code != 504:
raise http_exception
logging.getLogger().warning('Server timed out. %s', {'attempt': (attempt + 1)})
except requests_exceptions.Timeout:
self.on_timeout(attempt)
except requests_exceptions.SSLError as error:
Expand All @@ -101,7 +108,10 @@ def dispatch(self, ctera_request):
self._on_unreachable(error)
except requests_exceptions.RequestException as error:
logging.getLogger().warning(error)

attempt = attempt + 1
if attempt < self.retries:
time.sleep(self.timeout)
logging.getLogger().error('Reached maximum number of retries. %s', {'retries': self.retries, 'timeout': self.timeout})
raise ExhaustedException(self.retries, self.timeout)

Expand Down
4 changes: 2 additions & 2 deletions cterasdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def df():
)

http = dict(
timeout=20, # http client timeout (seconds)
retries=3, # handle connection timeout
timeout=10, # http client or server timeout (seconds)
retries=3, # handle client or server timeout
ssl='Consent', # ['Consent', 'Trust']
verbose=False # include request info on error
)
Expand Down

0 comments on commit e84b06f

Please sign in to comment.