graphile_worker/builder/local_queue.rs
1use crate::local_queue::LocalQueueConfig;
2
3use super::WorkerOptions;
4
5impl WorkerOptions {
6 /// Enables the LocalQueue with the specified configuration.
7 ///
8 /// LocalQueue batch-fetches jobs from the database to reduce DB load,
9 /// trading latency for throughput. Jobs are cached locally and distributed
10 /// to workers without additional database queries until the cache is empty.
11 ///
12 /// # Arguments
13 /// * `config` - The LocalQueue configuration (size and TTL)
14 ///
15 /// # Note
16 /// When LocalQueue is enabled, jobs may experience slightly higher latency
17 /// as they wait in the local cache. The cache has a TTL after which
18 /// unclaimed jobs are returned to the database.
19 ///
20 /// `LocalQueueConfig::queue_count` can be raised above 1 to run multiple
21 /// independent local queues in the same worker. `size` is applied per queue,
22 /// so total local capacity is `size * queue_count`.
23 /// There is no universal best setting for throughput; benchmark realistic
24 /// jobs with your own PostgreSQL latency, pool size, worker concurrency, and
25 /// local queue settings before tuning this in production.
26 ///
27 /// Workers with `forbidden_flags` will bypass the LocalQueue and fetch
28 /// jobs directly from the database.
29 ///
30 /// # Example
31 /// ```
32 /// # use graphile_worker::{WorkerOptions, LocalQueueConfig, RefetchDelayConfig};
33 /// # use std::time::Duration;
34 ///
35 /// let options = WorkerOptions::default()
36 /// .local_queue(
37 /// LocalQueueConfig::default()
38 /// .with_size(100)
39 /// .with_queue_count(2)
40 /// .with_ttl(Duration::from_secs(300))
41 /// .with_refetch_delay(
42 /// RefetchDelayConfig::default()
43 /// .with_duration(Duration::from_millis(100))
44 /// .with_threshold(10)
45 /// .with_max_abort_threshold(500),
46 /// ),
47 /// );
48 /// ```
49 pub fn local_queue(mut self, config: LocalQueueConfig) -> Self {
50 self.local_queue_config = Some(config);
51 self
52 }
53}