@@ -961,7 +961,7 @@ def _getaddrinfo(*args, **kwargs):
961961
962962def _setSocketPreConnect ():
963963 """
964- Makes a pre-connect version of socket.connect
964+ Makes a pre-connect version of socket.create_connection
965965 """
966966
967967 if conf .disablePrecon :
@@ -972,36 +972,28 @@ def _thread():
972972 try :
973973 for key in socket ._ready :
974974 if len (socket ._ready [key ]) < SOCKET_PRE_CONNECT_QUEUE_SIZE :
975- family , type , proto , address = key
976- s = socket .socket (family , type , proto )
977- s ._connect (address )
978- try :
979- if type == socket .SOCK_STREAM :
980- # Reference: https://www.techrepublic.com/article/tcp-ip-options-for-high-performance-data-transmission/
981- s .setsockopt (socket .IPPROTO_TCP , socket .TCP_NODELAY , 1 )
982- except :
983- pass
975+ s = socket .create_connection (* key [0 ], ** dict (key [1 ]))
984976 with kb .locks .socket :
985- socket ._ready [key ].append ((s . _sock , time .time ()))
977+ socket ._ready [key ].append ((s , time .time ()))
986978 except KeyboardInterrupt :
987979 break
988980 except :
989981 pass
990982 finally :
991983 time .sleep (0.01 )
992984
993- def connect ( self , address ):
994- found = False
985+ def create_connection ( * args , ** kwargs ):
986+ retVal = None
995987
996- key = (self . family , self . type , self . proto , address )
988+ key = (tuple ( args ), frozenset ( kwargs . items ()) )
997989 with kb .locks .socket :
998990 if key not in socket ._ready :
999991 socket ._ready [key ] = []
992+
1000993 while len (socket ._ready [key ]) > 0 :
1001994 candidate , created = socket ._ready [key ].pop (0 )
1002995 if (time .time () - created ) < PRECONNECT_CANDIDATE_TIMEOUT :
1003- self ._sock = candidate
1004- found = True
996+ retVal = candidate
1005997 break
1006998 else :
1007999 try :
@@ -1010,13 +1002,15 @@ def connect(self, address):
10101002 except socket .error :
10111003 pass
10121004
1013- if not found :
1014- self ._connect (address )
1005+ if not retVal :
1006+ retVal = socket ._create_connection (* args , ** kwargs )
1007+
1008+ return retVal
10151009
1016- if not hasattr (socket .socket , "_connect " ):
1010+ if not hasattr (socket .socket , "_create_connection " ):
10171011 socket ._ready = {}
1018- socket .socket . _connect = socket .socket . connect
1019- socket .socket . connect = connect
1012+ socket ._create_connection = socket .create_connection
1013+ socket .create_connection = create_connection
10201014
10211015 thread = threading .Thread (target = _thread )
10221016 setDaemon (thread )
0 commit comments