@@ -187,21 +187,19 @@ impl Accept {
187
187
let mut guard = self . waker . guard ( ) ;
188
188
match guard. pop_front ( ) {
189
189
// worker notify it becomes available. we may want to recover
190
- // from backpressure.
190
+ // from backpressure.
191
191
Some ( WakerInterest :: WorkerAvailable ) => {
192
192
drop ( guard) ;
193
193
self . maybe_backpressure ( & mut sockets, false ) ;
194
194
}
195
- // a new worker thread is made and it's handle would be added
196
- // to Accept
195
+ // a new worker thread is made and it's handle would be added to Accept
197
196
Some ( WakerInterest :: Worker ( handle) ) => {
198
197
drop ( guard) ;
199
198
// maybe we want to recover from a backpressure.
200
199
self . maybe_backpressure ( & mut sockets, false ) ;
201
200
self . handles . push ( handle) ;
202
201
}
203
- // got timer interest and it's time to try register socket(s)
204
- // again.
202
+ // got timer interest and it's time to try register socket(s) again
205
203
Some ( WakerInterest :: Timer ) => {
206
204
drop ( guard) ;
207
205
self . process_timer ( & mut sockets)
@@ -238,16 +236,23 @@ impl Accept {
238
236
239
237
fn process_timer ( & self , sockets : & mut Slab < ServerSocketInfo > ) {
240
238
let now = Instant :: now ( ) ;
241
- sockets. iter_mut ( ) . for_each ( |( token, info) | {
242
- // only the ServerSocketInfo have an associate timeout value was de registered.
243
- if let Some ( inst) = info. timeout . take ( ) {
244
- if now > inst {
245
- self . register_logged ( token, info) ;
246
- } else {
239
+ sockets
240
+ . iter_mut ( )
241
+ // Only sockets that had an associated timeout were deregistered.
242
+ . filter ( |( _, info) | info. timeout . is_some ( ) )
243
+ . for_each ( |( token, info) | {
244
+ let inst = info. timeout . take ( ) . unwrap ( ) ;
245
+
246
+ if now < inst {
247
247
info. timeout = Some ( inst) ;
248
+ } else if !self . backpressure {
249
+ self . register_logged ( token, info) ;
248
250
}
249
- }
250
- } ) ;
251
+
252
+ // Drop the timeout if server is in backpressure and socket timeout is expired.
253
+ // When server recovers from backpressure it will register all sockets without
254
+ // a timeout value so this socket register will be delayed till then.
255
+ } ) ;
251
256
}
252
257
253
258
#[ cfg( not( target_os = "windows" ) ) ]
@@ -301,20 +306,21 @@ impl Accept {
301
306
}
302
307
303
308
fn maybe_backpressure ( & mut self , sockets : & mut Slab < ServerSocketInfo > , on : bool ) {
304
- if self . backpressure {
305
- if !on {
309
+ // Only operate when server is in a different backpressure than the given flag.
310
+ if self . backpressure != on {
311
+ if on {
312
+ self . backpressure = true ;
313
+ // TODO: figure out if timing out sockets can be safely de-registered twice.
314
+ self . deregister_all ( sockets) ;
315
+ } else {
306
316
self . backpressure = false ;
307
- for ( token, info) in sockets. iter_mut ( ) {
308
- if info. timeout . is_some ( ) {
309
- // socket will attempt to re-register itself when its timeout completes
310
- continue ;
311
- }
312
- self . register_logged ( token, info) ;
313
- }
317
+ sockets
318
+ . iter_mut ( )
319
+ // Only operate on sockets without associated timeout.
320
+ // Sockets with it will attempt to re-register when their timeout expires.
321
+ . filter ( |( _, info) | info. timeout . is_none ( ) )
322
+ . for_each ( |( token, info) | self . register_logged ( token, info) ) ;
314
323
}
315
- } else if on {
316
- self . backpressure = true ;
317
- self . deregister_all ( sockets) ;
318
324
}
319
325
}
320
326
0 commit comments