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

Make RequestsClient thread-safe #531

Merged
merged 1 commit into from
Jan 30, 2019
Merged
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
11 changes: 7 additions & 4 deletions stripe/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import email
import time
import random
import threading

import stripe
from stripe import error, util, six
Expand Down Expand Up @@ -101,6 +102,8 @@ def __init__(self, verify_ssl_certs=True, proxy=None):
)
self._proxy = proxy.copy() if proxy else None

self._thread_local = threading.local()

def request_with_retries(self, method, url, headers, post_data=None):
num_retries = 0

Expand Down Expand Up @@ -191,7 +194,7 @@ class RequestsClient(HTTPClient):
def __init__(self, timeout=80, session=None, **kwargs):
super(RequestsClient, self).__init__(**kwargs)
self._timeout = timeout
self._session = session or requests.Session()
self._thread_local.session = session or requests.Session()

def request(self, method, url, headers, post_data=None):
kwargs = {}
Expand All @@ -205,7 +208,7 @@ def request(self, method, url, headers, post_data=None):

try:
try:
result = self._session.request(
result = self._thread_local.session.request(
method,
url,
headers=headers,
Expand Down Expand Up @@ -285,8 +288,8 @@ def _handle_request_error(self, e):
raise error.APIConnectionError(msg, should_retry=should_retry)

def close(self):
if self._session is not None:
self._session.close()
if self._thread_local.session is not None:
self._thread_local.session.close()


class UrlFetchClient(HTTPClient):
Expand Down