better handling for server-side socket closing in connection pool

This commit is contained in:
Alexander Bersenev
2026-02-17 21:14:42 +05:00
parent 34f743858c
commit 8920faf650

View File

@@ -483,7 +483,7 @@ myrandom = MyRandom()
class TgConnectionPool: class TgConnectionPool:
MAX_CONNS_IN_POOL = 64 MAX_CONNS_IN_POOL = 16
def __init__(self): def __init__(self):
self.pools = {} self.pools = {}
@@ -500,6 +500,16 @@ class TgConnectionPool:
timeout=config.TG_CONNECT_TIMEOUT) timeout=config.TG_CONNECT_TIMEOUT)
return reader_tgt, writer_tgt return reader_tgt, writer_tgt
def is_conn_dead(self, reader, writer):
if writer.transport.is_closing():
return True
raw_reader = reader
while hasattr(raw_reader, 'upstream'):
raw_reader = raw_reader.upstream
if raw_reader.at_eof():
return True
return False
def register_host_port(self, host, port, init_func): def register_host_port(self, host, port, init_func):
if (host, port, init_func) not in self.pools: if (host, port, init_func) not in self.pools:
self.pools[(host, port, init_func)] = [] self.pools[(host, port, init_func)] = []
@@ -519,8 +529,9 @@ class TgConnectionPool:
continue continue
reader, writer, *other = task.result() reader, writer, *other = task.result()
if writer.transport.is_closing(): if self.is_conn_dead(reader, writer):
self.pools[(host, port, init_func)].remove(task) self.pools[(host, port, init_func)].remove(task)
writer.transport.abort()
continue continue
if not ret: if not ret: