Makes a pre-connect version of socket.create_connection
()
| 1046 | socket.getaddrinfo = _getaddrinfo |
| 1047 | |
| 1048 | def _setSocketPreConnect(): |
| 1049 | """ |
| 1050 | Makes a pre-connect version of socket.create_connection |
| 1051 | """ |
| 1052 | |
| 1053 | if conf.disablePrecon: |
| 1054 | return |
| 1055 | |
| 1056 | def _thread(): |
| 1057 | while kb.get("threadContinue") and not conf.get("disablePrecon"): |
| 1058 | try: |
| 1059 | for key in socket._ready: |
| 1060 | if len(socket._ready[key]) < SOCKET_PRE_CONNECT_QUEUE_SIZE: |
| 1061 | s = socket.create_connection(*key[0], **dict(key[1])) |
| 1062 | with kb.locks.socket: |
| 1063 | socket._ready[key].append((s, time.time())) |
| 1064 | except KeyboardInterrupt: |
| 1065 | break |
| 1066 | except: |
| 1067 | pass |
| 1068 | finally: |
| 1069 | time.sleep(0.01) |
| 1070 | |
| 1071 | def create_connection(*args, **kwargs): |
| 1072 | retVal = None |
| 1073 | |
| 1074 | key = (tuple(args), frozenset(kwargs.items())) |
| 1075 | with kb.locks.socket: |
| 1076 | if key not in socket._ready: |
| 1077 | socket._ready[key] = [] |
| 1078 | |
| 1079 | while len(socket._ready[key]) > 0: |
| 1080 | candidate, created = socket._ready[key].pop(0) |
| 1081 | if (time.time() - created) < PRECONNECT_CANDIDATE_TIMEOUT: |
| 1082 | retVal = candidate |
| 1083 | break |
| 1084 | else: |
| 1085 | try: |
| 1086 | candidate.shutdown(socket.SHUT_RDWR) |
| 1087 | candidate.close() |
| 1088 | except socket.error: |
| 1089 | pass |
| 1090 | |
| 1091 | if not retVal: |
| 1092 | retVal = socket._create_connection(*args, **kwargs) |
| 1093 | |
| 1094 | return retVal |
| 1095 | |
| 1096 | if not hasattr(socket, "_create_connection"): |
| 1097 | socket._ready = {} |
| 1098 | socket._create_connection = socket.create_connection |
| 1099 | socket.create_connection = create_connection |
| 1100 | |
| 1101 | thread = threading.Thread(target=_thread) |
| 1102 | setDaemon(thread) |
| 1103 | thread.start() |
| 1104 | |
| 1105 | def _setHTTPHandlers(): |