From 80cc26bd9574af0e84cd869cf9cec34dbc4efa0e Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 9 Jul 2021 16:33:11 +0200 Subject: [PATCH] fix max-client limit --- copyparty/httpsrv.py | 23 +++++++++++++---------- copyparty/tcpsrv.py | 3 +-- copyparty/util.py | 14 -------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/copyparty/httpsrv.py b/copyparty/httpsrv.py index 97b51bc6..a4255ee1 100644 --- a/copyparty/httpsrv.py +++ b/copyparty/httpsrv.py @@ -58,6 +58,7 @@ class HttpSrv(object): self.tp_q = None if self.args.no_htp else queue.LifoQueue() self.srvs = [] + self.ncli = 0 self.clients = {} self.cb_ts = 0 self.cb_v = 0 @@ -107,7 +108,7 @@ class HttpSrv(object): while True: time.sleep(2 if self.tp_ncli else 30) with self.mutex: - self.tp_ncli = max(len(self.clients), self.tp_ncli - 2) + self.tp_ncli = max(self.ncli, self.tp_ncli - 2) if self.tp_nthr > self.tp_ncli + 8: self.stop_threads(4) @@ -127,9 +128,10 @@ class HttpSrv(object): if self.args.log_conn: self.log(self.name, "|%sC-ncli" % ("-" * 1,), c="1;30") - if len(self.clients) >= self.args.nc: - time.sleep(0.1) - continue + if self.ncli >= self.args.nc: + self.log(self.name, "at connection limit; waiting", 3) + while self.ncli >= self.args.nc: + time.sleep(0.1) if self.args.log_conn: self.log(self.name, "|%sC-acc1" % ("-" * 2,), c="1;30") @@ -159,9 +161,10 @@ class HttpSrv(object): if self.tp_q: self.tp_q.put((sck, addr)) with self.mutex: + self.ncli += 1 self.tp_time = self.tp_time or now - self.tp_ncli = max(self.tp_ncli, len(self.clients) + 1) - if self.tp_nthr < len(self.clients) + 4: + self.tp_ncli = max(self.tp_ncli, self.ncli + 1) + if self.tp_nthr < self.ncli + 4: self.start_threads(8) return @@ -169,6 +172,9 @@ class HttpSrv(object): m = "looks like the httpserver threadpool died; please make an issue on github and tell me the story of how you pulled that off, thanks and dog bless\n" self.log(self.name, m, 1) + with self.mutex: + self.ncli += 1 + thr = threading.Thread( target=self.thr_client, args=(sck, addr), @@ -197,10 +203,6 @@ class HttpSrv(object): except: self.log(self.name, "thr_client: " + min_ex(), 3) - def num_clients(self): - with self.mutex: - return len(self.clients) - def shutdown(self): self.stopping = True for srv in self.srvs: @@ -275,6 +277,7 @@ class HttpSrv(object): finally: with self.mutex: del self.clients[cli] + self.ncli -= 1 def cachebuster(self): if time.time() - self.cb_ts < 1: diff --git a/copyparty/tcpsrv.py b/copyparty/tcpsrv.py index c3c3bbca..ff446ce2 100644 --- a/copyparty/tcpsrv.py +++ b/copyparty/tcpsrv.py @@ -4,7 +4,7 @@ from __future__ import print_function, unicode_literals import re import socket -from .util import chkcmd, Counter +from .util import chkcmd class TcpSrv(object): @@ -18,7 +18,6 @@ class TcpSrv(object): self.args = hub.args self.log = hub.log - self.num_clients = Counter() self.stopping = False ip = "127.0.0.1" diff --git a/copyparty/util.py b/copyparty/util.py index 86eaf596..8b6b2b7c 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -137,20 +137,6 @@ REKOBO_KEY = { REKOBO_LKEY = {k.lower(): v for k, v in REKOBO_KEY.items()} -class Counter(object): - def __init__(self, v=0): - self.v = v - self.mutex = threading.Lock() - - def add(self, delta=1): - with self.mutex: - self.v += delta - - def set(self, absval): - with self.mutex: - self.v = absval - - class Cooldown(object): def __init__(self, maxage): self.maxage = maxage