def create_connection(self, host="", port=443, sock_life=5): if port != 443: xlog.warn("forward port %d not supported.", port) return None def _create_connection(ip_port, delay=0): time.sleep(delay) ip = ip_port[0] sock = None # start connection time record start_time = time.time() conn_time = 0 try: # create a ipv4/ipv6 socket object if config.PROXY_ENABLE: sock = socks.socksocket(socket.AF_INET if ':' not in ip else socket.AF_INET6) else: sock = socket.socket(socket.AF_INET if ':' not in ip else socket.AF_INET6) # set reuseaddr option to avoid 10048 socket error sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # resize socket recv buffer 8K->32K to improve browser releated application performance sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32 * 1024) # disable negal algorithm to send http request quickly. sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) # set a short timeout to trigger timeout retry more quickly. sock.settimeout(self.timeout) # TCP connect sock.connect(ip_port) # record TCP connection time conn_time = time.time() - start_time xlog.debug("tcp conn %s time:%d", ip, conn_time * 1000) google_ip.update_ip(ip, conn_time * 2000) #logging.info("create_tcp update ip:%s time:%d", ip, conn_time * 2000) # put ssl socket object to output queobj #sock.ip = ip self.tcp_connection_cache.put((time.time(), sock)) except Exception as e: conn_time = int((time.time() - start_time) * 1000) xlog.debug("tcp conn %s fail t:%d", ip, conn_time) google_ip.report_connect_fail(ip) #logging.info("create_tcp report fail ip:%s", ip) if sock: sock.close() finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release() if host != "appengine.google.com": while True: try: ctime, sock = self.tcp_connection_cache.get_nowait() if time.time() - ctime < sock_life: return sock else: sock.close() continue except Queue.Empty: break start_time = time.time() while time.time() - start_time < self.max_timeout: if self.thread_num < self.max_thread_num: if host == "appengine.google.com": ip = google_ip.get_host_ip("*.google.com") else: ip = google_ip.get_gws_ip() if not ip: xlog.error("no gws ip.") return addr = (ip, port) self.thread_num_lock.acquire() self.thread_num += 1 self.thread_num_lock.release() p = threading.Thread(target=_create_connection, args=(addr, )) p.daemon = True p.start() try: ctime, sock = self.tcp_connection_cache.get(timeout=0.2) return sock except: continue xlog.warning('create tcp connection fail.')
def create_connection(self, host="", port=443, sock_life=5): if port != 443: xlog.warn("forward port %d not supported.", port) return None def _create_connection(ip_port, delay=0): time.sleep(delay) ip = ip_port[0] sock = None # start connection time record start_time = time.time() conn_time = 0 try: # create a ipv4/ipv6 socket object if config.PROXY_ENABLE: sock = socks.socksocket(socket.AF_INET if ':' not in ip else socket.AF_INET6) else: sock = socket.socket(socket.AF_INET if ':' not in ip else socket.AF_INET6) # set reuseaddr option to avoid 10048 socket error sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # resize socket recv buffer 8K->32K to improve browser releated application performance sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32*1024) # disable negal algorithm to send http request quickly. sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) # set a short timeout to trigger timeout retry more quickly. sock.settimeout(self.timeout) # TCP connect sock.connect(ip_port) # record TCP connection time conn_time = time.time() - start_time xlog.debug("tcp conn %s time:%d", ip, conn_time * 1000) google_ip.update_ip(ip, conn_time * 2000) #logging.info("create_tcp update ip:%s time:%d", ip, conn_time * 2000) # put ssl socket object to output queobj #sock.ip = ip self.tcp_connection_cache.put((time.time(), sock)) except Exception as e: conn_time = int((time.time() - start_time) * 1000) xlog.debug("tcp conn %s fail t:%d", ip, conn_time) google_ip.report_connect_fail(ip) #logging.info("create_tcp report fail ip:%s", ip) if sock: sock.close() finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release() if host != "appengine.google.com": while True: try: ctime, sock = self.tcp_connection_cache.get_nowait() if time.time() - ctime < sock_life: return sock else: sock.close() continue except Queue.Empty: break start_time = time.time() while time.time() - start_time < self.max_timeout: if self.thread_num < self.max_thread_num: if host == "appengine.google.com": ip = google_ip.get_host_ip("*.google.com") else: ip = google_ip.get_gws_ip() if not ip: xlog.error("no gws ip.") return addr = (ip, port) self.thread_num_lock.acquire() self.thread_num += 1 self.thread_num_lock.release() p = threading.Thread(target=_create_connection, args=(addr,)) p.start() try: ctime, sock = self.tcp_connection_cache.get(timeout=0.2) return sock except: continue xlog.warning('create tcp connection fail.')