From 406ef202626b34388a91ac0fb30efc6be33a40dc Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 13 Dec 2017 21:44:16 -0800 Subject: [PATCH] add readme --- README.md | 4 +-- guide/src/qs_3_5.md | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 guide/src/qs_3_5.md diff --git a/README.md b/README.md index b06b8af0c..db7d57d2c 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ fn index(req: HttpRequest) -> String { fn main() { HttpServer::new( - Application::new() + || Application::new() .resource("/{name}", |r| r.f(index))) .serve::<_, ()>("127.0.0.1:8080"); } @@ -34,7 +34,7 @@ fn main() { * Transparent content compression/decompression (br, gzip, deflate) * Configurable request routing * Multipart streams - * Middlewares (Logger, Session included) + * Middlewares (Logger, Session, DefaultHeaders) * Built on top of [Actix](https://github.com/actix/actix). ## HTTP/2 diff --git a/guide/src/qs_3_5.md b/guide/src/qs_3_5.md new file mode 100644 index 000000000..2a7750b32 --- /dev/null +++ b/guide/src/qs_3_5.md @@ -0,0 +1,71 @@ +# Server + +## Multi-threading + +Http server automatically starts number of http workers, by default +this number is equal to number of logical cpu in the system. This number +could be overriden with `HttpServer::threads()` method. + +```rust +# extern crate actix_web; +# extern crate tokio_core; +# use tokio_core::net::TcpStream; +# use std::net::SocketAddr; +use actix_web::*; + +fn main() { + HttpServer::::new( + || Application::new() + .resource("/", |r| r.f(|r| httpcodes::HTTPOk))) + .threads(4); // <- Start 4 threads +} +``` + +Server create separate application instance for each created worker. Application state +is not shared between threads, to share state `Arc` could be used. Application state +does not need to be `Send` and `Sync` but application factory must be `Send` + `Sync`. + +## Keep-Alive + +Actix can wait for requesta on a keep-alive connection. *Keep alive* +connection behavior is defined by server settings. + + * `Some(75)` - enable 75 sec *keep alive* timer according request and response settings. + * `Some(0)` - disable *keep alive*. + * `None` - Use `SO_KEEPALIVE` socket option. + +```rust +# extern crate actix_web; +# extern crate tokio_core; +# use tokio_core::net::TcpStream; +# use std::net::SocketAddr; +use actix_web::*; + +fn main() { + HttpServer::::new(|| + Application::new() + .resource("/", |r| r.f(|r| httpcodes::HTTPOk))) + .keep_alive(None); // <- Use `SO_KEEPALIVE` socket option. +} +``` + +If first option is selected then *keep alive* state +calculated based on response's *connection-type*. By default +`HttpResponse::connection_type` is not defined in that case *keep alive* +defined by request's http version. Keep alive is off for *HTTP/1.0* +and is on for *HTTP/1.1* and "HTTP/2.0". + +*Connection type* could be change with `HttpResponseBuilder::connection_type()` method. + +```rust +# extern crate actix_web; +# use actix_web::httpcodes::*; +use actix_web::*; + +fn index(req: HttpRequest) -> HttpResponse { + HTTPOk.build() + .connection_type(headers::ConnectionType::Close) // <- Close connection + .finish().unwrap() +} +# fn main() {} +```