conproxy 0.2.1

Cache proxy server for heterogeneous RAG/vector search backends with cascade, scope, and MCP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! OS-level socket tuning for proxy listeners and upstream clients.

use crate::config::SocketTuningConfig;
use std::net::SocketAddr;
use std::time::Duration;
use tracing::{info, warn};

/// Create a TCP listener socket with production-tuned options.
///
/// Applied options (controlled by `config`):
/// - SO_REUSEADDR — allows quick restart after crash
/// - SO_REUSEPORT — kernel-level load balancing across accept threads (Unix)
/// - SO_KEEPALIVE — detects dead connections
/// - TCP_NODELAY — disables Nagle's algorithm
/// - TCP_DEFER_ACCEPT — kernel waits for data before waking accept() (Linux)
/// - TCP_USER_TIMEOUT — caps unacknowledged data lifetime (Linux)
/// - Optional send/receive buffer sizes
/// - Configurable listen backlog
pub fn create_tuned_listener(
    addr: SocketAddr,
    config: &SocketTuningConfig,
) -> std::io::Result<tokio::net::TcpListener> {
    use socket2::{Domain, Protocol, Socket, TcpKeepalive, Type};

    let socket = Socket::new(Domain::for_address(addr), Type::STREAM, Some(Protocol::TCP))?;
    socket.set_reuse_address(true)?;
    socket.set_nonblocking(true)?;

    // SO_REUSEPORT: kernel-level load balancing across accept threads
    #[cfg(unix)]
    if config.reuse_port {
        socket.set_reuse_port(true)?;
    }

    // TCP_NODELAY: disable Nagle's algorithm so accepted connections inherit it
    if config.tcp_nodelay {
        socket.set_tcp_nodelay(true)?;
    }

    // SO_KEEPALIVE: detect dead connections
    socket.set_tcp_keepalive(
        &TcpKeepalive::new()
            .with_time(Duration::from_secs(config.tcp_keepalive_secs))
            .with_interval(Duration::from_secs(config.tcp_keepalive_interval)),
    )?;

    // Optional send/receive buffer sizes (skip to preserve OS autotuning)
    if let Some(size) = config.send_buffer_size {
        socket.set_send_buffer_size(size)?;
    }
    if let Some(size) = config.recv_buffer_size {
        socket.set_recv_buffer_size(size)?;
    }

    // TCP_DEFER_ACCEPT: don't wake accept() until client sends data
    #[cfg(target_os = "linux")]
    set_defer_accept(&socket, config.defer_accept_secs)?;

    // TCP_USER_TIMEOUT: max time for unacknowledged data
    #[cfg(target_os = "linux")]
    set_user_timeout(&socket, config.user_timeout_ms)?;

    socket.bind(&addr.into())?;
    socket.listen(config.listen_backlog as i32)?;

    let std_listener: std::net::TcpListener = socket.into();
    tokio::net::TcpListener::from_std(std_listener)
}

/// Create a reqwest `ClientBuilder` with production TCP tuning for upstream connections.
///
/// Sets TCP_NODELAY, keepalive, connection pool sizing, and timeout.
/// Returns a `ClientBuilder` so callers can add headers/auth before `.build()`.
pub fn create_tuned_client(
    timeout: Duration,
    config: &SocketTuningConfig,
) -> reqwest::ClientBuilder {
    reqwest::Client::builder()
        .tcp_nodelay(config.tcp_nodelay)
        .tcp_keepalive(Duration::from_secs(config.tcp_keepalive_secs))
        .pool_idle_timeout(Duration::from_secs(config.upstream_pool_idle_timeout_secs))
        .pool_max_idle_per_host(config.upstream_pool_max_idle)
        .timeout(timeout)
}

/// Create a reqwest `ClientBuilder` with default socket tuning.
///
/// Convenience wrapper that uses `SocketTuningConfig::default()`.
pub fn create_tuned_client_default(timeout: Duration) -> reqwest::ClientBuilder {
    create_tuned_client(timeout, &SocketTuningConfig::default())
}

/// Log a warning if the file descriptor limit is too low for proxy workloads.
#[cfg(unix)]
#[allow(unsafe_code)]
pub fn check_fd_limit() {
    let mut rlim = libc::rlimit {
        rlim_cur: 0,
        rlim_max: 0,
    };
    // SAFETY: `rlim` is a valid stack-allocated rlimit struct, `getrlimit` writes
    // into it. No alignment or aliasing issues.
    let rc = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) };
    if rc == 0 {
        if rlim.rlim_cur < 4096 {
            warn!(
                limit = rlim.rlim_cur,
                "File descriptor limit is low (recommended: >=65535). \
                 Set `ulimit -n 65535` or Docker `--ulimit nofile=65535:65535`"
            );
        } else {
            info!(limit = rlim.rlim_cur, "File descriptor limit");
        }
    }
}

