@@ -5,7 +5,10 @@ use log::{debug, error, info, warn};
55use std:: error:: Error ;
66use std:: sync:: atomic:: Ordering ;
77use std:: sync:: Arc ;
8- use tokio:: net:: { TcpListener , TcpStream } ;
8+ use tokio:: {
9+ io:: { self } ,
10+ net:: { TcpListener , TcpStream } ,
11+ } ;
912
1013pub ( crate ) async fn proxy ( config : Arc < Proxy > ) -> Result < ( ) , Box < dyn Error > > {
1114 let listener = TcpListener :: bind ( config. listen ) . await ?;
@@ -24,6 +27,9 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
2427 let permit = config. maxclients . clone ( ) . acquire_owned ( ) . await . unwrap ( ) ;
2528
2629 match listener. accept ( ) . await {
30+ Err ( e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
31+ continue ;
32+ }
2733 Err ( err) => {
2834 error ! ( "Failed to accept connection: {}" , err) ;
2935 return Err ( Box :: new ( err) ) ;
@@ -51,9 +57,9 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Err
5157 } else {
5258 let old = GLOBAL_THREAD_COUNT . fetch_add ( 1 , Ordering :: SeqCst ) ;
5359 info ! (
54- "New connection from {:?} , num :{:?}: Current Connections :{:?}" ,
60+ "New connection from {:?} , old :{:?}: Current Connections :{:?}" ,
5561 inbound. peer_addr( ) ?,
56- old + 1 ,
62+ old,
5763 GLOBAL_THREAD_COUNT
5864 ) ;
5965 }
@@ -106,20 +112,18 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Err
106112 } else {
107113 let old = GLOBAL_THREAD_COUNT . fetch_sub ( 1 , Ordering :: SeqCst ) ;
108114 info ! (
109- "Connection closed for {:?}, num :{:?}: Current Connections :{:?}" ,
115+ "OKAY: Connection closed for {:?}, old :{:?}: Current Connections :{:?}" ,
110116 upstream_name, old, GLOBAL_THREAD_COUNT
111117 ) ;
112- //drop(permit);
113118 Ok ( ( ) )
114119 }
115120 }
116121 Err ( e) => {
117122 let old = GLOBAL_THREAD_COUNT . fetch_sub ( 1 , Ordering :: SeqCst ) ;
118123 info ! (
119- "Connection closed for {:?}, num :{:?}: Current Connections :{:?}" ,
124+ "ERROR: Connection closed for {:?}, num :{:?}: Current Connections :{:?}" ,
120125 upstream_name, old, GLOBAL_THREAD_COUNT
121126 ) ;
122- //drop(permit);
123127 error ! ( "my error {:?}" , e) ;
124128 Ok ( ( ) )
125129 }
0 commit comments