<s> from . import logger <EOL> from . exceptions import NoServerAvailable <EOL> from . fakettypes import Method , RestResponse <EOL> from time import time <EOL> try : <EOL> from urllib import urlencode <EOL> from urlparse import urlparse <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> from urllib . parse import urlparse <EOL> import random <EOL> import threading <EOL> import urllib3 <EOL> import heapq <EOL> from multiprocessing import current_process <EOL> __all__ = [ "<STR_LIT>" ] <EOL> DEFAULT_SERVER = ( "<STR_LIT:http>" , "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) <EOL> POOLS = { } <EOL> CERT_REQS = '<STR_LIT>' <EOL> def get_pool ( ) : <EOL> if not current_process ( ) in POOLS : <EOL> POOLS [ current_process ( ) ] = urllib3 . PoolManager ( cert_reqs = CERT_REQS ) <EOL> return POOLS [ current_process ( ) ] <EOL> def update_connection_pool ( maxsize = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> get_pool ( ) . connection_pool_kw . update ( maxsize = maxsize ) <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , servers = None , retry_time = <NUM_LIT> , max_retries = <NUM_LIT:3> , timeout = None , <EOL> basic_auth = None ) : <EOL> if servers is None : <EOL> servers = [ DEFAULT_SERVER ] <EOL> self . _active_servers = [ server . geturl ( ) for server in servers ] <EOL> self . _inactive_servers = [ ] <EOL> self . _retry_time = retry_time <EOL> self . _max_retries = max_retries <EOL> self . _timeout = timeout <EOL> if basic_auth : <EOL> self . _headers = urllib3 . make_headers ( basic_auth = "<STR_LIT>" % basic_auth ) <EOL> else : <EOL> self . _headers = { } <EOL> self . _lock = threading . RLock ( ) <EOL> self . _local = threading . local ( ) <EOL> def execute ( self , request ) : <EOL> """<STR_LIT>""" <EOL> url = request . uri <EOL> if request . parameters : <EOL> url += '<STR_LIT:?>' + urlencode ( request . parameters ) <EOL> if request . headers : <EOL> headers = dict ( self . _headers , ** request . headers ) <EOL> else : <EOL> headers = self . _headers <EOL> kwargs = dict ( <EOL> method = Method . _VALUES_TO_NAMES [ request . method ] , <EOL> url = url , <EOL> body = request . body , <EOL> headers = headers , <EOL> timeout = self . _timeout , <EOL> ) <EOL> retry = <NUM_LIT:0> <EOL> server = getattr ( self . _local , "<STR_LIT>" , None ) <EOL> while True : <EOL> if not server : <EOL> self . _local . server = server = self . _get_server ( ) <EOL> try : <EOL> parse_result = urlparse ( server ) <EOL> conn = get_pool ( ) . connection_from_host ( parse_result . hostname , <EOL> parse_result . port , <EOL> parse_result . scheme ) <EOL> response = conn . urlopen ( ** kwargs ) <EOL> return RestResponse ( status = response . status , <EOL> body = response . data , <EOL> headers = response . headers ) <EOL> except ( IOError , urllib3 . exceptions . HTTPError ) as ex : <EOL> self . _drop_server ( server ) <EOL> self . _local . server = server = None <EOL> if retry >= self . _max_retries : <EOL> logger . error ( "<STR_LIT>" , <EOL> self . _max_retries , exc_info = <NUM_LIT:1> ) <EOL> raise NoServerAvailable ( ex ) <EOL> logger . exception ( "<STR_LIT>" , self . _max_retries - retry ) <EOL> retry += <NUM_LIT:1> <EOL> def _get_server ( self ) : <EOL> with self . _lock : <EOL> try : <EOL> ts , server = heapq . heappop ( self . _inactive_servers ) <EOL> except IndexError : <EOL> pass <EOL> else : <EOL> if ts > time ( ) : <EOL> heapq . heappush ( self . _inactive_servers , ( ts , server ) ) <EOL> else : <EOL> self . _active_servers . append ( server ) <EOL> logger . info ( "<STR_LIT>" , server ) <EOL> try : <EOL> return random . choice ( self . _active_servers ) <EOL> except IndexError as ex : <EOL> raise NoServerAvailable ( ex ) <EOL> def _drop_server ( self , server ) : <EOL> with self . _lock : <EOL> try : <EOL> self . _active_servers . remove ( server ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> heapq . heappush ( self . _inactive_servers , ( time ( ) + self . _retry_time , server ) ) <EOL> logger . warning ( "<STR_LIT>" , server ) <EOL> connect = Connection </s>
