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
use crate::network::http::session::HAsyncService;
use tracing::error;
#[cfg(all(target_os = "linux", feature = "rt-glommio", not(feature = "rt-tokio")))]
pub(crate) async fn serve<T>(
connection: quinn::Connection,
service: T,
peer_addr: std::net::IpAddr,
) -> std::io::Result<()>
where
T: HAsyncService + Send + 'static,
{
let mut h3_conn = match h3::server::builder()
.build::<h3_quinn::Connection, bytes::Bytes>(h3_quinn::Connection::new(connection))
.await
{
Ok(h) => h,
Err(e) => {
error!("h3 handshake failed: {e:?}");
return Err(std::io::Error::other(format!("h3 handshake failed: {e:?}")));
}
};
// Share the per-connection service among request tasks
let svc = std::rc::Rc::new(std::cell::RefCell::new(Some(service)));
loop {
let svc_rc = std::rc::Rc::clone(&svc);
match h3_conn.accept().await {
Ok(Some(resolver)) => {
match resolver.resolve_request().await {
Ok((req, stream)) => {
glommio::spawn_local(async move {
let mut service = loop {
if let Some(s) = {
let mut guard = svc_rc.borrow_mut();
guard.take()
} {
break s;
}
glommio::yield_if_needed().await;
};
// run the service
use crate::network::http::h3_session::H3Session;
let result = service
.call(&mut H3Session::new(peer_addr, req, stream))
.await;
// Put the service back for the next request.
*svc_rc.borrow_mut() = Some(service);
if let Err(e) = result {
error!("h3 service error: {e}");
}
})
.detach();
}
Err(e) => {
error!("resolve_request: {e:?}");
}
};
}
Ok(None) => break,
Err(e) => {
error!("h3 accept error: {e:?}");
break;
}
}
glommio::yield_if_needed().await;
}
Ok(())
}
#[cfg(all(feature = "rt-tokio", not(feature = "rt-glommio")))]
pub(crate) async fn serve<T>(
connection: quinn::Connection,
service: T,
peer_addr: std::net::IpAddr,
) -> std::io::Result<()>
where
T: HAsyncService + Send + 'static,
{
// Build h3 connection over quinn (Tokio runtime underneath via h3-quinn)
let mut h3_conn = match h3::server::builder()
.build::<h3_quinn::Connection, bytes::Bytes>(h3_quinn::Connection::new(connection))
.await
{
Ok(h) => h,
Err(e) => {
error!("h3 handshake failed: {e:?}");
return Err(std::io::Error::other(format!("h3 handshake failed: {e:?}")));
}
};
// Shared per-connection service among request tasks (kept on the same LocalSet thread)
let svc = std::rc::Rc::new(std::cell::RefCell::new(Some(service)));
loop {
let svc_rc = std::rc::Rc::clone(&svc);
match h3_conn.accept().await {
Ok(Some(resolver)) => {
match resolver.resolve_request().await {
Ok((req, stream)) => {
tokio::task::spawn_local(async move {
// Spin-yield until we can take the service (single owner at a time)
let mut service = loop {
if let Some(s) = {
let mut guard = svc_rc.borrow_mut();
guard.take()
} {
break s;
}
tokio::task::yield_now().await;
};
// Run the service
use crate::network::http::h3_session::H3Session;
let result = service
.call(&mut H3Session::new(peer_addr, req, stream))
.await;
// Put the service back for the next request
*svc_rc.borrow_mut() = Some(service);
if let Err(e) = result {
error!("h3 service error: {e}");
}
});
}
Err(e) => {
error!("resolve_request: {e:?}");
}
}
}
Ok(None) => break, // graceful close
Err(e) => {
error!("h3 accept error: {e:?}");
break;
}
}
// Cooperative yield to keep the LocalSet responsive
tokio::task::yield_now().await;
}
Ok(())
}