camber 0.1.8

Opinionated async Rust for IO-bound services on top of Tokio
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
412
413
414
415
use super::handle::{ConnCtx, handle_request};
use super::router::ServerDispatch;
use super::server_lifecycle::{ConnectionLifecycle, ServerControl};
use crate::net::accept;
use crate::{RuntimeError, net};
use std::sync::Arc;

struct SyncConnectionState {
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    shutdown_notify: Arc<tokio::sync::Notify>,
    keepalive_timeout: std::time::Duration,
    remote_ip: std::net::IpAddr,
    lifecycle: ConnectionLifecycle,
}

pub(super) async fn accept_loop(
    listener: &net::Listener,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    shutdown_notify: Arc<tokio::sync::Notify>,
    keepalive_timeout: std::time::Duration,
    tls_acceptor: Option<tokio_rustls::TlsAcceptor>,
    conn_limit: Option<Arc<tokio::sync::Semaphore>>,
) -> Result<(), RuntimeError> {
    match &listener.inner {
        net::ListenerInner::Tcp(tcp) => {
            accept_tcp(
                tcp,
                router,
                ctx,
                shutdown_notify,
                keepalive_timeout,
                tls_acceptor,
                conn_limit,
            )
            .await
        }
        net::ListenerInner::Unix(unix, _) => {
            accept_unix(
                unix,
                router,
                ctx,
                shutdown_notify,
                keepalive_timeout,
                conn_limit,
            )
            .await
        }
    }
}

pub(super) async fn accept_tcp(
    listener: &tokio::net::TcpListener,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    shutdown_notify: Arc<tokio::sync::Notify>,
    keepalive_timeout: std::time::Duration,
    tls_acceptor: Option<tokio_rustls::TlsAcceptor>,
    conn_limit: Option<Arc<tokio::sync::Semaphore>>,
) -> Result<(), RuntimeError> {
    let script = listener
        .local_addr()
        .ok()
        .and_then(super::mock::lifecycle_script);
    accept::accept_loop_with_permit(
        listener,
        &shutdown_notify,
        conn_limit.as_ref(),
        script.as_ref(),
        |(stream, addr), permit| {
            let router = Arc::clone(&router);
            let ctx = Arc::clone(&ctx);
            let shutdown = Arc::clone(&shutdown_notify);
            let acceptor = tls_acceptor.clone();
            let remote_ip = addr.ip();
            async move {
                match acceptor {
                    Some(a) => {
                        let state = SyncConnectionState {
                            router,
                            ctx,
                            shutdown_notify: shutdown,
                            keepalive_timeout,
                            remote_ip,
                            lifecycle: ConnectionLifecycle::synchronous(permit),
                        };
                        serve_tls_connection(stream, a, state).await;
                    }
                    None => {
                        serve_stream(
                            stream,
                            router,
                            ctx,
                            shutdown,
                            keepalive_timeout,
                            Some(remote_ip),
                            ConnectionLifecycle::synchronous(permit),
                        )
                        .await;
                    }
                }
            }
        },
    )
    .await
}

async fn accept_unix(
    listener: &tokio::net::UnixListener,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    shutdown_notify: Arc<tokio::sync::Notify>,
    keepalive_timeout: std::time::Duration,
    conn_limit: Option<Arc<tokio::sync::Semaphore>>,
) -> Result<(), RuntimeError> {
    accept::accept_loop_with_permit(
        listener,
        &shutdown_notify,
        conn_limit.as_ref(),
        None,
        |stream, permit| {
            let router = Arc::clone(&router);
            let ctx = Arc::clone(&ctx);
            let shutdown = Arc::clone(&shutdown_notify);
            async move {
                serve_stream(
                    stream,
                    router,
                    ctx,
                    shutdown,
                    keepalive_timeout,
                    None,
                    ConnectionLifecycle::synchronous(permit),
                )
                .await;
            }
        },
    )
    .await
}

async fn serve_stream<S>(
    stream: S,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    shutdown_notify: Arc<tokio::sync::Notify>,
    keepalive_timeout: std::time::Duration,
    remote_addr: Option<std::net::IpAddr>,
    lifecycle: ConnectionLifecycle,
) where
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
    let io = hyper_util::rt::TokioIo::new(stream);
    serve_io(
        io,
        router,
        ctx,
        shutdown_notify,
        keepalive_timeout,
        remote_addr,
        lifecycle,
    )
    .await;
}

async fn serve_tls_connection(
    stream: tokio::net::TcpStream,
    acceptor: tokio_rustls::TlsAcceptor,
    state: SyncConnectionState,
) {
    let tls_stream = match accept::tls_handshake(stream, &acceptor).await {
        Some(s) => s,
        None => return,
    };
    serve_stream(
        tls_stream,
        state.router,
        state.ctx,
        state.shutdown_notify,
        state.keepalive_timeout,
        Some(state.remote_ip),
        state.lifecycle,
    )
    .await;
}

async fn serve_io<I>(
    io: hyper_util::rt::TokioIo<I>,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    shutdown_notify: Arc<tokio::sync::Notify>,
    keepalive_timeout: std::time::Duration,
    remote_addr: Option<std::net::IpAddr>,
    lifecycle: ConnectionLifecycle,
) where
    I: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
    let service = hyper::service::service_fn(move |req: hyper::Request<hyper::body::Incoming>| {
        let router = Arc::clone(&router);
        let ctx = Arc::clone(&ctx);
        let lifecycle = lifecycle.clone();
        async move { handle_request(req, &router, &ctx, remote_addr, &lifecycle).await }
    });

    let mut builder =
        hyper_util::server::conn::auto::Builder::new(hyper_util::rt::TokioExecutor::new());
    builder
        .http1()
        .keep_alive(true)
        .timer(hyper_util::rt::TokioTimer::new())
        .header_read_timeout(Some(keepalive_timeout));
    let conn = builder.serve_connection_with_upgrades(io, service);

    tokio::pin!(conn);
    tokio::select! {
        result = &mut conn => {
            match result {
                Ok(()) => {}
                Err(ref e) if is_benign_hyper_error(e.as_ref()) => {}
                Err(e) => tracing::warn!("connection error: {e}"),
            }
        }
        () = shutdown_notify.notified() => {
            // Signal HTTP/2 GOAWAY and let in-flight streams finish.
            conn.as_mut().graceful_shutdown();
            match tokio::time::timeout(std::time::Duration::from_secs(15), conn).await {
                Ok(Ok(())) => {}
                Ok(Err(ref e)) if is_benign_hyper_error(e.as_ref()) => {}
                Ok(Err(e)) => tracing::warn!("connection error during shutdown: {e}"),
                Err(_) => tracing::debug!("connection timed out during graceful shutdown"),
            }
        }
    }
}

pub(super) async fn serve_owned_connection(
    stream: tokio::net::TcpStream,
    tls_acceptor: Option<tokio_rustls::TlsAcceptor>,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    lifecycle: ConnectionLifecycle,
    keepalive_timeout: std::time::Duration,
    remote_addr: std::net::IpAddr,
) {
    match tls_acceptor {
        Some(acceptor) => {
            serve_owned_tls(
                stream,
                acceptor,
                router,
                ctx,
                lifecycle,
                keepalive_timeout,
                remote_addr,
            )
            .await;
        }
        None => {
            serve_owned_stream(
                stream,
                router,
                ctx,
                lifecycle,
                keepalive_timeout,
                remote_addr,
            )
            .await;
        }
    }
}

