pub struct Server { /* private fields */ }Expand description
General purpose TCP server that runs services receiving Tokio TcpStreams.
Handles creating worker threads, restarting faulted workers, connection accepting, and back-pressure logic.
Creates a worker per CPU core (or the number specified in ServerBuilder::workers) and
distributes connections with a round-robin strategy.
The Server must be awaited or polled in order to start running. It will resolve when the server has fully shut down.
§Shutdown Signals
On UNIX systems, SIGTERM will start a graceful shutdown and SIGQUIT or SIGINT will start a
forced shutdown. On Windows, a Ctrl-C signal will start a forced shutdown.
A graceful shutdown will wait for all workers to stop first.
§Examples
The following is a TCP echo server. Test using telnet 127.0.0.1 8080.
use std::io;
use actix_rt::net::TcpStream;
use actix_server::Server;
use actix_service::{fn_service, ServiceFactoryExt as _};
use bytes::BytesMut;
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
#[actix_rt::main]
async fn main() -> io::Result<()> {
    let bind_addr = ("127.0.0.1", 8080);
    Server::build()
        .bind("echo", bind_addr, move || {
            fn_service(move |mut stream: TcpStream| {
                async move {
                    let mut size = 0;
                    let mut buf = BytesMut::new();
                    loop {
                        match stream.read_buf(&mut buf).await {
                            // end of stream; bail from loop
                            Ok(0) => break,
                            // write bytes back to stream
                            Ok(bytes_read) => {
                                stream.write_all(&buf[size..]).await.unwrap();
                                size += bytes_read;
                            }
                            Err(err) => {
                                eprintln!("Stream Error: {:?}", err);
                                return Err(());
                            }
                        }
                    }
                    Ok(())
                }
            })
            .map_err(|err| eprintln!("Service Error: {:?}", err))
        })?
        .run()
        .await
}Implementations§
Source§impl Server
 
impl Server
Sourcepub fn build() -> ServerBuilder
 
pub fn build() -> ServerBuilder
Create server build.
Sourcepub fn handle(&self) -> ServerHandle
 
pub fn handle(&self) -> ServerHandle
Get a Server handle that can be used issue commands and change it’s state.
See ServerHandle for usage.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Server
impl !RefUnwindSafe for Server
impl Send for Server
impl !Sync for Server
impl Unpin for Server
impl !UnwindSafe for Server
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
 
impl<T> FutureExt for T
Source§fn with_cancellation_token(
    self,
    cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
    Self: Sized,
 
fn with_cancellation_token(
    self,
    cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
    Self: Sized,
CancellationToken::run_until_cancelled,
but with the advantage that it is easier to write fluent call chains,
and biased towards waiting for CancellationToken to complete. Read moreSource§fn with_cancellation_token_owned(
    self,
    cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
    Self: Sized,
 
fn with_cancellation_token_owned(
    self,
    cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
    Self: Sized,
CancellationToken::run_until_cancelled_owned,
but with the advantage that it is easier to write fluent call chains,
and biased towards waiting for CancellationToken to complete. Read moreSource§impl<T> FutureExt for T
 
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
 
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
 
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
 
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
 
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
 
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
    Self: Sized,
 
fn into_stream(self) -> IntoStream<Self>where
    Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
 
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
 
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
    Self: Sized,
 
fn fuse(self) -> Fuse<Self>where
    Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
 
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
    Self: Sized + UnwindSafe,
 
fn catch_unwind(self) -> CatchUnwind<Self>where
    Self: Sized + UnwindSafe,
Source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
    Self: Sized,
 
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
    Self: Sized,
() on completion and sends
its output to another future on a separate task. Read moreSource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
 
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
    Self: Sized + 'a,
 
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
    Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self>where
    Self: Sized,
 
fn unit_error(self) -> UnitError<Self>where
    Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = ()>.Source§fn never_error(self) -> NeverError<Self>where
    Self: Sized,
 
fn never_error(self) -> NeverError<Self>where
    Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = Never>.