Repository: liberapay/liberapay.com

Issue:
Each request to the MangoPay API opens a new HTTPS connection
I never really looked into this problem and thought it was probably because of the server side, but [this Stack Overflow answer](http://stackoverflow.com/questions/37654878/how-to-make-braintrees-python-client-reuse-connections) suggests that it's actually the python library being stupid. Reusing connections could significantly speed up payday and the views on liberapay.com that call the MangoPay API.

(Many thanks to @rohitpaulk for posting the link in https://github.com/gratipay/inside.gratipay.com/issues/729#issuecomment-234302583.)

Each request to the MangoPay API opens a new HTTPS connection
I never really looked into this problem and thought it was probably because of the server side, but [this Stack Overflow answer](http://stackoverflow.com/questions/37654878/how-to-make-braintrees-python-client-reuse-connections) suggests that it's actually the python library being stupid. Reusing connections could significantly speed up payday and the views on liberapay.com that call the MangoPay API.

(Many thanks to @rohitpaulk for posting the link in https://github.com/gratipay/inside.gratipay.com/issues/729#issuecomment-234302583.)



Discussion:
Okay, so there are two problems here.

First, despite [requests' claim](http://docs.python-requests.org/en/master/) that "Keep-alive and HTTP connection pooling are 100% automatic", they actually only work if you use a `Session` (that seems illogical to me, connection reuse and sessions are unrelated).

```
>>> import requests
>>> requests.get('https://www.mangopay.com/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.mangopay.com
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
>>> requests.get('https://www.mangopay.com/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.mangopay.com
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
>>> session = requests.Session()
>>> session.get('https://www.mangopay.com/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.mangopay.com
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
>>> session.get('https://www.mangopay.com/')
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
```

Second, the mangopay library [creates a new Session for each request](https://github.com/Mangopay/mangopay2-python-sdk/blob/v2/mangopaysdk/tools/resttool.py#L110).

I've reported the first problem upstream: https://github.com/kennethreitz/requests/issues/3445.

… and I was politely told to fuck off.

PR opened upstream for the second problem: https://github.com/Mangopay/mangopay2-python-sdk/pull/49.

Okay, so there are two problems here.

First, despite [requests' claim](http://docs.python-requests.org/en/master/) that "Keep-alive and HTTP connection pooling are 100% automatic", they actually only work if you use a `Session` (that seems illogical to me, connection reuse and sessions are unrelated).

```
>>> import requests
>>> requests.get('https://www.mangopay.com/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.mangopay.com
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
>>> requests.get('https://www.mangopay.com/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.mangopay.com
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
>>> session = requests.Session()
>>> session.get('https://www.mangopay.com/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): www.mangopay.com
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
>>> session.get('https://www.mangopay.com/')
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 None
<Response [200]>
```

Second, the mangopay library [creates a new Session for each request](https://github.com/Mangopay/mangopay2-python-sdk/blob/v2/mangopaysdk/tools/resttool.py#L110).

I've reported the first problem upstream: https://github.com/kennethreitz/requests/issues/3445.

… and I was politely told to fuck off.

PR opened upstream for the second problem: https://github.com/Mangopay/mangopay2-python-sdk/pull/49.


Resolving patch:
diff --git a/liberapay/wireup.py b/liberapay/wireup.py
--- a/liberapay/wireup.py
+++ b/liberapay/wireup.py
@@ -177,6 +177,8 @@ def billing(app_conf):
     Configuration.ClientID = app_conf.mangopay_client_id
     Configuration.ClientPassword = app_conf.mangopay_client_password
     Configuration.SSLVerification = True
+    import requests
+    Configuration.Session = requests.Session()
 
 
 def username_restrictions(www_root):


Test patch: