def connect_thread(self, sleep_time=0): time.sleep(sleep_time) try: while self.new_conn_pool.qsize() < self.https_new_connect_num: ip_str = google_ip.get_gws_ip() if not ip_str: time.sleep(60) xlog.warning("no enough ip") break port = 443 #xlog.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, port) ) if ssl_sock: ssl_sock.last_use_time = time.time() if self.new_conn_pool.qsize() >= self.connection_pool_max_num and self.ssl_timeout_cb: self.ssl_timeout_cb(ssl_sock) else: self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) elif not connect_control.allow_connect(): break time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(self, sleep_time=0): time.sleep(sleep_time) try: while self.new_conn_pool.qsize() < self.https_new_connect_num or \ self.new_conn_pool.qsize(only_h1=True) < 1: if self.new_conn_pool.qsize() > self.connection_pool_max_num: break if not connect_control.allow_connect(): break ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no enough ip") time.sleep(60) break ssl_sock = self._create_ssl_connection((ip_str, 443)) if not ssl_sock: continue if self.ssl_timeout_cb and \ self.new_conn_pool.qsize() > self.connection_pool_max_num + 1 and \ self.new_conn_pool.qsize() > self.https_new_connect_num + 1 and \ self.new_conn_pool.qsize(only_h1=True) > 2: self.ssl_timeout_cb(ssl_sock) else: self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def create_connection_worker(self, type="gae"): try: while connect_control.keep_running: if type == "gae": if (self.new_conn_pool.qsize() + self.gae_conn_pool.qsize()) >= self.connection_pool_min_num: break else: if self.new_conn_pool.qsize() >= self.connection_pool_min_num: break ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no gws ip") break port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, port) ) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) elif not connect_control.allow_connect(): break time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(self, sleep_time=0): time.sleep(sleep_time) try: while self.new_conn_pool.qsize() < self.connection_pool_min_num: if self.new_conn_pool.qsize() >= self.connection_pool_min_num: #xlog.debug("get enough conn") break ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("ip not enough") break port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection((ip_str, port)) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) elif not connect_control.allow_connect(): break time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(self, sleep_time=0): time.sleep(sleep_time) try: while self.new_conn_pool.qsize() < self.connection_pool_min_num: if self.new_conn_pool.qsize() >= self.connection_pool_min_num: #xlog.debug("get enough conn") break ip_str = google_ip.get_gws_ip() if not ip_str: time.sleep(60) xlog.warning("no enough ip") break port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, port) ) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) elif not connect_control.allow_connect(): break time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def create_connection_worker(self, type="gae"): try: while connect_control.keep_running: if type == "gae": if (self.new_conn_pool.qsize() + self.gae_conn_pool.qsize() ) >= self.connection_pool_min_num: break else: if self.new_conn_pool.qsize( ) >= self.connection_pool_min_num: break ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no gws ip") break port = 443 #logging.debug("create ssl conn %s", ip_str) connect_control.start_connect_register(True) ssl_sock = self._create_ssl_connection((ip_str, port)) connect_control.end_connect_register(True) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) elif not connect_control.allow_connect(): break time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(self, sleep_time=0): time.sleep(sleep_time) try: while self.new_conn_pool.qsize() < self.https_new_connect_num or \ self.new_conn_pool.qsize(only_h1=True) < 1: if self.new_conn_pool.qsize() > self.connection_pool_max_num: break if not connect_control.allow_connect(): break ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no enough ip") time.sleep(60) break ssl_sock = self._create_ssl_connection( (ip_str, 443) ) if not ssl_sock: continue if self.ssl_timeout_cb and \ self.new_conn_pool.qsize() > self.connection_pool_max_num + 1 and \ self.new_conn_pool.qsize() > self.https_new_connect_num + 1 and \ self.new_conn_pool.qsize(only_h1=True) > 2: self.ssl_timeout_cb(ssl_sock) else: self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def create_connection_worker(self): try: while connect_control.keep_running: if self.new_conn_pool.qsize() >= self.connection_pool_min_num: #xlog.debug("get enough conn") break if self.thread_num > self.connection_pool_min_num - self.new_conn_pool.qsize(): break ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no gws ip") break port = 443 #logging.debug("create ssl conn %s", ip_str) connect_control.start_connect_register(True) ssl_sock = self._create_ssl_connection( (ip_str, port) ) connect_control.end_connect_register(True) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) elif not connect_control.allow_connect(): xlog.debug("create_connection_worker, control not allow") time.sleep(10) time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_process(self): try: ip_str = google_ip.get_gws_ip() if not ip_str: time.sleep(60) xlog.warning("no enough ip") return #xlog.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, 443) ) if not ssl_sock: return self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) except: pass
def connect_process(self): try: ip_str = google_ip.get_gws_ip() if not ip_str: time.sleep(60) xlog.warning("no enough ip") return #xlog.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection((ip_str, 443)) if not ssl_sock: return self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) except: pass
def connect_process(self): try: ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no gws ip") return port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, port) ) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(): try: while self.conn_pool.qsize() < self.min_connection_num: ip_str = google_ip.get_gws_ip() if not ip_str: logging.warning("no gws ip") break port = 443 logging.debug("create ssl conn %s", ip_str) ssl_sock = _create_ssl_connection((ip_str, port)) if ssl_sock: self.conn_pool.put((time.time(), ssl_sock)) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_process(self): try: ip_str = google_ip.get_gws_ip() if not ip_str: xlog.warning("no gws ip") return port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection((ip_str, port)) if ssl_sock: ssl_sock.last_use_time = time.time() self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(self): try: while self.conn_pool.qsize() < self.connection_pool_min_num: ip_str = google_ip.get_gws_ip() if not ip_str: logging.warning("no gws ip") break port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, port) ) if ssl_sock: ssl_sock.last_use_time = time.time() self.conn_pool.put((ssl_sock.handshake_time, ssl_sock)) time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_thread(self): try: while self.conn_pool.qsize() < self.connection_pool_min_num: ip_str = google_ip.get_gws_ip() if not ip_str: logging.warning("no gws ip") break port = 443 #logging.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection((ip_str, port)) if ssl_sock: ssl_sock.last_use_time = time.time() self.conn_pool.put((ssl_sock.handshake_time, ssl_sock)) time.sleep(1) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def connect_process(self): try: ip_str = google_ip.get_gws_ip() if not ip_str: time.sleep(60) xlog.warning("no enough ip") return port = 443 #xlog.debug("create ssl conn %s", ip_str) ssl_sock = self._create_ssl_connection( (ip_str, port) ) if ssl_sock: ssl_sock.last_use_time = time.time() if self.new_conn_pool.qsize() >= self.connection_pool_max_num and self.ssl_timeout_cb: self.ssl_timeout_cb(ssl_sock) else: self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock)) finally: self.thread_num_lock.acquire() self.thread_num -= 1 self.thread_num_lock.release()
def create_connection(self, sock_life=5, cache_key=None): connection_cache_key = cache_key def _create_connection(ip_port, timeout, queobj): ip = ip_port[0] sock = None try: # create a ipv4/ipv6 socket object 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(timeout or self.max_timeout) # start connection time record start_time = time.time() # TCP connect sock.connect(ip_port) # record TCP connection time conn_time = time.time() - start_time google_ip.update_ip(ip, conn_time * 2000) # put ssl socket object to output queobj queobj.put(sock) except (socket.error, OSError) as e: # any socket.error, put Excpetions to output queobj. queobj.put(e) google_ip.report_connect_fail(ip) if sock: sock.close() def recycle_connection(count, queobj): for i in range(count): sock = queobj.get() if sock and not isinstance(sock, Exception): if connection_cache_key: self.tcp_connection_cache[connection_cache_key].put( (time.time(), sock)) else: sock.close() if connection_cache_key: try: ctime, sock = self.tcp_connection_cache[ connection_cache_key].get_nowait() if time.time() - ctime < 5: return sock except Queue.Empty: pass port = 443 timeout = 2 addresses = [] for i in range(5): addresses.append((google_ip.get_gws_ip(), port)) addrs = addresses queobj = Queue.Queue() for addr in addrs: thread.start_new_thread(_create_connection, (addr, timeout, queobj)) for i in range(len(addrs)): result = queobj.get() if not isinstance(result, (socket.error, OSError)): thread.start_new_thread(recycle_connection, (len(addrs) - i - 1, queobj)) return result else: if i == 0: # only output first error logging.warning( 'create_connection to %s return %r, try again.', addrs, result)
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.')
def create_connection(self, sock_life=5): def _create_connection(ip_port, queobj, delay=0): if delay != 0: time.sleep(delay) ip = ip_port[0] sock = None try: # create a ipv4/ipv6 socket object 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.max_timeout) # start connection time record start_time = time.time() conn_time = 0 # TCP connect sock.connect(ip_port) # record TCP connection time conn_time = time.time() - start_time google_ip.update_ip(ip, conn_time * 2000) #logging.info("create_tcp update ip:%s time:%d", ip, conn_time * 2000) logging.debug("tcp conn %s time:%d", ip, conn_time * 1000) # put ssl socket object to output queobj queobj.put(sock) except (socket.error, OSError) as e: # any socket.error, put Excpetions to output queobj. queobj.put(e) conn_time = int((time.time() - start_time) * 1000) logging.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() def recycle_connection(count, queobj): for i in range(count): sock = queobj.get() if sock and not isinstance(sock, Exception): self.tcp_connection_cache.put((time.time(), sock)) try: ctime, sock = self.tcp_connection_cache.get_nowait() if time.time() - ctime < sock_life: return sock except Queue.Empty: pass port = 443 for j in range(self.max_retry): addresses = [] for i in range(3): ip = google_ip.get_gws_ip() if not ip: logging.warning("no gws ip.") return addresses.append((ip, port)) addrs = addresses queobj = Queue.Queue() delay = 0 for addr in addrs: thread.start_new_thread(_create_connection, (addr, queobj, delay)) #delay += 0.05 for i in range(len(addrs)): result = queobj.get() if not isinstance(result, (socket.error, OSError)): thread.start_new_thread(recycle_connection, (len(addrs)-i-1, queobj)) return result logging.warning('create_connection fail.')