Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update use_connection_pooling (False) to address session overwriting #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## [2.0]
## [2.0.1]
### Changed
- Updated `use_connection_pooling` option to address session overwriting.

## [2.0.0]
### Changed
- Updated transactional email request optional argument `amp_body` to `body_amp` for consistency across APIs ([#93](https://github.com/customerio/customerio-python/pull/93))

Expand Down
2 changes: 1 addition & 1 deletion customerio/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (2, 0, 0, 'final', 0)
VERSION = (2, 0, 1, 'final', 0)

def get_version():
version = '%s.%s' % (VERSION[0], VERSION[1])
Expand Down
46 changes: 12 additions & 34 deletions customerio/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@ def __init__(self, retries=3, timeout=10, backoff_factor=0.02, use_connection_po
@property
def http(self):
if self._current_session is None:
self._current_session = self._get_session()
self._current_session = self._build_session()

return self._current_session

def send_request(self, method, url, data):
'''Dispatches the request and returns a response'''

try:
response = self.http.request(
method, url=url, json=self._sanitize(data), timeout=self.timeout)

result_status = response.status_code
if result_status != 200:
raise CustomerIOException('%s: %s %s %s' % (result_status, url, data, response.text))
return response.text

if self.use_connection_pooling:
response = self.http.request(
method, url=url, json=self._sanitize(data), timeout=self.timeout)
else:
with self._build_session() as http:
response = http.request(
method, url=url, json=self._sanitize(data), timeout=self.timeout)
except Exception as e:
# Raise exception alerting user that the system might be
# experiencing an outage and refer them to system status page.
Expand All @@ -51,8 +49,10 @@ def send_request(self, method, url, data):
'''.format(klass=type(e), message=e, count=self.retries)
raise CustomerIOException(message)

finally:
self._close()
result_status = response.status_code
if result_status != 200:
raise CustomerIOException('%s: %s %s %s' % (result_status, url, data, response.text))
return response.text

def _sanitize(self, data):
for k, v in data.items():
Expand All @@ -77,21 +77,6 @@ def _stringify_list(self, customer_ids):
'customer_ids cannot be {type}'.format(type=type(v)))
return customer_string_ids

# gets a session based on whether we want pooling or not. If no pooling is desired, we create a new session each time.
def _get_session(self):
if (self.use_connection_pooling):
if (self._current_session is None):
self._current_session = self._build_session()

# if we're using pooling, return the existing session.
logging.debug("Using existing session...")
return self._current_session
else:
# if we're not using pooling, build a new session.
logging.debug("Creating new session...")
self._current_session = self._build_session()
return self._current_session

# builds the session.
def _build_session(self):
session = Session()
Expand All @@ -104,10 +89,3 @@ def _build_session(self):
HTTPAdapter(max_retries=Retry(total=self.retries, backoff_factor=self.backoff_factor)))

return session

# closes the session if we're not using connection pooling.
def _close(self):
# if we're not using pooling; clean up the resources.
if (not self.use_connection_pooling):
self._current_session.close()
self._current_session = None
2 changes: 1 addition & 1 deletion customerio/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ def _build_session(self):
session = super()._build_session()
session.auth = (self.site_id, self.api_key)

return session
return session