6
6
//! `requests_received`: Total requests received
7
7
//!
8
8
9
- use std:: { net:: SocketAddr , sync:: Arc , time:: Duration } ;
10
-
11
9
use bytes:: Bytes ;
12
10
use http:: { header:: InvalidHeaderValue , status:: InvalidStatusCode , HeaderMap } ;
13
11
use http_body_util:: { combinators:: BoxBody , BodyExt } ;
14
- use hyper:: { header, service:: service_fn, Request , Response , StatusCode } ;
15
- use hyper_util:: {
16
- rt:: { TokioExecutor , TokioIo } ,
17
- server:: conn:: auto,
18
- } ;
12
+ use hyper:: { header, Request , Response , StatusCode } ;
19
13
use metrics:: counter;
20
14
use serde:: { Deserialize , Serialize } ;
21
- use tokio :: { pin , sync :: Semaphore , task :: JoinSet } ;
22
- use tracing:: { debug, error, info } ;
15
+ use std :: { net :: SocketAddr , time :: Duration } ;
16
+ use tracing:: { debug, error} ;
23
17
24
18
use super :: General ;
25
19
@@ -42,9 +36,9 @@ pub enum Error {
42
36
/// Failed to deserialize the configuration.
43
37
#[ error( "Failed to deserialize the configuration: {0}" ) ]
44
38
Serde ( #[ from] serde_json:: Error ) ,
45
- /// Wrapper for [`std::io ::Error`].
46
- #[ error( "IO error: {0}" ) ]
47
- Io ( #[ from] std :: io :: Error ) ,
39
+ /// Wrapper for [`crate::blackhole::common ::Error`].
40
+ #[ error( transparent ) ]
41
+ Common ( #[ from] crate :: blackhole :: common :: Error ) ,
48
42
}
49
43
50
44
/// Body variant supported by this blackhole.
@@ -240,72 +234,32 @@ impl Http {
240
234
/// Function will return an error if the configuration is invalid or if
241
235
/// receiving a packet fails.
242
236
pub async fn run ( self ) -> Result < ( ) , Error > {
243
- let listener = tokio:: net:: TcpListener :: bind ( self . httpd_addr ) . await ?;
244
- let sem = Arc :: new ( Semaphore :: new ( self . concurrency_limit ) ) ;
245
- let mut join_set = JoinSet :: new ( ) ;
246
-
247
- let shutdown = self . shutdown . recv ( ) ;
248
- pin ! ( shutdown) ;
249
- loop {
250
- tokio:: select! {
251
- ( ) = & mut shutdown => {
252
- info!( "shutdown signal received" ) ;
253
- break ;
254
- }
255
-
256
- incoming = listener. accept( ) => {
257
- let ( stream, addr) = match incoming {
258
- Ok ( ( s, a) ) => ( s, a) ,
259
- Err ( e) => {
260
- error!( "accept error: {e}" ) ;
261
- continue ;
262
- }
263
- } ;
264
-
265
- let metric_labels = self . metric_labels. clone( ) ;
266
- let body_bytes = self . body_bytes. clone( ) ;
267
- let headers = self . headers. clone( ) ;
268
- let status = self . status;
269
- let response_delay = self . response_delay;
270
- let sem = Arc :: clone( & sem) ;
271
-
272
- join_set. spawn( async move {
273
- debug!( "Accepted connection from {addr}" ) ;
274
- let permit = match sem. acquire_owned( ) . await {
275
- Ok ( p) => p,
276
- Err ( e) => {
277
- error!( "Semaphore closed: {e}" ) ;
278
- return ;
279
- }
280
- } ;
281
-
282
- let builder = auto:: Builder :: new( TokioExecutor :: new( ) ) ;
283
- let serve_future = builder
284
- . serve_connection(
285
- TokioIo :: new( stream) ,
286
- service_fn( move |req: Request <hyper:: body:: Incoming >| {
287
- debug!( "REQUEST: {:?}" , req) ;
288
- srv(
289
- status,
290
- metric_labels. clone( ) ,
291
- body_bytes. clone( ) ,
292
- req,
293
- headers. clone( ) ,
294
- response_delay,
295
- )
296
- } )
297
- ) ;
237
+ crate :: blackhole:: common:: run_httpd (
238
+ self . httpd_addr ,
239
+ self . concurrency_limit ,
240
+ self . shutdown ,
241
+ move || {
242
+ let metric_labels = self . metric_labels . clone ( ) ;
243
+ let body_bytes = self . body_bytes . clone ( ) ;
244
+ let headers = self . headers . clone ( ) ;
245
+ let status = self . status ;
246
+ let response_delay = self . response_delay ;
247
+
248
+ hyper:: service:: service_fn ( move |req| {
249
+ debug ! ( "REQUEST: {:?}" , req) ;
250
+ srv (
251
+ status,
252
+ metric_labels. clone ( ) ,
253
+ body_bytes. clone ( ) ,
254
+ req,
255
+ headers. clone ( ) ,
256
+ response_delay,
257
+ )
258
+ } )
259
+ } ,
260
+ )
261
+ . await ?;
298
262
299
- if let Err ( e) = serve_future. await {
300
- error!( "Error serving {addr}: {e}" ) ;
301
- }
302
- drop( permit) ;
303
- } ) ;
304
- }
305
- }
306
- }
307
- drop ( listener) ;
308
- while join_set. join_next ( ) . await . is_some ( ) { }
309
263
Ok ( ( ) )
310
264
}
311
265
}
0 commit comments