#[cfg(not(unix))]
pub fn check_fd_limit() {}

/// Read a sysctl value from `/proc/sys/...` (Linux only).
#[cfg(target_os = "linux")]
fn read_sysctl(path: &str) -> Option<String> {
    std::fs::read_to_string(path)
        .ok()
        .map(|s| s.trim().to_string())
}

/// Check Linux sysctls and log warnings for suboptimal networking settings.
///
/// Informational only — never modifies sysctls.
#[cfg(target_os = "linux")]
pub fn check_sysctl_recommendations() {
    struct SysctlCheck {
        path: &'static str,
        name: &'static str,
        min: u64,
        why: &'static str,
    }

    let checks = [
        SysctlCheck {
            path: "/proc/sys/net/core/somaxconn",
            name: "net.core.somaxconn",
            min: 4096,
            why: "must match listen backlog for burst handling",
        },
        SysctlCheck {
            path: "/proc/sys/net/ipv4/tcp_max_syn_backlog",
            name: "net.ipv4.tcp_max_syn_backlog",
            min: 4096,
            why: "SYN queue size for connection burst handling",
        },
        SysctlCheck {
            path: "/proc/sys/net/core/netdev_max_backlog",
            name: "net.core.netdev_max_backlog",
            min: 5000,
            why: "input packet queue for burst traffic",
        },
    ];

    struct SysctlExact {
        path: &'static str,
        name: &'static str,
        expected: &'static str,
        why: &'static str,
    }

    let exact_checks = [
        SysctlExact {
            path: "/proc/sys/net/ipv4/tcp_slow_start_after_idle",
            name: "net.ipv4.tcp_slow_start_after_idle",
            expected: "0",
            why: "prevents cwnd reset after idle (hurts keep-alive connections)",
        },
        SysctlExact {
            path: "/proc/sys/net/ipv4/tcp_tw_reuse",
            name: "net.ipv4.tcp_tw_reuse",
            expected: "1",
            why: "reuse TIME_WAIT sockets for outbound connections",
        },
    ];

    let mut warnings: u32 = 0;

    for check in &checks {
        if let Some(val_str) = read_sysctl(check.path) {
            if let Ok(val) = val_str.parse::<u64>() {
                if val < check.min {
                    warn!(
                        sysctl = check.name,
                        value = val,
                        recommended_min = check.min,
                        reason = check.why,
                        "Sysctl value below recommended minimum"
                    );
                    warnings = warnings.saturating_add(1);
                }
            }
        }
    }

    for check in &exact_checks {
        if let Some(val_str) = read_sysctl(check.path) {
            if val_str.trim() != check.expected {
                warn!(
                    sysctl = check.name,
                    value = val_str.trim(),
                    recommended = check.expected,
                    reason = check.why,
                    "Sysctl value differs from recommended"
                );
                warnings = warnings.saturating_add(1);
            }
        }
    }

    // Check tcp_fin_timeout (should be <= 15)
    if let Some(val_str) = read_sysctl("/proc/sys/net/ipv4/tcp_fin_timeout") {
        if let Ok(val) = val_str.parse::<u64>() {
            if val > 15 {
                warn!(
                    sysctl = "net.ipv4.tcp_fin_timeout",
                    value = val,
                    recommended_max = 15,
                    "tcp_fin_timeout too high, reduces TIME_WAIT duration"
                );
                warnings = warnings.saturating_add(1);
            }
        }
    }

    // Check tcp_notsent_lowat (should be set to 131072)
    if let Some(val_str) = read_sysctl("/proc/sys/net/ipv4/tcp_notsent_lowat") {
        if let Ok(val) = val_str.parse::<u64>() {
            if val == u64::MAX || val > 131_072 {
                warn!(
                    sysctl = "net.ipv4.tcp_notsent_lowat",
                    value = val,
                    recommended = 131072,
                    "tcp_notsent_lowat too high, reduces send bufferbloat"
                );
                warnings = warnings.saturating_add(1);
            }
        }
    }

    if warnings == 0 {
        info!("Sysctl check: all networking parameters look good");
    } else {
        warn!(
            warnings,
            "Sysctl check: see https://docs.conproxy.dev/tuning for details"
        );
    }
}

#[cfg(not(target_os = "linux"))]
pub fn check_sysctl_recommendations() {
    // Sysctl checks only apply to Linux
}

