add configurable thread scaling and queue counter reset
- Add scale_cooldown and scale_threshold config options - ThreadScaler now reads scaling params from config - Reset priority queue counter when queue empties to prevent unbounded growth
This commit is contained in:
@@ -963,6 +963,9 @@ class PriorityJobQueue(object):
|
||||
def put(self, job, priority=3):
|
||||
"""Add job with priority (lower = higher priority)."""
|
||||
with self.lock:
|
||||
# Reset counter when queue was empty to prevent unbounded growth
|
||||
if not self.heap:
|
||||
self.counter = 0
|
||||
heapq.heappush(self.heap, (priority, self.counter, job))
|
||||
self.counter += 1
|
||||
self.not_empty.notify()
|
||||
@@ -1042,13 +1045,14 @@ class ThreadScaler(object):
|
||||
- Success rate drops below threshold
|
||||
"""
|
||||
|
||||
def __init__(self, min_threads=5, max_threads=100, target_queue_per_thread=5):
|
||||
def __init__(self, min_threads=5, max_threads=100, target_queue_per_thread=5,
|
||||
scale_cooldown=10, scale_threshold=10.0):
|
||||
self.min_threads = min_threads
|
||||
self.max_threads = max_threads
|
||||
self.target_queue_per_thread = target_queue_per_thread
|
||||
self.last_scale_time = 0
|
||||
self.scale_cooldown = 10 # seconds between scaling (faster with greenlets)
|
||||
self.success_threshold = 10.0 # minimum success rate % to scale up
|
||||
self.scale_cooldown = scale_cooldown
|
||||
self.success_threshold = scale_threshold
|
||||
|
||||
def should_scale(self, current_threads, queue_size, stats):
|
||||
"""Determine if scaling is needed.
|
||||
@@ -1771,8 +1775,10 @@ class Proxywatchd():
|
||||
min_t = config.watchd.min_threads if config.watchd.min_threads > 0 else max(5, config.watchd.threads // 4)
|
||||
self.scaler = ThreadScaler(
|
||||
min_threads=min_t,
|
||||
max_threads=config.watchd.threads, # respect configured thread limit
|
||||
target_queue_per_thread=5
|
||||
max_threads=config.watchd.threads,
|
||||
target_queue_per_thread=5,
|
||||
scale_cooldown=config.watchd.scale_cooldown,
|
||||
scale_threshold=config.watchd.scale_threshold
|
||||
)
|
||||
self.thread_id_counter = 0
|
||||
self.last_jobs_log = 0
|
||||
|
||||
Reference in New Issue
Block a user