Skip to content

Commit

Permalink
#188067891 : Added suppor to refresh connection pool on ConnectionErr…
Browse files Browse the repository at this point in the history
…or. (#24)

* Added suppor to refresh connection pool on ConnectionError.

* Log message on ConnectionError and Attempt to reestablish connection pool.
  • Loading branch information
praves77 authored Oct 8, 2024
1 parent bad0dbe commit 289e66f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion moesifapi/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Configuration(object):

# Your Application Id for authentication/authorization
application_id = 'SET_ME'
version = 'moesifapi-python/1.5.0'
version = 'moesifapi-python/1.5.1'

pool_connections = 10
pool_maxsize = 10
53 changes: 47 additions & 6 deletions moesifapi/http/requests_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
from .http_client import HttpClient
from .http_response import HttpResponse
from .http_method_enum import HttpMethodEnum
from requests.packages.urllib3.util.retry import Retry


# Decorator to refresh connection pool on ConnectionError
def refresh_session(func):
# Wrap function with retry once on ConnectionError
def wrapper(self, *args, **kwargs):
try:
result = func(self, *args, **kwargs)
except requests.exceptions.ConnectionError as e:
print(f"ConnectionError: Attempt to reestablish the connection")
# Attempt to reestablish the connection
self.__refresh_connection_pool__()

# retry
result = func(self, *args, **kwargs)

return result

return wrapper


class RequestsClient(HttpClient):

Expand All @@ -20,13 +41,32 @@ class RequestsClient(HttpClient):
"""

# connection pool here
def __create_connection_pool__(self):
retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
)

def __init__(self):
self.session = requests.Session()
self.adapter = HTTPAdapter(pool_connections=Configuration.pool_connections, pool_maxsize=Configuration.pool_maxsize)
self.adapter = HTTPAdapter(
max_retries=retry_strategy,
pool_connections=Configuration.pool_connections,
pool_maxsize=Configuration.pool_maxsize
)
self.session.mount('http://', self.adapter)
self.session.mount('https://', self.adapter)

def __refresh_connection_pool__(self):
# Attempt to reestablish the connection.
# clear closes all open connections - leaves in-flight connections, but it will not be re-used after completion.
# It automatically opens a new ConnectionPool if no open connections exist for the request.
self.adapter.poolmanager.clear()

def __init__(self):
self.__create_connection_pool__()

@refresh_session
def execute_as_string(self, request):
"""Execute a given HttpRequest to get a string response back
Expand All @@ -40,7 +80,7 @@ def execute_as_string(self, request):
auth = None

if request.username or request.password:
auth=(request.username, request.password)
auth = (request.username, request.password)

# connection pool to make request
response = self.session.request(HttpMethodEnum.to_string(request.http_method),
Expand All @@ -52,7 +92,8 @@ def execute_as_string(self, request):
auth=auth)

return self.convert_response(response, False)


@refresh_session
def execute_as_binary(self, request):
"""Execute a given HttpRequest to get a binary response back
Expand All @@ -66,7 +107,7 @@ def execute_as_binary(self, request):
auth = None

if request.username or request.password:
auth=(request.username, request.password)
auth = (request.username, request.password)

response = requests.request(HttpMethodEnum.to_string(request.http_method),
request.query_url,
Expand All @@ -92,4 +133,4 @@ def convert_response(self, response, binary):
if binary == True:
return HttpResponse(response.status_code, response.headers, response.content)
else:
return HttpResponse(response.status_code, response.headers, response.text)
return HttpResponse(response.status_code, response.headers, response.text)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.5.0',
version='1.5.1',

description='Moesif API Lib for Python',
long_description=long_description,
Expand Down

0 comments on commit 289e66f

Please sign in to comment.