reifydb_sub_worker/
builder.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4//! Builder pattern for configuring the worker pool subsystem
5
6use std::time::Duration;
7
8use crate::WorkerConfig;
9
10/// Builder for configuring the worker pool subsystem
11pub struct WorkerBuilder {
12	num_workers: usize,
13	max_queue_size: usize,
14	scheduler_interval: Duration,
15	task_timeout_warning: Duration,
16}
17
18impl Default for WorkerBuilder {
19	fn default() -> Self {
20		Self::new()
21	}
22}
23
24impl WorkerBuilder {
25	/// Create a new WorkerPoolBuilder with default settings
26	pub fn new() -> Self {
27		Self {
28			num_workers: 1,
29			max_queue_size: 10000,
30			scheduler_interval: Duration::from_millis(10),
31			task_timeout_warning: Duration::from_secs(30),
32		}
33	}
34
35	/// Set the number of worker threads
36	///
37	/// Default: 1
38	pub fn num_workers(mut self, workers: usize) -> Self {
39		self.num_workers = workers.max(1);
40		self
41	}
42
43	/// Set the maximum number of queued tasks
44	///
45	/// Default: 10000
46	pub fn max_queue_size(mut self, size: usize) -> Self {
47		self.max_queue_size = size.max(1);
48		self
49	}
50
51	/// Set how often to check for periodic tasks
52	///
53	/// Default: 10ms
54	pub fn scheduler_interval(mut self, interval: Duration) -> Self {
55		self.scheduler_interval = interval;
56		self
57	}
58
59	/// Set the maximum time a task can run before warning
60	///
61	/// Default: 30 seconds
62	pub fn task_timeout_warning(mut self, timeout: Duration) -> Self {
63		self.task_timeout_warning = timeout;
64		self
65	}
66
67	/// Build the worker pool configuration
68	pub fn build(self) -> WorkerConfig {
69		WorkerConfig {
70			num_workers: self.num_workers,
71			max_queue_size: self.max_queue_size,
72			scheduler_interval: self.scheduler_interval,
73			task_timeout_warning: self.task_timeout_warning,
74		}
75	}
76}