async fn serve_owned_tls(
    stream: tokio::net::TcpStream,
    acceptor: tokio_rustls::TlsAcceptor,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    lifecycle: ConnectionLifecycle,
    keepalive_timeout: std::time::Duration,
    remote_addr: std::net::IpAddr,
) {
    let mut control = match lifecycle.control() {
        Some(control) => control,
        None => return,
    };
    let handshake = accept::tls_handshake(stream, &acceptor);
    tokio::pin!(handshake);
    let tls_stream = tokio::select! {
        biased;
        _ = wait_for_shutdown(&mut control) => return,
        stream = &mut handshake => match stream {
            Some(stream) => stream,
            None => return,
        },
    };
    serve_owned_stream(
        tls_stream,
        router,
        ctx,
        lifecycle,
        keepalive_timeout,
        remote_addr,
    )
    .await;
}

async fn serve_owned_stream<S>(
    stream: S,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    lifecycle: ConnectionLifecycle,
    keepalive_timeout: std::time::Duration,
    remote_addr: std::net::IpAddr,
) where
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
    let io = hyper_util::rt::TokioIo::new(stream);
    serve_owned_io(
        io,
        router,
        ctx,
        lifecycle,
        keepalive_timeout,
        Some(remote_addr),
    )
    .await;
}

async fn serve_owned_io<I>(
    io: hyper_util::rt::TokioIo<I>,
    router: Arc<ServerDispatch>,
    ctx: Arc<ConnCtx>,
    lifecycle: ConnectionLifecycle,
    keepalive_timeout: std::time::Duration,
    remote_addr: Option<std::net::IpAddr>,
) where
    I: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
    let service_lifecycle = lifecycle.clone();
    let service = hyper::service::service_fn(move |request| {
        let router = Arc::clone(&router);
        let ctx = Arc::clone(&ctx);
        let lifecycle = service_lifecycle.clone();
        async move { handle_request(request, &router, &ctx, remote_addr, &lifecycle).await }
    });
    let builder = connection_builder(keepalive_timeout);
    let connection = builder.serve_connection_with_upgrades(io, service);
    tokio::pin!(connection);
    let mut control = match lifecycle.control() {
        Some(control) => control,
        None => return,
    };
    tokio::select! {
        biased;
        mode = wait_for_shutdown(&mut control) => match mode {
            ServerControl::Graceful | ServerControl::Abort => {
                connection.as_mut().graceful_shutdown();
                log_connection_result(connection.await, true);
            }
            ServerControl::Running => {}
        },
        result = &mut connection => log_connection_result(result, false),
    }
}

fn connection_builder(
    keepalive_timeout: std::time::Duration,
) -> hyper_util::server::conn::auto::Builder<hyper_util::rt::TokioExecutor> {
    let mut builder =
        hyper_util::server::conn::auto::Builder::new(hyper_util::rt::TokioExecutor::new());
    builder
        .http1()
        .keep_alive(true)
        .timer(hyper_util::rt::TokioTimer::new())
        .header_read_timeout(Some(keepalive_timeout));
    builder
}

async fn wait_for_shutdown(
    control: &mut tokio::sync::watch::Receiver<ServerControl>,
) -> ServerControl {
    loop {
        let current = *control.borrow_and_update();
        if current != ServerControl::Running {
            return current;
        }
        match control.changed().await {
            Ok(()) => {}
            Err(_) => return current,
        }
    }
}

fn log_connection_result<E>(result: Result<(), E>, draining: bool)
where
    E: AsRef<dyn std::error::Error + Send + Sync> + std::fmt::Display,
{
    match (result, draining) {
        (Ok(()), _) => {}
        (Err(ref error), _) if is_benign_hyper_error(error.as_ref()) => {}
        (Err(error), true) => tracing::warn!("connection error during shutdown: {error}"),
        (Err(error), false) => tracing::warn!("connection error: {error}"),
    }
}

fn is_benign_hyper_error(err: &(dyn std::error::Error + 'static)) -> bool {
    let mut source: Option<&(dyn std::error::Error + 'static)> = Some(err);
    while let Some(e) = source {
        match e.downcast_ref::<std::io::Error>() {
            Some(io_err) => return crate::error::is_benign_io(io_err),
            None => source = e.source(),
        }
    }
    false
}