Move build method to Server type.

This commit is contained in:
fakeshadow 2021-04-06 08:29:12 +08:00
parent e01c6a4bb8
commit 5c463c67c1
6 changed files with 28 additions and 30 deletions

View File

@ -4,7 +4,7 @@
* Server can start regardless what runtime it's in. [#266] * Server can start regardless what runtime it's in. [#266]
* Remove `Future` impl for `ServerBuilder`. [#266] * Remove `Future` impl for `ServerBuilder`. [#266]
* Rename `Server` to `ServerHandle`. `ServerHandle` must be explicitly constructed with `Server::handle` API. [#266] * Rename `Server` to `ServerHandle`. `ServerHandle` must be explicitly constructed with `Server::handle` API. [#266]
* Add `Server` type that can be `await` for blocking until server stop. [#266] * Add `Server`(new type) that can be `await` for blocking until server stop. [#266]
[#266]: https://github.com/actix/actix-net/pull/266 [#266]: https://github.com/actix/actix-net/pull/266

View File

@ -9,17 +9,18 @@
//! Start typing. When you press enter the typed line will be echoed back. The server will log //! Start typing. When you press enter the typed line will be echoed back. The server will log
//! the length of each line it echos and the total size of data sent when the connection is closed. //! the length of each line it echos and the total size of data sent when the connection is closed.
use std::sync::{ use std::{
env, io,
sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
Arc, Arc,
},
}; };
use std::{env, io};
use actix_rt::net::TcpStream; use actix_rt::net::TcpStream;
use actix_server::ServerHandle; use actix_server::Server;
use actix_service::pipeline_factory; use actix_service::pipeline_factory;
use bytes::BytesMut; use bytes::BytesMut;
use futures_util::future::ok;
use log::{error, info}; use log::{error, info};
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
@ -36,7 +37,7 @@ async fn main() -> io::Result<()> {
// Bind socket address and start worker(s). By default, the server uses the number of available // Bind socket address and start worker(s). By default, the server uses the number of available
// logical CPU cores as the worker count. For this reason, the closure passed to bind needs // logical CPU cores as the worker count. For this reason, the closure passed to bind needs
// to return a service *factory*; so it can be created once per worker. // to return a service *factory*; so it can be created once per worker.
ServerHandle::build() Server::build()
.bind("echo", addr, move || { .bind("echo", addr, move || {
let count = Arc::clone(&count); let count = Arc::clone(&count);
let num2 = Arc::clone(&count); let num2 = Arc::clone(&count);
@ -79,7 +80,7 @@ async fn main() -> io::Result<()> {
.and_then(move |(_, size)| { .and_then(move |(_, size)| {
let num = num2.load(Ordering::SeqCst); let num = num2.load(Ordering::SeqCst);
info!("[{}] total bytes read: {}", num, size); info!("[{}] total bytes read: {}", num, size);
ok(size) async move { Ok(size) }
}) })
})? })?
.workers(1) .workers(1)

View File

@ -37,6 +37,11 @@ pub struct Server {
} }
impl Server { impl Server {
/// Start server building process
pub fn build() -> ServerBuilder {
ServerBuilder::default()
}
pub(crate) fn new(mut builder: ServerBuilder) -> Self { pub(crate) fn new(mut builder: ServerBuilder) -> Self {
let sockets = mem::take(&mut builder.sockets) let sockets = mem::take(&mut builder.sockets)
.into_iter() .into_iter()
@ -282,11 +287,6 @@ impl ServerHandle {
ServerHandle(tx, None) ServerHandle(tx, None)
} }
/// Start server building process
pub fn build() -> ServerBuilder {
ServerBuilder::default()
}
pub(crate) fn worker_faulted(&self, idx: usize) { pub(crate) fn worker_faulted(&self, idx: usize) {
let _ = self.0.send(ServerCommand::WorkerFaulted(idx)); let _ = self.0.send(ServerCommand::WorkerFaulted(idx));
} }

View File

@ -3,7 +3,7 @@ use std::{net, thread};
use actix_rt::{net::TcpStream, System}; use actix_rt::{net::TcpStream, System};
use crate::{ServerBuilder, ServerHandle, ServiceFactory}; use crate::{Server, ServerBuilder, ServerHandle, ServiceFactory};
/// The `TestServer` type. /// The `TestServer` type.
/// ///
@ -50,10 +50,7 @@ impl TestServer {
// run server in separate thread // run server in separate thread
thread::spawn(move || { thread::spawn(move || {
System::new().block_on(async { System::new().block_on(async {
let server = factory(ServerHandle::build()) let server = factory(Server::build()).workers(1).disable_signals().run();
.workers(1)
.disable_signals()
.run();
tx.send((server.handle(), System::current())).unwrap(); tx.send((server.handle(), System::current())).unwrap();
server.await server.await
@ -81,7 +78,7 @@ impl TestServer {
let local_addr = tcp.local_addr().unwrap(); let local_addr = tcp.local_addr().unwrap();
sys.block_on(async { sys.block_on(async {
let server = ServerHandle::build() let server = Server::build()
.listen("test", tcp, factory) .listen("test", tcp, factory)
.unwrap() .unwrap()
.workers(1) .workers(1)

View File

@ -2,7 +2,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{mpsc, Arc}; use std::sync::{mpsc, Arc};
use std::{net, thread, time}; use std::{net, thread, time};
use actix_server::ServerHandle; use actix_server::Server;
use actix_service::fn_service; use actix_service::fn_service;
use actix_utils::future::ok; use actix_utils::future::ok;
use futures_util::future::lazy; use futures_util::future::lazy;
@ -23,7 +23,7 @@ fn test_bind() {
let h = thread::spawn(move || { let h = thread::spawn(move || {
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
.workers(1) .workers(1)
.disable_signals() .disable_signals()
.bind("test", addr, move || fn_service(|_| ok::<_, ()>(()))) .bind("test", addr, move || fn_service(|_| ok::<_, ()>(())))
@ -50,7 +50,7 @@ fn test_listen() {
let lst = net::TcpListener::bind(addr).unwrap(); let lst = net::TcpListener::bind(addr).unwrap();
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
.disable_signals() .disable_signals()
.workers(1) .workers(1)
.listen("test", lst, move || { .listen("test", lst, move || {
@ -87,7 +87,7 @@ fn test_start() {
let h = thread::spawn(move || { let h = thread::spawn(move || {
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
.backlog(100) .backlog(100)
.disable_signals() .disable_signals()
.bind("test", addr, move || { .bind("test", addr, move || {
@ -155,7 +155,7 @@ fn test_configure() {
let h = thread::spawn(move || { let h = thread::spawn(move || {
let num = num2.clone(); let num = num2.clone();
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
.disable_signals() .disable_signals()
.configure(move |cfg| { .configure(move |cfg| {
let num = num.clone(); let num = num.clone();
@ -215,7 +215,7 @@ async fn test_max_concurrent_connections() {
let h = thread::spawn(move || { let h = thread::spawn(move || {
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
// Set a relative higher backlog. // Set a relative higher backlog.
.backlog(12) .backlog(12)
// max connection for a worker is 3. // max connection for a worker is 3.
@ -311,7 +311,7 @@ async fn test_service_restart() {
let h = thread::spawn(move || { let h = thread::spawn(move || {
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
.backlog(1) .backlog(1)
.disable_signals() .disable_signals()
.configure(move |cfg| { .configure(move |cfg| {
@ -387,7 +387,7 @@ async fn test_service_restart() {
let h = thread::spawn(move || { let h = thread::spawn(move || {
let num = num.clone(); let num = num.clone();
actix_rt::System::new().block_on(async { actix_rt::System::new().block_on(async {
let server = ServerHandle::build() let server = Server::build()
.backlog(1) .backlog(1)
.disable_signals() .disable_signals()
.bind("addr1", addr1, move || { .bind("addr1", addr1, move || {

View File

@ -30,7 +30,7 @@ use std::{
}; };
use actix_rt::net::TcpStream; use actix_rt::net::TcpStream;
use actix_server::ServerHandle; use actix_server::Server;
use actix_service::pipeline_factory; use actix_service::pipeline_factory;
use actix_tls::accept::rustls::{Acceptor as RustlsAcceptor, TlsStream}; use actix_tls::accept::rustls::{Acceptor as RustlsAcceptor, TlsStream};
use futures_util::future::ok; use futures_util::future::ok;
@ -68,7 +68,7 @@ async fn main() -> io::Result<()> {
let addr = ("127.0.0.1", 8443); let addr = ("127.0.0.1", 8443);
info!("starting server on port: {}", &addr.0); info!("starting server on port: {}", &addr.0);
ServerHandle::build() Server::build()
.bind("tls-example", addr, move || { .bind("tls-example", addr, move || {
let count = Arc::clone(&count); let count = Arc::clone(&count);