1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 17:52:40 +01:00
actix-extras/guide/src/qs_3_5.md

204 lines
6.7 KiB
Markdown
Raw Normal View History

2017-12-14 06:44:16 +01:00
# Server
2018-03-28 22:16:01 +02:00
The [*HttpServer*](../actix_web/struct.HttpServer.html) type is responsible for
serving http requests. *HttpServer* accepts application factory as a parameter,
Application factory must have `Send` + `Sync` boundaries. More about that in the
*multi-threading* section. To bind to a specific socket address, `bind()` must be used.
This method can be called multiple times. To start the http server, one of the *start*
methods can be used. `start()` method starts a simple server, `start_tls()` or `start_ssl()`
2017-12-19 03:56:58 +01:00
starts ssl server. *HttpServer* is an actix actor, it has to be initialized
2018-03-28 22:16:01 +02:00
within a properly configured actix system:
2017-12-19 03:56:58 +01:00
```rust
# extern crate actix;
# extern crate actix_web;
use actix::*;
use actix_web::*;
fn main() {
let sys = actix::System::new("guide");
HttpServer::new(
|| Application::new()
2018-03-02 04:12:59 +01:00
.resource("/", |r| r.h(httpcodes::HttpOk)))
2017-12-19 03:56:58 +01:00
.bind("127.0.0.1:59080").unwrap()
2017-12-19 18:08:36 +01:00
.start();
2017-12-19 03:56:58 +01:00
2018-02-13 07:56:47 +01:00
# actix::Arbiter::system().do_send(actix::msgs::SystemExit(0));
2017-12-19 03:56:58 +01:00
let _ = sys.run();
}
```
2017-12-14 07:36:28 +01:00
2018-03-28 22:16:01 +02:00
It is possible to start a server in a separate thread with the *spawn()* method. In that
case the server spawns a new thread and creates a new actix system in it. To stop
this server, send a `StopServer` message.
2017-12-28 02:49:10 +01:00
2018-03-28 22:16:01 +02:00
Http server is implemented as an actix actor. It is possible to communicate with the server
via a messaging system. All start methods like `start()`, `start_ssl()`, etc. return the
address of the started http server. Actix http server accepts several messages:
2017-12-28 02:49:10 +01:00
* `PauseServer` - Pause accepting incoming connections
* `ResumeServer` - Resume accepting incoming connections
* `StopServer` - Stop incoming connection processing, stop all workers and exit
```rust
# extern crate futures;
# extern crate actix;
# extern crate actix_web;
# use futures::Future;
use actix_web::*;
2018-01-06 01:32:36 +01:00
use std::thread;
use std::sync::mpsc;
2017-12-28 02:49:10 +01:00
fn main() {
2018-01-06 01:32:36 +01:00
let (tx, rx) = mpsc::channel();
2018-02-13 07:56:47 +01:00
2018-01-06 01:32:36 +01:00
thread::spawn(move || {
let sys = actix::System::new("http-server");
let addr = HttpServer::new(
|| Application::new()
2018-03-02 04:12:59 +01:00
.resource("/", |r| r.h(httpcodes::HttpOk)))
2018-01-06 01:32:36 +01:00
.bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0")
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
.start();
let _ = tx.send(addr);
let _ = sys.run();
});
let addr = rx.recv().unwrap();
2018-02-13 07:56:47 +01:00
let _ = addr.send(
2018-01-12 03:35:05 +01:00
server::StopServer{graceful:true}).wait(); // <- Send `StopServer` message to server.
2017-12-28 02:49:10 +01:00
}
```
2017-12-18 22:06:41 +01:00
2017-12-14 06:44:16 +01:00
## Multi-threading
2018-03-28 22:16:01 +02:00
Http server automatically starts an number of http workers, by default
this number is equal to number of logical CPUs in the system. This number
can be overridden with the `HttpServer::threads()` method.
2017-12-14 06:44:16 +01:00
```rust
# extern crate actix_web;
# extern crate tokio_core;
use actix_web::*;
fn main() {
2018-02-10 20:01:54 +01:00
HttpServer::new(
2017-12-14 06:44:16 +01:00
|| Application::new()
2018-03-02 04:12:59 +01:00
.resource("/", |r| r.h(httpcodes::HttpOk)))
2017-12-20 03:44:17 +01:00
.threads(4); // <- Start 4 workers
2017-12-14 06:44:16 +01:00
}
```
2018-03-28 22:16:01 +02:00
The server creates a separate application instance for each created worker. Application state
2017-12-14 06:44:16 +01:00
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`.
2017-12-14 06:56:30 +01:00
## SSL
2018-03-28 22:16:01 +02:00
There are two features for ssl server: `tls` and `alpn`. The `tls` feature is for `native-tls`
2017-12-14 06:56:30 +01:00
integration and `alpn` is for `openssl`.
```toml
[dependencies]
actix-web = { git = "https://github.com/actix/actix-web", features=["alpn"] }
```
```rust,ignore
use std::fs::File;
use actix_web::*;
fn main() {
let mut file = File::open("identity.pfx").unwrap();
let mut pkcs12 = vec![];
file.read_to_end(&mut pkcs12).unwrap();
let pkcs12 = Pkcs12::from_der(&pkcs12).unwrap().parse("12345").unwrap();
HttpServer::new(
|| Application::new()
.resource("/index.html", |r| r.f(index)))
2017-12-19 18:08:36 +01:00
.bind("127.0.0.1:8080").unwrap()
.serve_ssl(pkcs12).unwrap();
2017-12-14 06:56:30 +01:00
}
```
2017-12-15 05:12:28 +01:00
Note on *HTTP/2.0* protocol over tls without prior knowledge, it requires
2017-12-14 06:56:30 +01:00
[tls alpn](https://tools.ietf.org/html/rfc7301). At the moment only
`openssl` has `alpn ` support.
2017-12-30 21:13:23 +01:00
Please check [example](https://github.com/actix/actix-web/tree/master/examples/tls)
2018-03-28 22:16:01 +02:00
for a full example.
2017-12-14 06:56:30 +01:00
2017-12-14 06:44:16 +01:00
## Keep-Alive
2017-12-15 05:12:28 +01:00
Actix can wait for requests on a keep-alive connection. *Keep alive*
2017-12-14 06:44:16 +01:00
connection behavior is defined by server settings.
* `75` or `Some(75)` or `KeepAlive::Timeout(75)` - enable 75 sec *keep alive* timer according
2018-03-10 06:40:51 +01:00
request and response settings.
* `None` or `KeepAlive::Disabled` - disable *keep alive*.
2018-03-10 06:40:51 +01:00
* `KeepAlive::Tcp(75)` - Use `SO_KEEPALIVE` socket option.
2017-12-14 06:44:16 +01:00
```rust
# extern crate actix_web;
# extern crate tokio_core;
use actix_web::*;
fn main() {
HttpServer::new(||
Application::new()
.resource("/", |r| r.h(httpcodes::HttpOk)))
.keep_alive(75); // <- Set keep-alive to 75 seconds
2018-02-10 20:01:54 +01:00
HttpServer::new(||
2017-12-14 06:44:16 +01:00
Application::new()
2018-03-02 04:12:59 +01:00
.resource("/", |r| r.h(httpcodes::HttpOk)))
2018-03-10 06:40:51 +01:00
.keep_alive(server::KeepAlive::Tcp(75)); // <- Use `SO_KEEPALIVE` socket option.
HttpServer::new(||
Application::new()
.resource("/", |r| r.h(httpcodes::HttpOk)))
.keep_alive(None); // <- Disable keep-alive
2017-12-14 06:44:16 +01:00
}
```
2018-03-28 22:16:01 +02:00
If first option is selected then *keep alive* state is
calculated based on the response's *connection-type*. By default
2017-12-14 06:44:16 +01:00
`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*
2018-03-02 04:12:59 +01:00
and is on for *HTTP/1.1* and *HTTP/2.0*.
2017-12-14 06:44:16 +01:00
2018-03-28 22:16:01 +02:00
*Connection type* can be change with `HttpResponseBuilder::connection_type()` method.
2017-12-14 06:44:16 +01:00
```rust
# extern crate actix_web;
2018-03-31 02:31:18 +02:00
use actix_web::{HttpRequest, HttpResponse, http, httpcodes::HttpOk};
2017-12-14 06:44:16 +01:00
fn index(req: HttpRequest) -> HttpResponse {
2018-03-02 04:12:59 +01:00
HttpOk.build()
2018-03-31 02:31:18 +02:00
.connection_type(http::ConnectionType::Close) // <- Close connection
2017-12-14 07:36:28 +01:00
.force_close() // <- Alternative method
2017-12-14 06:44:16 +01:00
.finish().unwrap()
}
# fn main() {}
```
2017-12-29 01:25:47 +01:00
## Graceful shutdown
2018-03-28 22:16:01 +02:00
Actix http server supports graceful shutdown. After receiving a stop signal, workers
have a specific amount of time to finish serving requests. Workers still alive after the
timeout are force-dropped. By default the shutdown timeout is set to 30 seconds.
You can change this parameter with the `HttpServer::shutdown_timeout()` method.
2017-12-29 01:25:47 +01:00
2018-03-28 22:16:01 +02:00
You can send a stop message to the server with the server address and specify if you want
graceful shutdown or not. The `start()` methods return address of the server.
2017-12-29 01:25:47 +01:00
2018-01-06 01:32:36 +01:00
Http server handles several OS signals. *CTRL-C* is available on all OSs,
other signals are available on unix systems.
2017-12-29 01:25:47 +01:00
* *SIGINT* - Force shutdown workers
* *SIGTERM* - Graceful shutdown workers
* *SIGQUIT* - Force shutdown workers
2018-03-28 22:16:01 +02:00
It is possible to disable signal handling with `HttpServer::disable_signals()` method.