#
actix_server/
accept.rs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462

    use std::{io, thread, time::Duration};

    use actix_rt::time::Instant;
    use mio::{Interest, Poll, Token as MioToken};
    use tracing::{debug, error, info};

    use crate::{
        availability::Availability,
        socket::MioListener,
        waker_queue::{WakerInterest, WakerQueue, WAKER_TOKEN},
        worker::{Conn, ServerWorker, WorkerHandleAccept, WorkerHandleServer},
        ServerBuilder, ServerHandle,
    };

    const TIMEOUT_DURATION_ON_ERROR: Duration = Duration::from_millis(510);

    struct ServerSocketInfo {
        token: usize,

        lst: MioListener,

        /// Timeout is used to mark the deadline when this socket's listener should be registered again
        /// after an error.
        timeout: Option<actix_rt::time::Instant>,
    }

    /// Poll instance of the server.
    pub(crate) struct Accept {
        poll: Poll,
        waker_queue: WakerQueue,
        handles: Vec<WorkerHandleAccept>,
        srv: ServerHandle,
        next: usize,
        avail: Availability,
        /// use the smallest duration from sockets timeout.
        timeout: Option<Duration>,
        paused: bool,
    }

    impl Accept {
        pub(crate) fn start(
            sockets: Vec<(usize, MioListener)>,
            builder: &ServerBuilder,
        ) -> io::Result<(WakerQueue, Vec<WorkerHandleServer>, thread::JoinHandle<()>)> {
            let handle_server = ServerHandle::new(builder.cmd_tx.clone());

            // construct poll instance and its waker
            let poll = Poll::new()?;
            let waker_queue = WakerQueue::new(poll.registry())?;

            // start workers and collect handles
            let (handles_accept, handles_server) = (0..builder.threads)
                .map(|idx| {
                    // clone service factories
                    let factories = builder
                        .factories
                        .iter()
                        .map(|f| f.clone_factory())
                        .collect::<Vec<_>>();

                    // start worker using service factories
                    ServerWorker::start(idx, factories, waker_queue.clone(), builder.worker_config)
                })
                .collect::<io::Result<Vec<_>>>()?
                .into_iter()
                .unzip();

            let (mut accept, mut sockets) = Accept::new_with_sockets(
                poll,
                waker_queue.clone(),
                sockets,
                handles_accept,
                handle_server,
            )?;

            let accept_handle = thread::Builder::new()
                .name("actix-server acceptor".to_owned())
                .spawn(move || accept.poll_with(&mut sockets))
                .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;

            Ok((waker_queue, handles_server, accept_handle))
        }

        fn new_with_sockets(
            poll: Poll,
            waker_queue: WakerQueue,
            sockets: Vec<(usize, MioListener)>,
            accept_handles: Vec<WorkerHandleAccept>,
            server_handle: ServerHandle,
        ) -> io::Result<(Accept, Box<[ServerSocketInfo]>)> {
            let sockets = sockets
                .into_iter()
                .map(|(token, mut lst)| {
                    // Start listening for incoming connections
                    poll.registry()
                        .register(&mut lst, MioToken(token), Interest::READABLE)?;

                    Ok(ServerSocketInfo {
                        token,
                        lst,
                        timeout: None,
                    })
                })
                .collect::<io::Result<_>>()?;

            let mut avail = Availability::default();

            // Assume all handles are avail at construct time.
            avail.set_available_all(&accept_handles);

            let accept = Accept {
                poll,
                waker_queue,
                handles: accept_handles,
                srv: server_handle,
                next: 0,
                avail,
                timeout: None,
                paused: false,
            };

            Ok((accept, sockets))
        }

        /// blocking wait for readiness events triggered by mio
        fn poll_with(&mut self, sockets: &mut [ServerSocketInfo]) {
            let mut events = mio::Events::with_capacity(256);

            loop {
                if let Err(err) = self.poll.poll(&mut events, self.timeout) {
                    match err.kind() {
                        io::ErrorKind::Interrupted => {}
                        _ => panic!("Poll error: {}", err),
                    }
                }

                for event in events.iter() {
                    let token = event.token();
                    match token {
                        WAKER_TOKEN => {
                            let exit = self.handle_waker(sockets);
                            if exit {
                                info!("accept thread stopped");
                                return;
                            }
                        }
                        _ => {
                            let token = usize::from(token);
                            self.accept(sockets, token);
                        }
                    }
                }

                // check for timeout and re-register sockets
                self.process_timeout(sockets);
            }
        }

        fn handle_waker(&mut self, sockets: &mut [ServerSocketInfo]) -> bool {
            // This is a loop because interests for command from previous version was
            // a loop that would try to drain the command channel. It's yet unknown
            // if it's necessary/good practice to actively drain the waker queue.
            loop {
                // Take guard with every iteration so no new interests can be added until the current
                // task is done. Take care not to take the guard again inside this loop.
                let mut guard = self.waker_queue.guard();

                #[allow(clippy::significant_drop_in_scrutinee)]
                match guard.pop_front() {
                    // Worker notified it became available.
                    Some(WakerInterest::WorkerAvailable(idx)) => {
                        drop(guard);

                        self.avail.set_available(idx, true);

                        if !self.paused {
                            self.accept_all(sockets);
                        }
                    }

                    // A new worker thread has been created so store its handle.
                    Some(WakerInterest::Worker(handle)) => {
                        drop(guard);

                        self.avail.set_available(handle.idx(), true);
                        self.handles.push(handle);

                        if !self.paused {
                            self.accept_all(sockets);
                        }
                    }

                    Some(WakerInterest::Pause) => {
                        drop(guard);

                        if !self.paused {
                            self.paused = true;

                            self.deregister_all(sockets);
                        }
                    }

                    Some(WakerInterest::Resume) => {
                        drop(guard);

                        if self.paused {
                            self.paused = false;

                            sockets.iter_mut().for_each(|info| {
                                self.register_logged(info);
                            });

                            self.accept_all(sockets);
                        }
                    }

                    Some(WakerInterest::Stop) => {
                        if !self.paused {
                            self.deregister_all(sockets);
                        }

                        return true;
                    }

                    // waker queue is drained
                    None => {
                        // Reset the WakerQueue before break so it does not grow infinitely
                        WakerQueue::reset(&mut guard);

                        return false;
                    }
                }
            }
        }

        fn process_timeout(&mut self, sockets: &mut [ServerSocketInfo]) {
            // always remove old timeouts
            if self.timeout.take().is_some() {
                let now = Instant::now();

                sockets
                    .iter_mut()
                    // Only sockets that had an associated timeout were deregistered.
                    .filter(|info| info.timeout.is_some())
                    .for_each(|info| {
                        let inst = info.timeout.take().unwrap();

                        if now < inst {
                            // still timed out; try to set new timeout
                            info.timeout = Some(inst);
                            self.set_timeout(inst - now);
                        } else if !self.paused {
                            // timeout expired; register socket again
                            self.register_logged(info);
                        }

                        // Drop the timeout if server is paused and socket timeout is expired.
                        // When server recovers from pause it will register all sockets without
                        // a timeout value so this socket register will be delayed till then.
                    });
            }
        }

        /// Update accept timeout with `duration` if it is shorter than current timeout.
        fn set_timeout(&mut self, duration: Duration) {
            match self.timeout {
                Some(ref mut timeout) => {
                    if *timeout > duration {
                        *timeout = duration;
                    }
                }
                None => self.timeout = Some(duration),
            }
        }

        #[cfg(not(target_os = "windows"))]
        fn register(&self, info: &mut ServerSocketInfo) -> io::Result<()> {
            let token = MioToken(info.token);
            self.poll
                .registry()
                .register(&mut info.lst, token, Interest::READABLE)
        }

        #[cfg(target_os = "windows")]
        fn register(&self, info: &mut ServerSocketInfo) -> io::Result<()> {
            // On windows, calling register without deregister cause an error.
            // See https://github.com/actix/actix-web/issues/905
            // Calling reregister seems to fix the issue.
            let token = MioToken(info.token);
            self.poll
                .registry()
                .register(&mut info.lst, token, Interest::READABLE)
                .or_else(|_| {
                    self.poll
                        .registry()
                        .reregister(&mut info.lst, token, Interest::READABLE)
                })
        }

        fn register_logged(&self, info: &mut ServerSocketInfo) {
            match self.register(info) {
                Ok(_) => debug!("resume accepting connections on {}", info.lst.local_addr()),
                Err(err) => error!("can not register server socket {}", err),
            }
        }

        fn deregister_logged(&self, info: &mut ServerSocketInfo) {
            match self.poll.registry().deregister(&mut info.lst) {
                Ok(_) => debug!("paused accepting connections on {}", info.lst.local_addr()),
                Err(err) => {
                    error!("can not deregister server socket {}", err)
                }
            }
        }

        fn deregister_all(&self, sockets: &mut [ServerSocketInfo]) {
            // This is a best effort implementation with following limitation:
            //
            // Every ServerSocketInfo with associated timeout will be skipped and it's timeout is
            // removed in the process.
            //
            // Therefore WakerInterest::Pause followed by WakerInterest::Resume in a very short gap
            // (less than 500ms) would cause all timing out ServerSocketInfos be re-registered before
            // expected timing.
            sockets
                .iter_mut()
                // Take all timeout.
                // This is to prevent Accept::process_timer method re-register a socket afterwards.
                .map(|info| (info.timeout.take(), info))
                // Socket info with a timeout is already deregistered so skip them.
                .filter(|(timeout, _)| timeout.is_none())
                .for_each(|(_, info)| self.deregister_logged(info));
        }

        // Send connection to worker and handle error.
        fn send_connection(&mut self, conn: Conn) -> Result<(), Conn> {
            let next = self.next();
            match next.send(conn) {
                Ok(_) => {
                    // Increment counter of WorkerHandle.
                    // Set worker to unavailable with it hit max (Return false).
                    if !next.inc_counter() {
                        let idx = next.idx();
                        self.avail.set_available(idx, false);
                    }
                    self.set_next();
                    Ok(())
                }
                Err(conn) => {
                    // Worker thread is error and could be gone.
                    // Remove worker handle and notify `ServerBuilder`.
                    self.remove_next();

                    if self.handles.is_empty() {
                        error!("no workers");
                        // All workers are gone and Conn is nowhere to be sent.
                        // Treat this situation as Ok and drop Conn.
                        return Ok(());
                    } else if self.handles.len() <= self.next {
                        self.next = 0;
                    }

                    Err(conn)
                }
            }
        }

        fn accept_one(&mut self, mut conn: Conn) {
            loop {
                let next = self.next();
                let idx = next.idx();

                if self.avail.get_available(idx) {
                    match self.send_connection(conn) {
                        Ok(_) => return,
                        Err(c) => conn = c,
                    }
                } else {
                    self.avail.set_available(idx, false);
                    self.set_next();

                    if !self.avail.available() {
                        while let Err(c) = self.send_connection(conn) {
                            conn = c;
                        }
                        return;
                    }
                }
            }
        }

        fn accept(&mut self, sockets: &mut [ServerSocketInfo], token: usize) {
            while self.avail.available() {
                let info = &mut sockets[token];

                match info.lst.accept() {
                    Ok(io) => {
                        let conn = Conn { io, token };
                        self.accept_one(conn);
                    }
                    Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => return,
                    Err(ref err) if connection_error(err) => continue,
                    Err(err) => {
                        error!("error accepting connection: {}", err);

                        // deregister listener temporary
                        self.deregister_logged(info);

                        // sleep after error. write the timeout to socket info as later
                        // the poll would need it mark which socket and when it's
                        // listener should be registered
                        info.timeout = Some(Instant::now() + Duration::from_millis(500));
                        self.set_timeout(TIMEOUT_DURATION_ON_ERROR);

                        return;
                    }
                };
            }
        }

        fn accept_all(&mut self, sockets: &mut [ServerSocketInfo]) {
            sockets
                .iter_mut()
                .map(|info| info.token)
                .collect::<Vec<_>>()
                .into_iter()
                .for_each(|idx| self.accept(sockets, idx))
        }

        #[inline(always)]
        fn next(&self) -> &WorkerHandleAccept {
            &self.handles[self.next]
        }

        /// Set next worker handle that would accept connection.
        #[inline(always)]
        fn set_next(&mut self) {
            self.next = (self.next + 1) % self.handles.len();
        }

        /// Remove next worker handle that fail to accept connection.
        fn remove_next(&mut self) {
            let handle = self.handles.swap_remove(self.next);
            let idx = handle.idx();
            // A message is sent to `ServerBuilder` future to notify it a new worker
            // should be made.
            self.srv.worker_faulted(idx);
            self.avail.set_available(idx, false);
        }
    }

    /// This function defines errors that are per-connection; if we get this error from the `accept()`
    /// system call it means the next connection might be ready to be accepted.
    ///
    /// All other errors will incur a timeout before next `accept()` call is attempted. The timeout is
    /// useful to handle resource exhaustion errors like `ENFILE` and `EMFILE`. Otherwise, it could
    /// enter into a temporary spin loop.
    fn connection_error(e: &io::Error) -> bool {
        e.kind() == io::ErrorKind::ConnectionRefused
            || e.kind() == io::ErrorKind::ConnectionAborted
            || e.kind() == io::ErrorKind::ConnectionReset
    }