/// Set TCP_DEFER_ACCEPT (Linux only).
/// Value is timeout in seconds — kernel waits for data before completing accept().
#[cfg(target_os = "linux")]
#[allow(unsafe_code)]
fn set_defer_accept(socket: &socket2::Socket, secs: i32) -> std::io::Result<()> {
    use std::os::unix::io::AsRawFd;
    let fd = socket.as_raw_fd();
    // SAFETY: `fd` is a valid socket fd, `secs` is a valid i32 on the stack.
    // `setsockopt` copies the value, no ownership transfer. Size matches type.
    let rc = unsafe {
        libc::setsockopt(
            fd,
            libc::IPPROTO_TCP,
            libc::TCP_DEFER_ACCEPT,
            &secs as *const i32 as *const libc::c_void,
            std::mem::size_of::<i32>() as libc::socklen_t,
        )
    };
    if rc != 0 {
        return Err(std::io::Error::last_os_error());
    }
    Ok(())
}

/// Set TCP_USER_TIMEOUT (Linux only).
/// Value is timeout in milliseconds — max time unacknowledged data may remain.
#[cfg(target_os = "linux")]
#[allow(unsafe_code)]
fn set_user_timeout(socket: &socket2::Socket, millis: u32) -> std::io::Result<()> {
    use std::os::unix::io::AsRawFd;
    let fd = socket.as_raw_fd();
    // SAFETY: `fd` is a valid socket fd, `millis` is a valid u32 on the stack.
    // `setsockopt` copies the value, no ownership transfer. Size matches type.
    let rc = unsafe {
        libc::setsockopt(
            fd,
            libc::IPPROTO_TCP,
            libc::TCP_USER_TIMEOUT,
            &millis as *const u32 as *const libc::c_void,
            std::mem::size_of::<u32>() as libc::socklen_t,
        )
    };
    if rc != 0 {
        return Err(std::io::Error::last_os_error());
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]
    use super::*;

    #[tokio::test]
    async fn test_create_tuned_listener_port_zero() {
        let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
        let config = SocketTuningConfig::default();
        let listener = create_tuned_listener(addr, &config).unwrap();
        let local = listener.local_addr().unwrap();
        assert_ne!(local.port(), 0); // OS assigned a real port
    }

    #[tokio::test]
    async fn test_create_tuned_listener_with_buffer_sizes() {
        let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
        let config = SocketTuningConfig {
            send_buffer_size: Some(65536),
            recv_buffer_size: Some(65536),
            ..Default::default()
        };
        let listener = create_tuned_listener(addr, &config).unwrap();
        assert_ne!(listener.local_addr().unwrap().port(), 0);
    }

    #[test]
    fn test_create_tuned_client_returns_builder() {
        let config = SocketTuningConfig::default();
        let builder = create_tuned_client(Duration::from_secs(30), &config);
        // Builder should produce a valid client
        let client = builder.build().unwrap();
        drop(client);
    }

    #[test]
    fn test_create_tuned_client_default_returns_builder() {
        let builder = create_tuned_client_default(Duration::from_secs(10));
        let client = builder.build().unwrap();
        drop(client);
    }

    #[test]
    fn test_create_tuned_client_custom_config() {
        let config = SocketTuningConfig {
            tcp_nodelay: false,
            tcp_keepalive_secs: 120,
            upstream_pool_idle_timeout_secs: 30,
            upstream_pool_max_idle: 5,
            ..Default::default()
        };
        let builder = create_tuned_client(Duration::from_secs(5), &config);
        let client = builder.build().unwrap();
        drop(client);
    }

    #[test]
    fn test_check_fd_limit_no_panic() {
        // Should not panic on any platform
        check_fd_limit();
    }

    #[test]
    fn test_check_sysctl_recommendations_no_panic() {
        // Should not panic on any platform (no-op on non-Linux)
        check_sysctl_recommendations();
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_read_sysctl_valid_path() {
        // somaxconn should always exist on Linux
        let val = read_sysctl("/proc/sys/net/core/somaxconn");
        assert!(val.is_some());
        assert!(!val.unwrap().is_empty());
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_read_sysctl_invalid_path() {
        let val = read_sysctl("/proc/sys/nonexistent/value");
        assert!(val.is_none());
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_set_defer_accept_on_new_socket() {
        use socket2::{Domain, Protocol, Socket, Type};
        let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP)).unwrap();
        let result = set_defer_accept(&socket, 5);
        assert!(result.is_ok());
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_set_user_timeout_on_new_socket() {
        use socket2::{Domain, Protocol, Socket, Type};
        let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP)).unwrap();
        let result = set_user_timeout(&socket, 10000);
        assert!(result.is_ok());
    }
}