1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

add readme

This commit is contained in:
Nikolay Kim 2017-12-13 21:44:16 -08:00
parent c2751efa87
commit 406ef20262
2 changed files with 73 additions and 2 deletions

View File

@ -11,7 +11,7 @@ fn index(req: HttpRequest) -> String {
fn main() { fn main() {
HttpServer::new( HttpServer::new(
Application::new() || Application::new()
.resource("/{name}", |r| r.f(index))) .resource("/{name}", |r| r.f(index)))
.serve::<_, ()>("127.0.0.1:8080"); .serve::<_, ()>("127.0.0.1:8080");
} }
@ -34,7 +34,7 @@ fn main() {
* Transparent content compression/decompression (br, gzip, deflate) * Transparent content compression/decompression (br, gzip, deflate)
* Configurable request routing * Configurable request routing
* Multipart streams * Multipart streams
* Middlewares (Logger, Session included) * Middlewares (Logger, Session, DefaultHeaders)
* Built on top of [Actix](https://github.com/actix/actix). * Built on top of [Actix](https://github.com/actix/actix).
## HTTP/2 ## HTTP/2

71
guide/src/qs_3_5.md Normal file
View File

@ -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::<TcpStream, SocketAddr, _, _>::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::<TcpStream, SocketAddr, _, _>::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() {}
```