1
0
mirror of https://github.com/actix/actix-website synced 2024-12-04 20:51:54 +01:00
actix-website/docs/server.md

125 lines
7.0 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Server
---
2023-11-01 16:28:34 +01:00
import RenderCodeBlock from '@theme/CodeBlock'; import CodeBlock from '@site/src/components/code_block.js'; import { actixWebMajorVersion } from "@site/vars";
2018-05-22 23:15:08 +02:00
# The HTTP Server
2020-09-12 17:21:54 +02:00
The [**HttpServer**][httpserverstruct] type is responsible for serving HTTP requests.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
`HttpServer` accepts an application factory as a parameter, and the application factory must have `Send` + `Sync` boundaries. More about that in the _multi-threading_ section.
2018-05-22 23:15:08 +02:00
2022-04-07 17:52:12 +02:00
To start the web server it must first be bound to a network socket. Use [`HttpServer::bind()`][bindmethod] with a socket address tuple or string such as `("127.0.0.1", 8080)` or `"0.0.0.0:8080"`. This will fail if the socket is being used by another application.
2018-05-22 23:15:08 +02:00
2022-04-07 17:52:12 +02:00
After the `bind` is successful, use [`HttpServer::run()`][httpserver_run] to return a [`Server`][server] instance. The `Server` must be `await`ed or `spawn`ed to start processing requests and will run until it receives a shutdown signal (such as, by default, a `ctrl-c`; [read more here](#graceful-shutdown)).
2018-05-22 23:15:08 +02:00
<CodeBlock example="server" section="main" />
2018-05-22 23:15:08 +02:00
2022-02-26 06:01:13 +01:00
## Multi-Threading
2018-05-22 23:15:08 +02:00
`HttpServer` automatically starts a number of HTTP _workers_, by default this number is equal to the number of physical CPUs in the system. This number can be overridden with the [`HttpServer::workers()`][workers] method.
2018-05-22 23:15:08 +02:00
<CodeBlock example="server" file="workers.rs" section="workers" />
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
Once the workers are created, they each receive a separate _application_ instance to handle requests. Application state is not shared between the threads, and handlers are free to manipulate their copy of the state with no concurrency concerns.
2018-05-22 23:15:08 +02:00
2022-04-07 17:52:12 +02:00
Application state does not need to be `Send` or `Sync`, but application factories must be `Send` + `Sync`.
2018-05-22 23:15:08 +02:00
2022-04-07 17:52:12 +02:00
To share state between worker threads, use an `Arc`/`Data`. Special care should be taken once sharing and synchronization are introduced. In many cases, performance costs are inadvertently introduced as a result of locking the shared state for modifications.
2022-02-26 05:41:49 +01:00
In some cases these costs can be alleviated using more efficient locking strategies, for example using [read/write locks](https://doc.rust-lang.org/std/sync/struct.RwLock.html) instead of [mutexes](https://doc.rust-lang.org/std/sync/struct.Mutex.html) to achieve non-exclusive locking, but the most performant implementations often tend to be ones in which no locking occurs at all.
2022-02-26 05:41:49 +01:00
Since each worker thread processes its requests sequentially, handlers which block the current thread will cause the current worker to stop processing new requests:
```rust
fn my_handler() -> impl Responder {
std::thread::sleep(Duration::from_secs(5)); // <-- Bad practice! Will cause the current worker thread to hang!
"response"
}
```
2020-03-17 15:41:27 +01:00
2022-02-26 05:41:49 +01:00
For this reason, any long, non-cpu-bound operation (e.g. I/O, database operations, etc.) should be expressed as futures or asynchronous functions. Async handlers get executed concurrently by worker threads and thus don't block execution:
```rust
async fn my_handler() -> impl Responder {
tokio::time::sleep(Duration::from_secs(5)).await; // <-- Ok. Worker thread will handle other requests here
"response"
}
```
2022-02-26 05:41:49 +01:00
The same limitation applies to extractors as well. When a handler function receives an argument which implements `FromRequest`, and that implementation blocks the current thread, the worker thread will block when running the handler. Special attention must be given when implementing extractors for this very reason, and they should also be implemented asynchronously where needed.
2022-02-26 06:22:21 +01:00
## TLS / HTTPS
2018-05-22 23:15:08 +02:00
2022-04-07 17:52:12 +02:00
Actix Web supports two TLS implementations out-of-the-box: `rustls` and `openssl`.
The `rustls` crate feature is for `rustls` integration and `openssl` is for `openssl` integration.
2018-05-22 23:15:08 +02:00
<!-- DEPENDENCY -->
<RenderCodeBlock className="language-toml">
{`[dependencies]
actix-web = { version = "${actixWebMajorVersion}", features = ["openssl"] }
2020-12-26 19:34:17 +01:00
openssl = { version = "0.10" }
`}
</RenderCodeBlock>
2018-05-22 23:15:08 +02:00
<CodeBlock example="server" file="ssl.rs" section="ssl" />
2018-05-22 23:15:08 +02:00
To create the key.pem and cert.pem use the command. **Fill in your own subject**
2022-02-26 05:41:49 +01:00
2018-05-22 23:15:08 +02:00
```bash
$ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem \
-days 365 -sha256 -subj "/C=CN/ST=Fujian/L=Xiamen/O=TVlinux/OU=Org/CN=muro.lxd"
```
2022-02-26 05:41:49 +01:00
To remove the password, then copy nopass.pem to key.pem
2022-02-26 05:41:49 +01:00
2018-05-22 23:15:08 +02:00
```bash
$ openssl rsa -in key.pem -out nopass.pem
```
## Keep-Alive
2022-02-26 06:22:21 +01:00
Actix Web keeps connections open to wait for subsequent requests.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
> _keep alive_ connection behavior is defined by server settings.
2018-05-22 23:15:08 +02:00
2022-02-26 06:22:21 +01:00
- `Duration::from_secs(75)` or `KeepAlive::Timeout(75)`: enables 75 second keep-alive timer.
- `KeepAlive::Os`: uses OS keep-alive.
- `None` or `KeepAlive::Disabled`: disables keep-alive.
2018-05-22 23:15:08 +02:00
<CodeBlock example="server" file="keep_alive.rs" section="keep-alive" />
2018-05-22 23:15:08 +02:00
2023-02-03 20:11:14 +01:00
If the first option above is selected, then keep-alive is enabled for HTTP/1.1 requests if the response does not explicitly disallow it by, for example, setting the [connection type][httpconnectiontype] to `Close` or `Upgrade`. Force closing a connection can be done with [the `force_close()` method on `HttpResponseBuilder`](https://docs.rs/actix-web/4/actix_web/struct.HttpResponseBuilder.html#method.force_close)
2018-05-22 23:15:08 +02:00
2022-02-26 06:22:21 +01:00
> Keep-alive is **off** for HTTP/1.0 and is **on** for HTTP/1.1 and HTTP/2.0.
<CodeBlock example="server" file="keep_alive_tp.rs" section="example" />
2018-05-22 23:15:08 +02:00
## Graceful shutdown
2022-02-26 05:41:49 +01:00
`HttpServer` supports graceful shutdown. After receiving a stop signal, workers have a specific amount of time to finish serving requests. Any 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()`][shutdowntimeout] method.
2018-05-22 23:15:08 +02:00
2022-02-26 06:22:21 +01:00
`HttpServer` handles several OS signals. _CTRL-C_ is available on all OSes, other signals are available on unix systems.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
- _SIGINT_ - Force shutdown workers
- _SIGTERM_ - Graceful shutdown workers
- _SIGQUIT_ - Force shutdown workers
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
> It is possible to disable signal handling with [`HttpServer::disable_signals()`][disablesignals] method.
2022-03-06 00:55:35 +01:00
[server]: https://docs.rs/actix-web/4/actix_web/dev/struct.Server.html
[httpserverstruct]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html
[bindmethod]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.bind
2022-04-07 17:52:12 +02:00
[httpserver_run]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.run
2022-03-06 00:55:35 +01:00
[bindopensslmethod]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.bind_openssl
[bindrusttls]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.bind_rustls
[workers]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.workers
[tlsalpn]: https://tools.ietf.org/html/rfc7301
2021-02-26 19:48:27 +01:00
[exampleopenssl]: https://github.com/actix/examples/tree/master/security/openssl
2022-03-06 00:55:35 +01:00
[shutdowntimeout]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.shutdown_timeout
[disablesignals]: https://docs.rs/actix-web/4/actix_web/struct.HttpServer.html#method.disable_signals
[httpconnectiontype]: https://docs.rs/actix-web/4/actix_web/http/enum.ConnectionType.html