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

Add option to create new session per request in RequestsClient #399

Open
wants to merge 1 commit into
base: master
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
9 changes: 9 additions & 0 deletions CHANGELOG-MASTER.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ Changelog-Master
*This file will contain the Changelog of the master branch.*

*The content will be used to build the Changelog of the new bravado release.*

Modified `RequestsClient`.

There is known [bug](https://github.com/requests/requests/issues/1906) in `requests` package which is still not fixed.

Basically it randomly raises `SSLError` when running multiple processes with same `Session`. We fixed it in out project
with little hack of yours class. If we create new session per request, the bug is gone.

Therefore I want to add option to `RequestsClient` for creating new `Session` per request.
7 changes: 5 additions & 2 deletions bravado/requests_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import copy
import logging

import requests
Expand Down Expand Up @@ -95,18 +96,20 @@ class RequestsClient(HttpClient):
"""Synchronous HTTP client implementation.
"""

def __init__(self, ssl_verify=True, ssl_cert=None):
def __init__(self, ssl_verify=True, ssl_cert=None, session_per_request=False):
"""
:param ssl_verify: Set to False to disable SSL certificate validation. Provide the path to a
CA bundle if you need to use a custom one.
:param ssl_cert: Provide a client-side certificate to use. Either a sequence of strings pointing
to the certificate (1) and the private key (2), or a string pointing to the combined certificate
and key.
:param session_per_request: Set to True to create new Session for every request.
"""
self.session = requests.Session()
self.authenticator = None
self.ssl_verify = ssl_verify
self.ssl_cert = ssl_cert
self.session_per_request = session_per_request

def separate_params(self, request_params):
"""Splits the passed in dict of request_params into two buckets.
Expand Down Expand Up @@ -151,7 +154,7 @@ def request(self, request_params, operation=None, request_config=None):
sanitized_params, misc_options = self.separate_params(request_params)

requests_future = RequestsFutureAdapter(
self.session,
copy.deepcopy(self.session) if self.session_per_request else self.session,
macisamuele marked this conversation as resolved.
Show resolved Hide resolved
self.authenticated_request(sanitized_params),
misc_options,
)
Expand Down