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 !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 Twhere
T: Future + ?Sized,
impl<T> FutureExt for Twhere T: Future + ?Sized,
source§fn map<U, F>(self, f: F) -> Map<Self, F>where
F: FnOnce(Self::Output) -> U,
Self: Sized,
fn map<U, F>(self, f: F) -> Map<Self, F>where F: FnOnce(Self::Output) -> U, Self: Sized,
source§fn map_into<U>(self) -> MapInto<Self, U>where
Self::Output: Into<U>,
Self: Sized,
fn map_into<U>(self) -> MapInto<Self, U>where Self::Output: Into<U>, Self: Sized,
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
Self: Sized,
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where F: FnOnce(Self::Output) -> Fut, Fut: Future, Self: Sized,
f
. Read moresource§fn left_future<B>(self) -> Either<Self, B>where
B: Future<Output = Self::Output>,
Self: Sized,
fn left_future<B>(self) -> Either<Self, B>where B: Future<Output = Self::Output>, Self: Sized,
source§fn right_future<A>(self) -> Either<A, Self>where
A: Future<Output = Self::Output>,
Self: Sized,
fn right_future<A>(self) -> Either<A, Self>where A: Future<Output = Self::Output>, Self: Sized,
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>where
Self::Output: Future,
Self: Sized,
fn flatten(self) -> Flatten<Self>where Self::Output: Future, Self: Sized,
source§fn flatten_stream(self) -> FlattenStream<Self>where
Self::Output: Stream,
Self: Sized,
fn flatten_stream(self) -> FlattenStream<Self>where Self::Output: Stream, Self: Sized,
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>where
F: FnOnce(&Self::Output),
Self: Sized,
fn inspect<F>(self, f: F) -> Inspect<Self, F>where F: FnOnce(&Self::Output), Self: Sized,
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, Global>>where
Self: Sized + Send + 'a,
fn boxed<'a>( self ) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a, Global>>where Self: Sized + Send + 'a,
source§fn boxed_local<'a>(
self
) -> Pin<Box<dyn Future<Output = Self::Output> + 'a, Global>>where
Self: Sized + 'a,
fn boxed_local<'a>( self ) -> Pin<Box<dyn Future<Output = Self::Output> + 'a, Global>>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
>.