Skip to main content

nntp_proxy/config/
defaults.rs

1//! Default values for configuration fields
2//!
3//! This module centralizes all default value functions used in serde deserialization.
4
5use super::types::CompressionCodec;
6use crate::types::{CacheCapacity, MaxConnections, MaxErrors};
7use std::path::PathBuf;
8use std::time::Duration;
9
10/// Default maximum connections per server
11///
12/// # Panics
13/// Panics only if the hard-coded non-zero default becomes invalid.
14#[inline]
15#[must_use]
16pub fn max_connections() -> MaxConnections {
17    MaxConnections::try_new(10).expect("10 is non-zero")
18}
19
20/// Default health check interval
21#[inline]
22#[must_use]
23pub const fn health_check_interval() -> Duration {
24    Duration::from_secs(30)
25}
26
27/// Default health check timeout
28#[inline]
29#[must_use]
30pub const fn health_check_timeout() -> Duration {
31    Duration::from_secs(5)
32}
33
34/// Default unhealthy threshold
35///
36/// # Panics
37/// Panics only if the hard-coded non-zero default becomes invalid.
38#[inline]
39#[must_use]
40pub fn unhealthy_threshold() -> MaxErrors {
41    MaxErrors::try_new(3).expect("3 is non-zero")
42}
43
44/// Default cache max capacity in bytes (memory tier)
45///
46/// # Panics
47/// Panics only if the hard-coded non-zero default becomes invalid.
48#[inline]
49#[must_use]
50pub fn cache_max_capacity() -> CacheCapacity {
51    // 64 MB default (good for availability-only mode)
52    CacheCapacity::try_new(64 * 1024 * 1024).expect("64MB is non-zero")
53}
54
55/// Default cache TTL (1 hour)
56#[inline]
57#[must_use]
58pub const fn cache_ttl() -> Duration {
59    crate::constants::duration_polyfill::from_hours(1)
60}
61
62/// Default for caching article bodies in explicit `[cache]` sections.
63///
64/// Availability-only mode is the default; set `store_article_bodies = true`
65/// explicitly when you want full body caching.
66#[inline]
67pub const fn cache_articles() -> bool {
68    false
69}
70
71/// Default for adaptive availability prechecking (false = disabled)
72#[inline]
73pub const fn adaptive_precheck() -> bool {
74    false
75}
76
77/// Default socket receive buffer size
78#[inline]
79#[must_use]
80pub const fn socket_recv_buffer_size() -> usize {
81    crate::constants::socket::HIGH_THROUGHPUT_RECV_BUFFER
82}
83
84/// Default socket send buffer size
85#[inline]
86#[must_use]
87pub const fn socket_send_buffer_size() -> usize {
88    crate::constants::socket::HIGH_THROUGHPUT_SEND_BUFFER
89}
90
91/// Default size of each pooled I/O buffer
92#[inline]
93#[must_use]
94pub const fn buffer_pool_size() -> usize {
95    crate::constants::buffer::POOL
96}
97
98/// Default number of buffers in the main buffer pool
99/// Sized for ~100 concurrent connections with single buffer per connection
100/// Total memory: 100 × 1MiB = 100MiB
101#[inline]
102#[must_use]
103pub const fn buffer_pool_count() -> usize {
104    crate::constants::buffer::POOL_COUNT
105}
106
107/// Default size of each capture buffer
108#[inline]
109#[must_use]
110pub const fn capture_pool_size() -> usize {
111    crate::constants::buffer::CAPTURE
112}
113
114/// Default number of buffers in the capture pool for caching
115/// Sized for 16 concurrent caching operations
116/// Total memory: 16 × 1MiB = 16MiB
117#[inline]
118#[must_use]
119pub const fn capture_pool_count() -> usize {
120    crate::constants::buffer::CAPTURE_COUNT
121}
122
123/// Default for TLS certificate verification (true for security)
124#[inline]
125#[must_use]
126pub const fn tls_verify_cert() -> bool {
127    true
128}
129
130/// Default maximum number of connections to check per health check cycle
131#[inline]
132#[must_use]
133pub const fn health_check_max_per_cycle() -> usize {
134    use crate::constants::pool::MAX_CONNECTIONS_PER_HEALTH_CHECK_CYCLE;
135    MAX_CONNECTIONS_PER_HEALTH_CHECK_CYCLE
136}
137
138/// Default timeout when acquiring a connection for health checking
139#[inline]
140#[must_use]
141pub const fn health_check_pool_timeout() -> Duration {
142    use crate::constants::pool::HEALTH_CHECK_POOL_TIMEOUT_MS;
143    Duration::from_millis(HEALTH_CHECK_POOL_TIMEOUT_MS)
144}
145
146// Disk cache defaults (for hybrid-cache feature)
147
148/// Default disk cache path
149#[inline]
150#[must_use]
151pub fn disk_cache_path() -> PathBuf {
152    PathBuf::from("/var/cache/nntp-proxy")
153}
154
155/// Default disk cache capacity (10 GB)
156///
157/// # Panics
158/// Panics only if the hard-coded non-zero default becomes invalid.
159#[inline]
160#[must_use]
161pub fn disk_cache_capacity() -> CacheCapacity {
162    CacheCapacity::try_new(10 * 1024 * 1024 * 1024).expect("10GB is non-zero")
163}
164
165/// Default disk cache compression codec (LZ4 = fast, ~60% reduction)
166#[inline]
167#[must_use]
168pub const fn disk_cache_compression_codec() -> CompressionCodec {
169    CompressionCodec::Lz4
170}
171
172/// Default disk cache shards
173#[inline]
174#[must_use]
175pub const fn disk_cache_shards() -> usize {
176    4
177}
178
179/// Default backend idle timeout (10 minutes)
180///
181/// After this duration of proxy-wide inactivity, idle backend connections are cleared.
182/// Prevents stale connections from accumulating during overnight idle periods.
183#[inline]
184pub const fn backend_idle_timeout() -> Duration {
185    crate::constants::duration_polyfill::from_minutes(10)
186}
187
188/// Default connection replacement cooldown.
189#[inline]
190pub const fn replacement_cooldown() -> Duration {
191    Duration::from_secs(30)
192}
193
194/// Default connection replacement cooldown option.
195#[inline]
196#[allow(clippy::unnecessary_wraps)] // Config defaults model an optional field even when the default is always present.
197pub const fn replacement_cooldown_option() -> Option<Duration> {
198    Some(replacement_cooldown())
199}
200
201/// Default log file level (warn)
202#[inline]
203pub fn log_file_level() -> String {
204    "warn".to_string()
205}