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

Reuse connections when making API calls #14

Merged
merged 1 commit into from
May 16, 2018
Merged

Conversation

stevenjm
Copy link
Contributor

@stevenjm stevenjm commented May 9, 2018

By default, the Python requests module will create a new session object each time it is called. This means that each requests.get call has a separate connection pool and cannot reuse connections created by previous requests.

Here we create a class variable with a requests session, which is used for all instances of the APIClient class.

This does mean that sessions will now be shared between all APIClient objects in a process, even if they use different credentials. I do not believe this is a problem because requests are independently authenticated, but I would appreciate a review from someone more familiar with this code anyway.

By default, the Python requests module will create a new session
object each time it is called. This means that each requests.get
call has a separate connection pool and cannot reuse connections
created by previous requests.

Here we create a class variable with a requests session, which is
used for all instances of the APIClient class.
@stevenjm stevenjm requested a review from tzengerink May 9, 2018 09:26
@coveralls
Copy link

coveralls commented May 9, 2018

Coverage Status

Coverage increased (+0.2%) to 78.899% when pulling 8a80f77 on reuse-connections into 77f25ad on master.

@stevenjm stevenjm requested review from gwkunze and mvijftigschild May 9, 2018 09:36
@tzengerink
Copy link
Contributor

Reusing the connection is a nice improvement indeed. There's no real way to add tests for this, is there?

@stevenjm
Copy link
Contributor Author

stevenjm commented May 9, 2018

We could maybe test to make sure that that the socket module's connect method is only called once per request, but I don't think it's worth it. The code works whether or not it reuses connections, and we already have tests for it working. This is just a performance optimisation.

@stevenjm stevenjm merged commit a83948f into master May 16, 2018
@stevenjm stevenjm deleted the reuse-connections branch May 16, 2018 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants