Struct actix_server::Server
source · pub struct Server { /* private fields */ }
Expand description
General purpose TCP server that runs services receiving Tokio TcpStream
s.
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 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 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
>.