refactor: Use only 2 pool variables.

This commit is contained in:
Ankush Menat 2026-05-28 15:54:33 +05:30
parent 48260712ea
commit 2471050b3a

View File

@ -77,9 +77,8 @@ class ThreadWorker(base.Worker):
self.worker_connections = self.cfg.worker_connections self.worker_connections = self.cfg.worker_connections
self.max_keepalived = self.cfg.worker_connections - self.cfg.threads self.max_keepalived = self.cfg.worker_connections - self.cfg.threads
# initialise the pool(s): a single pool when routing is disabled, or a # initialise the pool(s): a single pool when routing is disabled, or a
# separate fast and slow pool when it is enabled # separate fast (``self.tpool``) and slow pool when it is enabled
self.tpool = None self.tpool = None
self.fast_pool = None
self.slow_pool = None self.slow_pool = None
# number of slow requests submitted but not yet finished, used to bound # number of slow requests submitted but not yet finished, used to bound
# the slow lane (running + queued) and shed load with 503 # the slow lane (running + queued) and shed load with 503
@ -106,19 +105,16 @@ class ThreadWorker(base.Worker):
"Check the number of worker connections and threads.") "Check the number of worker connections and threads.")
def init_process(self): def init_process(self):
self.tpool = self.get_thread_pool()
if self.routing_enabled: if self.routing_enabled:
self.predictor = SlowRoutePredictor( self.predictor = SlowRoutePredictor(
self.slow_threshold, self.slow_threshold,
seed_patterns=self.cfg.slow_routes, seed_patterns=self.cfg.slow_routes,
) )
# a dedicated pool per lane: slow requests can never occupy the # a dedicated pool for the slow lane: slow requests can never
# fast pool's threads # occupy the fast pool's (``self.tpool``) threads
self.fast_pool = futures.ThreadPoolExecutor(
max_workers=self.cfg.threads)
self.slow_pool = futures.ThreadPoolExecutor( self.slow_pool = futures.ThreadPoolExecutor(
max_workers=self.cfg.slow_threads) max_workers=self.cfg.slow_threads)
else:
self.tpool = self.get_thread_pool()
self.poller = selectors.DefaultSelector() self.poller = selectors.DefaultSelector()
self._lock = RLock() self._lock = RLock()
super().init_process() super().init_process()
@ -128,7 +124,7 @@ class ThreadWorker(base.Worker):
return futures.ThreadPoolExecutor(max_workers=self.cfg.threads) return futures.ThreadPoolExecutor(max_workers=self.cfg.threads)
def _shutdown_pools(self, wait): def _shutdown_pools(self, wait):
for pool in (self.tpool, self.fast_pool, self.slow_pool): for pool in (self.tpool, self.slow_pool):
if pool is not None: if pool is not None:
pool.shutdown(wait) pool.shutdown(wait)
@ -156,9 +152,7 @@ class ThreadWorker(base.Worker):
def enqueue_req(self, conn, slow=False): def enqueue_req(self, conn, slow=False):
conn.init() conn.init()
# submit the connection to the appropriate pool # submit the connection to the appropriate pool
if not self.routing_enabled: if self.routing_enabled and slow:
fs = self.tpool.submit(self.handle, conn)
elif slow:
cap = self.cfg.slow_queue_maxsize cap = self.cfg.slow_queue_maxsize
if cap and self.nr_slow >= self.cfg.slow_threads + cap: if cap and self.nr_slow >= self.cfg.slow_threads + cap:
# slow lane (running + queued) is full; shed load with 503 # slow lane (running + queued) is full; shed load with 503
@ -168,7 +162,7 @@ class ThreadWorker(base.Worker):
self.nr_slow += 1 self.nr_slow += 1
fs = self.slow_pool.submit(self.handle, conn) fs = self.slow_pool.submit(self.handle, conn)
else: else:
fs = self.fast_pool.submit(self.handle, conn) fs = self.tpool.submit(self.handle, conn)
self._wrap_future(fs, conn, slow=slow) self._wrap_future(fs, conn, slow=slow)
def accept(self, server, listener): def accept(self, server, listener):