77
88use anyhow:: Result ;
99use async_channel:: bounded;
10+ use async_executor:: Executor ;
1011use async_io:: Async ;
11- use futures_lite:: future;
1212use http_body_util:: Full ;
1313use hyper:: {
1414 Method , Request , Response , StatusCode , Uri ,
@@ -102,7 +102,6 @@ impl Task<'_> {
102102 ) ;
103103
104104 let blocklist = self . config . pprof_blocklist . unwrap_or ( PPROF_BLOCKLIST ) ;
105-
106105 let guard = ProfilerGuardBuilder :: default ( )
107106 . frequency ( profile_sampling)
108107 . blocklist ( blocklist)
@@ -183,14 +182,29 @@ pub async fn serve<'a>(bind_address: SocketAddr, config: Config<'a>) -> Result<(
183182 let listener = Async :: < TcpListener > :: bind ( bind_address) ?;
184183 let ( s, r) = bounded :: < Task > ( MAX_CONCURRENT_REQUESTS ) ;
185184 let config = Arc :: new ( config) ;
185+ let ex = Arc :: new ( Executor :: new ( ) ) ;
186186
187- loop {
188- // stack max MAX_CONCURRENT_REQUESTS requests, prefering stacking than answering to them.
189- // if we cannot stack anymore, drop the connection and other pending requests.
190- // we don't need a multi threaded server to serve pprof server, but don't want it to be a source of DDOS.
191- future:: or (
192- async {
193- // Wait for a new client.
187+ ex. spawn ( {
188+ let ex = ex. clone ( ) ;
189+ async move {
190+ loop {
191+ if let Ok ( task) = r. recv ( ) . await {
192+ ex. spawn ( async {
193+ task. handle_client ( ) . await . unwrap_or_default ( ) ;
194+ } )
195+ . detach ( ) ;
196+ }
197+ }
198+ }
199+ } )
200+ . detach ( ) ;
201+
202+ ex. run ( {
203+ async move {
204+ // stack max MAX_CONCURRENT_REQUESTS requests
205+ // if we cannot add more tasks, drop the connection
206+ // we don't need a multi threaded server to serve pprof server, but don't want it to be a source of DOS.
207+ loop {
194208 let listener = listener. accept ( ) . await ;
195209 if let Ok ( ( client, _) ) = listener {
196210 let task = Task {
@@ -201,13 +215,10 @@ pub async fn serve<'a>(bind_address: SocketAddr, config: Config<'a>) -> Result<(
201215 // we ignore the potential error as it would mean we should drop the connection if channel is full.
202216 let _ = s. try_send ( task) ;
203217 }
204- } ,
205- async {
206- if let Ok ( task) = r. recv ( ) . await {
207- task. handle_client ( ) . await . unwrap_or_default ( ) ;
208- }
209- } ,
210- )
211- . await ;
212- }
218+ }
219+ }
220+ } )
221+ . await ;
222+
223+ Ok ( ( ) )
213224}
0 commit comments