Skip to content

Commit

Permalink
5.7.7 avoid too many threads on creating connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-X-Net committed Sep 27, 2023
1 parent 8c004b3 commit 5f62f2b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
21 changes: 21 additions & 0 deletions code/default/launcher/web_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
module_menus = {}


class FakeHttpHandler(simple_http_server.HttpServerHandler):
def handle_one_request(self):
# This function will replace simple_http_server HttpHandler.handle_one_request to hold all http requests.
# Doing this is to simulate bug.
self.close_connection = 0


class Http_Handler(simple_http_server.HttpServerHandler):
deploy_proc = None

Expand Down Expand Up @@ -244,6 +251,10 @@ def do_GET(self):
return self.req_log_handler()
elif url_path == "/keep_log":
return self.req_keep_log_handler()
elif url_path == "/suck_threads":
return self.req_suck_threads()
elif url_path == "/hold_8085":
return self.req_hold_8085()
elif url_path == '/update':
self.req_update_handler()
elif url_path == '/config_proxy':
Expand Down Expand Up @@ -797,6 +808,16 @@ def req_keep_log_handler(self):
mimetype = 'text/plain'
self.send_response(mimetype, data)

def req_suck_threads(self):
self.send_response('text/plain', "Start suck threads")
while True:
threading.Thread(target=time.sleep, args=(1000,)).start()

def req_hold_8085(self):
global server
self.send_response('text/plain', "Hold 8085")
server.handler = FakeHttpHandler

def req_log_handler(self):
req = urlparse(self.path).query
reqs = self.unpack_reqs(parse_qs(req, keep_blank_values=True))
Expand Down
4 changes: 3 additions & 1 deletion code/default/lib/noarch/front_base/connect_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def __init__(self, logger, config, connect_creator, ip_manager, check_local_netw
self._waiting_num_lock = threading.Lock()
self._connection_waiting_num = 0
self.no_ip_lock = threading.Lock()
self.no_ip_time = 0

# after new created ssl_sock timeout(50 seconds)
# call the callback.
Expand Down Expand Up @@ -259,7 +260,7 @@ def _connect_thread(self, sleep_time=0):
time.sleep(sleep_time)

try:
while self.running and self._need_more_ip():
while self.running and self._need_more_ip() and time.time() - self.no_ip_time > 10:
if self.new_conn_pool.qsize() > self.config.https_connection_pool_max:
break

Expand All @@ -275,6 +276,7 @@ def _connect_process(self):
try:
ip_str, sni, host = self.ip_manager.get_ip_sni_host()
if not ip_str:
self.no_ip_time = time.time()
with self.no_ip_lock:
# self.logger.warning("not enough ip")
time.sleep(10)
Expand Down
2 changes: 1 addition & 1 deletion code/default/lib/noarch/simple_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def check_listen_port_or_reset(self):
if res:
return

self.logger.warn("Listen %s:%d check failed", ip, port)
self.logger.warn("Listen %s:%d check failed, try restart listening", ip, port)
self.shutdown()
time.sleep(3)
self.start()
Expand Down
2 changes: 1 addition & 1 deletion code/default/smart_router/local/smart_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def do_socks(sock, host, port, client_address, left_buf=b""):
raise XTunnelNotRunning()

try:
conn_id = g.x_tunnel.proxy_session.create_conn(sock, host, port)
conn_id = g.x_tunnel.proxy_session.create_conn(sock, host, port, True)
except Exception as e:
xlog.warn("do_sock to %s:%d, x_tunnel fail:%r", host, port, e)
raise XTunnelNotRunning()
Expand Down
2 changes: 1 addition & 1 deletion code/default/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.7.6
5.7.7
8 changes: 5 additions & 3 deletions code/default/x_tunnel/local/proxy_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def login_session(self):

return False

def create_conn(self, sock, host, port):
def create_conn(self, sock, host, port, log=False):
if not self.running:
xlog.debug("session not running, try to connect")
time.sleep(1)
Expand All @@ -457,6 +457,8 @@ def create_conn(self, sock, host, port):
min(g.config.concurent_thread_num - g.config.min_on_road, self.target_on_roads + 5)
self.trigger_more()

if log:
xlog.info("Connect to %s:%d conn:%d", host, port, conn_id)
return conn_id

# Called by stop
Expand Down Expand Up @@ -1143,7 +1145,7 @@ def login_process():
return True


def create_conn(sock, host, port):
def create_conn(sock, host, port, log=False):
if not (g.config.login_account and g.config.login_password):
time.sleep(1)
return False
Expand All @@ -1154,7 +1156,7 @@ def create_conn(sock, host, port):
else:
time.sleep(1)

return g.session.create_conn(sock, host, port)
return g.session.create_conn(sock, host, port, log)


def update_quota_loop():
Expand Down

0 comments on commit 5f62f2b

Please sign in to comment.