threading: Add factory that creates curl session for each thread.

When creating a repo with an import url pointing to another repo on the same enterprise instance
we call the vcssserver to check the url. The vcsserver then calls to enterprise to verify the url.
This leads to two threads using the same cur session.
This commit is contained in:
Martin Bornhold 2016-06-22 10:18:06 +02:00
parent 24d554a54e
commit 4a1c60fba2

View file

@ -31,6 +31,7 @@ implementation.
import copy
import logging
import threading
import urllib2
import urlparse
import uuid
@ -38,7 +39,7 @@ import uuid
import msgpack
import requests
from . import exceptions
from . import exceptions, CurlSession
log = logging.getLogger(__name__)
@ -216,3 +217,17 @@ class VcsHttpProxy(object):
headers = iterator.next()
return iterator, status, headers
class ThreadlocalSessionFactory(object):
"""
Creates one CurlSession per thread on demand.
"""
def __init__(self):
self._thread_local = threading.local()
def __call__(self):
if not hasattr(self._thread_local, 'curl_session'):
self._thread_local.curl_session = CurlSession()
return self._thread_local.curl_session