async fn run(&mut self) -> crate::Result<()> {
        info!("accepting inbound connections");

        loop {
            // Wait for a permit to become available
            //
            // `acquire` returns a permit that is bound via a lifetime to the
            // semaphore. When the permit value is dropped, it is automatically
            // returned to the semaphore. This is convenient in many cases.
            // However, in this case, the permit must be returned in a different
            // task than it is acquired in (the handler task). To do this, we
            // "forget" the permit, which drops the permit value **without**
            // incrementing the semaphore's permits. Then, in the handler task
            // we manually add a new permit when processing completes.
            //
            // `acquire()` returns `Err` when the semaphore has been closed. We
            // don't ever close the sempahore, so `unwrap()` is safe.
            self.limit_connections.acquire().await.unwrap().forget();

            // Accept a new socket. This will attempt to perform error handling.
            // The `accept` method internally attempts to recover errors, so an
            // error here is non-recoverable.
            let socket = self.accept().await?;

            // Create the necessary per-connection handler state.
            let mut handler = Handler {
                // Get a handle to the shared database.
                db: self.db_holder.db(),

                // Initialize the connection state. This allocates read/write
                // buffers to perform redis protocol frame parsing.
                connection: Connection::new(socket),

                // The connection state needs a handle to the max connections
                // semaphore. When the handler is done processing the
                // connection, a permit is added back to the semaphore.
                limit_connections: self.limit_connections.clone(),

                // Receive shutdown notifications.
                shutdown: Shutdown::new(self.notify_shutdown.subscribe()),

                // Notifies the receiver half once all clones are
                // dropped.
                _shutdown_complete: self.shutdown_complete_tx.clone(),
            };

            // Spawn a new task to process the connections. Tokio tasks are like
            // asynchronous green threads and are executed concurrently.
            tokio::spawn(async move {
                // Process the connection. If an error is encountered, log it.
                if let Err(err) = handler.run().await {
                    error!(cause = ?err, "connection error");
                }
            });
        }
    }
/// Run the server
///
/// Listen for inbound connections. For each inbound connection, spawn a
/// task to process that connection.
///
/// # Errors
///
/// Returns `Err` if accepting returns an error. This can happen for a
/// number reasons that resolve over time. For example, if the underlying
/// operating system has reached an internal limit for max number of
/// sockets, accept will fail.
///
/// The process is not able to detect when a transient error resolves
/// itself. One strategy for handling this is to implement a back off
/// strategy, which is what we do here.
Run the server
Listen for inbound connections. For each inbound connection, spawn a
task to process that connection.
Run the server Listen for inbound connections . For each inbound connection spawn a task to process that connection .
async fn run(&mut self) -> crate::Result<()> {
        info!("accepting inbound connections");
        loop {
            // Wait for a permit to become available
            //
            // `acquire` returns a permit that is bound via a lifetime to the
            // semaphore. When the permit value is dropped, it is automatically
            // returned to the semaphore. This is convenient in many cases.
            // However, in this case, the permit must be returned in a different
            // task than it is acquired in (the handler task). To do this, we
            // "forget" the permit, which drops the permit value **without**
            // incrementing the semaphore's permits. Then, in the handler task
            // we manually add a new permit when processing completes.
            //
            // `acquire()` returns `Err` when the semaphore has been closed. We
            // don't ever close the sempahore, so `unwrap()` is safe.
            self.limit_connections.acquire().await.unwrap().forget();
            // Accept a new socket. This will attempt to perform error handling.
            // The `accept` method internally attempts to recover errors, so an
            // error here is non-recoverable.
            let socket = self.accept().await?;
            // Create the necessary per-connection handler state.
            let mut handler = Handler {
                // Get a handle to the shared database.
                db: self.db_holder.db(),
                // Initialize the connection state. This allocates read/write
                // buffers to perform redis protocol frame parsing.
                connection: Connection::new(socket),
                // The connection state needs a handle to the max connections
                // semaphore. When the handler is done processing the
                // connection, a permit is added back to the semaphore.
                limit_connections: self.limit_connections.clone(),
                // Receive shutdown notifications.
                shutdown: Shutdown::new(self.notify_shutdown.subscribe()),
                // Notifies the receiver half once all clones are
                // dropped.
                _shutdown_complete: self.shutdown_complete_tx.clone(),
            };
            // Spawn a new task to process the connections. Tokio tasks are like
            // asynchronous green threads and are executed concurrently.
            tokio::spawn(async move {
                // Process the connection. If an error is encountered, log it.
                if let Err(err) = handler.run().await {
                    error!(cause = ?err, "connection error");
                }
            });
        }
    }
async fn run ( & mut self ) -> crate :: Result < ( ) > { info ! ( " " ) ; loop { // Wait for a permit to become available // // `acquire` returns a permit that is bound via a lifetime to the // semaphore. When the permit value is dropped, it is automatically // returned to the semaphore. This is convenient in many cases. // However, in this case, the permit must be returned in a different // task than it is acquired in (the handler task). To do this, we // "forget" the permit, which drops the permit value **without** // incrementing the semaphore's permits. Then, in the handler task // we manually add a new permit when processing completes. // // `acquire()` returns `Err` when the semaphore has been closed. We // don't ever close the sempahore, so `unwrap()` is safe. self . limit_connections . acquire ( ) . await . unwrap ( ) . forget ( ) ; // Accept a new socket. This will attempt to perform error handling. // The `accept` method internally attempts to recover errors, so an // error here is non-recoverable. let socket = self . accept ( ) . await ? ; // Create the necessary per-connection handler state. let mut handler = Handler { // Get a handle to the shared database. db : self . db_holder . db ( ) , // Initialize the connection state. This allocates read/write // buffers to perform redis protocol frame parsing. connection : Connection :: new ( socket ) , // The connection state needs a handle to the max connections // semaphore. When the handler is done processing the // connection, a permit is added back to the semaphore. limit_connections : self . limit_connections . clone ( ) , // Receive shutdown notifications. shutdown : Shutdown :: new ( self . notify_shutdown . subscribe ( ) ) , // Notifies the receiver half once all clones are // dropped. _shutdown_complete : self . shutdown_complete_tx . clone ( ) , } ; // Spawn a new task to process the connections. Tokio tasks are like // asynchronous green threads and are executed concurrently. tokio :: spawn ( async move { // Process the connection. If an error is encountered, log it. if let Err ( err ) = handler . run ( ) . await { error ! ( cause err " " ) ; } } ) ; } }
Run the server
Listen for inbound connections.
Run the server Listen for inbound connections .
&mut self
errors
Returns `Err` if accepting returns an error. This can happen for a
number reasons that resolve over time. For example, if the underlying
operating system has reached an internal limit for max number of
sockets, accept will fail.

The process is not able to detect when a transient error resolves
itself. One strategy for handling this is to implement a back off
strategy, which is what we do here.
Returns ` Err ` if accepting returns an error . This can happen for a number reasons that resolve over time . For example if the underlying operating system has reached an internal limit for max number of sockets accept will fail . The process is not able to detect when a transient error resolves itself . One strategy for handling this is to implement a back off strategy which is what we do here .