@@ -5,7 +5,10 @@ use log::{debug, error, info, warn};
5
5
use std:: error:: Error ;
6
6
use std:: sync:: atomic:: Ordering ;
7
7
use std:: sync:: Arc ;
8
- use tokio:: net:: { TcpListener , TcpStream } ;
8
+ use tokio:: {
9
+ io:: { self } ,
10
+ net:: { TcpListener , TcpStream } ,
11
+ } ;
9
12
10
13
pub ( crate ) async fn proxy ( config : Arc < Proxy > ) -> Result < ( ) , Box < dyn Error > > {
11
14
let listener = TcpListener :: bind ( config. listen ) . await ?;
@@ -24,6 +27,9 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
24
27
let permit = config. maxclients . clone ( ) . acquire_owned ( ) . await . unwrap ( ) ;
25
28
26
29
match listener. accept ( ) . await {
30
+ Err ( e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
31
+ continue ;
32
+ }
27
33
Err ( err) => {
28
34
error ! ( "Failed to accept connection: {}" , err) ;
29
35
return Err ( Box :: new ( err) ) ;
@@ -51,9 +57,9 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Err
51
57
} else {
52
58
let old = GLOBAL_THREAD_COUNT . fetch_add ( 1 , Ordering :: SeqCst ) ;
53
59
info ! (
54
- "New connection from {:?} , num :{:?}: Current Connections :{:?}" ,
60
+ "New connection from {:?} , old :{:?}: Current Connections :{:?}" ,
55
61
inbound. peer_addr( ) ?,
56
- old + 1 ,
62
+ old,
57
63
GLOBAL_THREAD_COUNT
58
64
) ;
59
65
}
@@ -106,20 +112,18 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Err
106
112
} else {
107
113
let old = GLOBAL_THREAD_COUNT . fetch_sub ( 1 , Ordering :: SeqCst ) ;
108
114
info ! (
109
- "Connection closed for {:?}, num :{:?}: Current Connections :{:?}" ,
115
+ "OKAY: Connection closed for {:?}, old :{:?}: Current Connections :{:?}" ,
110
116
upstream_name, old, GLOBAL_THREAD_COUNT
111
117
) ;
112
- //drop(permit);
113
118
Ok ( ( ) )
114
119
}
115
120
}
116
121
Err ( e) => {
117
122
let old = GLOBAL_THREAD_COUNT . fetch_sub ( 1 , Ordering :: SeqCst ) ;
118
123
info ! (
119
- "Connection closed for {:?}, num :{:?}: Current Connections :{:?}" ,
124
+ "ERROR: Connection closed for {:?}, num :{:?}: Current Connections :{:?}" ,
120
125
upstream_name, old, GLOBAL_THREAD_COUNT
121
126
) ;
122
- //drop(permit);
123
127
error ! ( "my error {:?}" , e) ;
124
128
Ok ( ( ) )
125
129
}
0 commit comments