98
98
& mut self ,
99
99
remote_address : SocketAddress ,
100
100
stream : TcpStream ,
101
+ linger : Option < Duration > ,
101
102
subscriber_ctx : Self :: ConnectionContext ,
102
103
publisher : & Pub ,
103
104
clock : & C ,
@@ -107,7 +108,10 @@ where
107
108
{
108
109
// Make sure TCP_NODELAY is set
109
110
let _ = stream. set_nodelay ( true ) ;
110
- let _ = stream. set_linger ( Some ( Duration :: ZERO ) ) ;
111
+
112
+ if linger. is_some ( ) {
113
+ let _ = stream. set_linger ( linger) ;
114
+ }
111
115
112
116
let now = clock. get_time ( ) ;
113
117
@@ -116,7 +120,14 @@ where
116
120
let prev_stream = core:: mem:: replace ( & mut self . stream , Some ( ( stream, remote_address) ) ) ;
117
121
let prev_ctx = core:: mem:: replace ( & mut self . subscriber_ctx , Some ( subscriber_ctx) ) ;
118
122
119
- if let Some ( remote_address) = prev_stream. map ( |( _socket, remote_address) | remote_address) {
123
+ if let Some ( remote_address) = prev_stream. map ( |( socket, remote_address) | {
124
+ // If linger wasn't already set or it was set to a value other than 0, then override it
125
+ if linger. is_none ( ) || linger != Some ( Duration :: ZERO ) {
126
+ // close the stream immediately and send a reset to the client
127
+ let _ = socket. set_linger ( Some ( Duration :: ZERO ) ) ;
128
+ }
129
+ remote_address
130
+ } ) {
120
131
let sojourn_time = now. saturating_duration_since ( prev_queue_time) ;
121
132
let buffer_len = match prev_state {
122
133
WorkerState :: Init => 0 ,
@@ -331,6 +342,10 @@ impl WorkerState {
331
342
error : error. error ,
332
343
} ;
333
344
continue ;
345
+ } else {
346
+ // close the stream immediately and send a reset to the client
347
+ let _ = socket. set_linger ( Some ( Duration :: ZERO ) ) ;
348
+ drop ( socket) ;
334
349
}
335
350
}
336
351
return Err ( Some ( error. error ) ) . into ( ) ;
@@ -381,16 +396,15 @@ impl WorkerState {
381
396
}
382
397
383
398
#[ inline]
384
- fn poll_initial_packet < S , Pub > (
399
+ fn poll_initial_packet < Pub > (
385
400
cx : & mut task:: Context ,
386
- stream : & mut S ,
401
+ stream : & mut TcpStream ,
387
402
remote_address : & SocketAddress ,
388
403
recv_buffer : & mut msg:: recv:: Message ,
389
404
sojourn_time : Duration ,
390
405
publisher : & Pub ,
391
406
) -> Poll < Result < server:: InitialPacket , Option < io:: Error > > >
392
407
where
393
- S : Socket ,
394
408
Pub : EndpointPublisher ,
395
409
{
396
410
loop {
@@ -403,6 +417,10 @@ impl WorkerState {
403
417
sojourn_time,
404
418
} ,
405
419
) ;
420
+
421
+ // close the stream immediately and send a reset to the client
422
+ let _ = stream. set_linger ( Some ( Duration :: ZERO ) ) ;
423
+
406
424
return Err ( None ) . into ( ) ;
407
425
}
408
426
@@ -437,6 +455,9 @@ impl WorkerState {
437
455
} ,
438
456
) ;
439
457
458
+ // close the stream immediately and send a reset to the client
459
+ let _ = stream. set_linger ( Some ( Duration :: ZERO ) ) ;
460
+
440
461
return Err ( None ) . into ( ) ;
441
462
}
442
463
}
0 commit comments