88from __future__ import division
99
1010import codecs
11+ import collections
1112import functools
1213import glob
1314import inspect
@@ -1057,11 +1058,31 @@ def _setSocketPreConnect():
10571058 def _thread ():
10581059 while kb .get ("threadContinue" ) and not conf .get ("disablePrecon" ):
10591060 try :
1060- for key in socket ._ready :
1061- if len (socket ._ready [key ]) < SOCKET_PRE_CONNECT_QUEUE_SIZE :
1062- s = socket .create_connection (* key [0 ], ** dict (key [1 ]))
1063- with kb .locks .socket :
1064- socket ._ready [key ].append ((s , time .time ()))
1061+ with kb .locks .socket :
1062+ keys = list (socket ._ready .keys ())
1063+
1064+ for key in keys :
1065+ with kb .locks .socket :
1066+ q = socket ._ready .get (key )
1067+ if q is None or len (q ) >= SOCKET_PRE_CONNECT_QUEUE_SIZE :
1068+ continue
1069+ args = key [0 ]
1070+ kwargs = dict (key [1 ])
1071+
1072+ s = socket ._create_connection (* args , ** kwargs )
1073+
1074+ with kb .locks .socket :
1075+ q = socket ._ready .get (key )
1076+ if q is not None and len (q ) < SOCKET_PRE_CONNECT_QUEUE_SIZE :
1077+ q .append ((s , time .time ()))
1078+ s = None
1079+
1080+ if s is not None :
1081+ try :
1082+ s .close ()
1083+ except :
1084+ pass
1085+
10651086 except KeyboardInterrupt :
10661087 break
10671088 except :
@@ -1071,26 +1092,36 @@ def _thread():
10711092
10721093 def create_connection (* args , ** kwargs ):
10731094 retVal = None
1095+ stale = []
10741096
10751097 key = (tuple (args ), frozenset (kwargs .items ()))
10761098 with kb .locks .socket :
10771099 if key not in socket ._ready :
1078- socket ._ready [key ] = []
1100+ socket ._ready [key ] = collections . deque ()
10791101
1080- while len (socket ._ready [key ]) > 0 :
1081- candidate , created = socket ._ready [key ].pop (0 )
1102+ q = socket ._ready [key ]
1103+ while len (q ) > 0 :
1104+ candidate , created = q .popleft ()
10821105 if (time .time () - created ) < PRECONNECT_CANDIDATE_TIMEOUT :
10831106 retVal = candidate
10841107 break
10851108 else :
1086- try :
1087- candidate .shutdown (socket .SHUT_RDWR )
1088- candidate .close ()
1089- except socket .error :
1090- pass
1109+ stale .append (candidate )
1110+
1111+ for candidate in stale :
1112+ try :
1113+ candidate .shutdown (socket .SHUT_RDWR )
1114+ candidate .close ()
1115+ except :
1116+ pass
10911117
10921118 if not retVal :
10931119 retVal = socket ._create_connection (* args , ** kwargs )
1120+ else :
1121+ try :
1122+ retVal .settimeout (kwargs .get ("timeout" , socket .getdefaulttimeout ()))
1123+ except :
1124+ pass
10941125
10951126 return retVal
10961127
0 commit comments