You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: builder.rs
    path of file: ./repos/ntex/ntex/src/server
    
    the code of the file until where you have to start completion: use std::{fmt, future::Future, io, marker, mem, net, pin::Pin, task::Context, task::Poll};

use async_channel::unbounded;
use log::{error, info};
use socket2::{Domain, SockAddr, Socket, Type};

use crate::rt::{spawn, Signal, System};
use crate::time::{sleep, Millis};
use crate::{io::Io, service::ServiceFactory, util::join_all, util::Stream};

use super::accept::{AcceptLoop, AcceptNotify};
use super::config::{
    Config, ConfigWrapper, ConfiguredService, ServiceConfig, ServiceRuntime,
};
use super::service::{Factory, InternalServiceFactory};
use super::worker::{self, Worker, WorkerAvailability, WorkerClient, WorkerManagerCmd};
use super::{socket, socket::Listener, Server, ServerCommand, ServerStatus, Token};

const STOP_DELAY: Millis = Millis(300);

type ServerStream = Pin<Box<dyn Stream<Item = ServerCommand>>>;

/// Server builder
pub struct ServerBuilder {
    threads: usize,
    
    fn handle_cmd(&mut self, item: ServerCommand) {
        match item {
            ServerCommand::Pause(tx) => {
                self.accept.send(WorkerManagerCmd::Pause);
                let _ = tx.send(());
            }
            ServerCommand::Resume(tx) => {
                self.accept.send(WorkerManagerCmd::Resume);
                let _ = tx.send(());
            }
            ServerCommand::Signal(sig) => {
                // Signals support
                // Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop ntex system
                match sig {
                    Signal::Int => {
                        info!("SIGINT received, exiting");
                        self.exit = true;
                        self.handle_cmd(ServerCommand::Stop {
                            graceful: false,
                            completion: None,
                        })
                    }
                    Signal::Term => {
                        info!("SIGTERM received, stopping");
                        self.exit = true;
                        self.handle_cmd(ServerCommand::Stop {
                            graceful: true,
                            completion: None,
                        })
                    }
                    Signal::Quit => {
                        info!("SIGQUIT received, exiting");
                        self.exit = true;
                        self.handle_cmd(ServerCommand::Stop {
                            graceful: false,
                            completion: None,
                        })
                    }
                    _ => (),
                }
            }
            ServerCommand::Notify(tx) => {
                self.notify.push(tx);
            }
            ServerCommand::Stop {
                graceful,
                completion,
            } => {
                let exit = self.exit;

                // stop accept thread
                let (tx, rx) = std::sync::mpsc::channel();
                self.accept.send(WorkerManagerCmd::Stop(tx));
                let _ = rx.recv();

                let notify = std::mem::take(&mut self.notify);

                // stop workers
                if !self.workers.is_empty() && graceful {
                    let futs: Vec<_> = self
                        .workers
                        .iter()
                        .map(move |worker| worker.1.stop(graceful))
                        .collect();
                    spawn(async move {
                        let _ = join_all(futs).await;

                        if let Some(tx) = completion {
                            let _ = tx.send(());
                        }
                        for tx in notify {
                            let _ = tx.send(());
                        }
                        if exit {
                            sleep(STOP_DELAY).await;
                            System::current().stop();
                        }
                    });
                } else {
                    self.workers.iter().for_each(move |worker| {
                        worker.1.stop(false);
                    });

                    // we need to stop system if server was spawned
                    if self.exit {
                        spawn(async {
                            sleep(STOP_DELAY).await;
                            System::current().stop();
                        });
                    }
                    if let Some(tx) = completion {
                        let _ = tx.send(());
                    }
                    for tx in notify {
                        let _ = tx.send(());
                    }
                }
            }
            ServerCommand::WorkerFaulted(idx) => {
                let mut found = false;
                for i in 0..self.workers.len() {
                    if self.workers[i].0 == idx {
                        self.workers.swap_remove(i);
                        found = true;
                        break;
                    }
                }

                if found {
                    error!("Worker has died {:?}, restarting", idx);

                    let mut new_idx = self.workers.len();
                    'found: loop {
                        for i in 0..self.workers.len() {
                            if self.workers[i].0 == new_idx {
                                new_idx += 1;
                                continue 'found;
                            }
                        }
                        break;
                    }

                    let worker = self.start_worker(new_idx, self.accept.notify());
                    self.workers.push((new_idx, worker.clone()));
                    self.accept.send(WorkerManagerCmd::Worker(worker));
                }
            }
        }
    }