Skip to main content

apalis_pgmq/
config.rs

1use std::{marker::PhantomData, time::Duration};
2
3use apalis_codec::json::JsonCodec;
4use apalis_core::backend::{
5    poll_strategy::{BackoffConfig, IntervalStrategy, MultiStrategy, StrategyBuilder},
6    queue::Queue,
7};
8
9/// Configuration for apalis-pgmq
10#[derive(Debug)]
11pub struct Config<Codec = JsonCodec<Vec<u8>>> {
12    poll_strategy: MultiStrategy,
13    buffer_size: usize,
14    queue: Queue,
15    visibility_timeout: Duration,
16    _codec: PhantomData<Codec>,
17}
18
19impl<C> Clone for Config<C> {
20    fn clone(&self) -> Self {
21        Self {
22            poll_strategy: self.poll_strategy.clone(),
23            buffer_size: self.buffer_size,
24            queue: self.queue.clone(),
25            visibility_timeout: self.visibility_timeout,
26            _codec: self._codec,
27        }
28    }
29}
30
31impl<Codec> Config<Codec> {
32    /// Gets the queue name
33    pub fn queue(&self) -> &Queue {
34        &self.queue
35    }
36
37    /// Sets the queue name (builder style)
38    pub fn with_queue<S: AsRef<str>>(mut self, queue: S) -> Self {
39        self.queue = Queue::from(queue.as_ref());
40        self
41    }
42
43    /// Gets the polling strategy
44    pub fn poll_strategy(&self) -> &MultiStrategy {
45        &self.poll_strategy
46    }
47
48    /// Sets the polling strategy (builder style)
49    pub fn with_poll_strategy(mut self, strategy: MultiStrategy) -> Self {
50        self.poll_strategy = strategy;
51        self
52    }
53
54    /// Gets the buffer size
55    pub fn buffer_size(&self) -> usize {
56        self.buffer_size
57    }
58
59    /// Sets the buffer size (builder style)
60    pub fn with_buffer_size(mut self, size: usize) -> Self {
61        self.buffer_size = size;
62        self
63    }
64
65    /// Gets the visibility timeout
66    pub fn visibility_timeout(&self) -> Duration {
67        self.visibility_timeout
68    }
69
70    /// Sets the visibility timeout (builder style)
71    pub fn with_visibility_timeout(mut self, timeout: Duration) -> Self {
72        self.visibility_timeout = timeout;
73        self
74    }
75
76    /// Specify your own codec
77    pub fn with_codec<C>(self) -> Config<C> {
78        Config {
79            poll_strategy: self.poll_strategy,
80            buffer_size: self.buffer_size,
81            queue: self.queue,
82            visibility_timeout: self.visibility_timeout,
83            _codec: PhantomData,
84        }
85    }
86}
87
88impl Default for Config<JsonCodec<Vec<u8>>> {
89    fn default() -> Self {
90        let config = BackoffConfig::default().with_jitter(0.9);
91        let interval = IntervalStrategy::new(Duration::from_millis(50)).with_backoff(config);
92        let poll_strategy = StrategyBuilder::new().apply(interval).build();
93        Self {
94            poll_strategy,
95            buffer_size: 100,
96            queue: "default_queue".into(),
97            visibility_timeout: Duration::from_secs(30),
98            _codec: PhantomData,
99        }
100    }
101}