mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
Describe keep-alive parking in the Keep-Alive cookbook page
This commit is contained in:
1 parent
b99ccee3db
commit
0289c0f528
2 files changed
+36
-4
No files matched your search
@@ -34,7 +34,7 @@ svr.set_keep_alive_timeout(10s);
|
|||||||
## Tuning ideas
|
## Tuning ideas
|
||||||
|
|
||||||
**Too many idle connections eating resources**
|
**Too many idle connections eating resources**
|
||||||
Shorten the timeout so idle connections drop and release their worker threads.
|
Shorten the timeout so idle connections drop and release their worker threads. If you want to keep them open, turn on keep-alive parking instead (see below).
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
svr.set_keep_alive_timeout(2s);
|
svr.set_keep_alive_timeout(2s);
|
||||||
@@ -52,6 +52,22 @@ Set `set_keep_alive_max_count(1)` and every request gets its own connection. Mos
|
|||||||
|
|
||||||
## Relationship with the thread pool
|
## Relationship with the thread pool
|
||||||
|
|
||||||
A Keep-Alive connection holds a worker thread for its entire lifetime. The pool adds threads under load, up to its maximum thread count (4× the base count by default). If open connections outnumber that maximum, new connections wait in the queue until one closes. For thread counts, see [S21. Configure the thread pool](../s21-thread-pool).
|
By default, a Keep-Alive connection holds a worker thread for its entire lifetime. The pool adds threads under load, up to its maximum thread count (4× the base count by default). If open connections outnumber that maximum, new connections wait in the queue until one closes. For thread counts, see [S21. Configure the thread pool](../s21-thread-pool).
|
||||||
|
|
||||||
|
## Keep idle connections off the threads
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
svr.set_keep_alive_parking(true); // call it before listen()
|
||||||
|
```
|
||||||
|
|
||||||
|
With keep-alive parking, a connection waiting for its next request is handed to an event loop instead of keeping its worker thread, and goes back to a worker when the request arrives. An idle connection then costs a socket and some memory (a few kilobytes, more with TLS) instead of a thread, and connections that outnumber the threads take turns instead of waiting for one to close. `SSLServer` supports it too.
|
||||||
|
|
||||||
|
What changes with it:
|
||||||
|
|
||||||
|
- Consecutive requests on one connection may run on different threads, so don't keep per-connection state in `thread_local` variables.
|
||||||
|
- With no more connections than threads and very light handlers, handing the connection over can lower the requests served per second.
|
||||||
|
- A handler, a content provider and a WebSocket still hold their thread while they run.
|
||||||
|
|
||||||
|
The full list is in the [README](https://github.com/yhirose/cpp-httplib/blob/master/README.md#keep-alive-parking).
|
||||||
|
|
||||||
> **Note:** For the client side, see [C14. Understand connection reuse and Keep-Alive behavior](../c14-keep-alive). Even when the server closes the connection on timeout, the client reconnects automatically.
|
> **Note:** For the client side, see [C14. Understand connection reuse and Keep-Alive behavior](../c14-keep-alive). Even when the server closes the connection on timeout, the client reconnects automatically.
|
||||||
@@ -34,7 +34,7 @@ svr.set_keep_alive_timeout(10s);
|
|||||||
## チューニングの目安
|
## チューニングの目安
|
||||||
|
|
||||||
**アイドル接続が多くてリソースを食う**
|
**アイドル接続が多くてリソースを食う**
|
||||||
タイムアウトを短めに設定すると、遊んでいる接続がすぐに切れてスレッドが解放されます。
|
タイムアウトを短めに設定すると、遊んでいる接続がすぐに切れてスレッドが解放されます。接続は残したままにしたい場合は、代わりにKeep-Aliveのパーキング(後述)を有効にします。
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
svr.set_keep_alive_timeout(2s);
|
svr.set_keep_alive_timeout(2s);
|
||||||
@@ -52,6 +52,22 @@ svr.set_keep_alive_max_count(1000);
|
|||||||
|
|
||||||
## スレッドプールとの関係
|
## スレッドプールとの関係
|
||||||
|
|
||||||
Keep-Aliveでつながりっぱなしの接続は、その間ずっとワーカースレッドを1つ占有します。スレッドプールは負荷に応じて最大スレッド数(デフォルトはベースの4倍)までスレッドを増やしますが、開いている接続数がそれを超えると、新しい接続はどれかが閉じるまでキューで待たされます。スレッド数の調整は[S21. マルチスレッド数を設定する](../s21-thread-pool)を参照してください。
|
デフォルトでは、Keep-Aliveでつながりっぱなしの接続は、その間ずっとワーカースレッドを1つ占有します。スレッドプールは負荷に応じて最大スレッド数(デフォルトはベースの4倍)までスレッドを増やしますが、開いている接続数がそれを超えると、新しい接続はどれかが閉じるまでキューで待たされます。スレッド数の調整は[S21. マルチスレッド数を設定する](../s21-thread-pool)を参照してください。
|
||||||
|
|
||||||
|
## アイドル接続にスレッドを持たせない
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
svr.set_keep_alive_parking(true); // listen()の前に呼ぶ
|
||||||
|
```
|
||||||
|
|
||||||
|
Keep-Aliveのパーキングを有効にすると、次のリクエストを待っている接続はワーカースレッドを手放してイベントループに預けられ、リクエストが届くとまたワーカーに戻ります。アイドル接続のコストはスレッド1つではなく、ソケットと少しのメモリ(数KB、TLSではもう少し多い)になり、スレッド数より多い接続も、どれかが閉じるのを待たずに順番に処理されます。`SSLServer`でも使えます。
|
||||||
|
|
||||||
|
有効にすると変わること:
|
||||||
|
|
||||||
|
- 同じ接続の連続するリクエストが、別々のスレッドで処理されることがあります。接続ごとの状態を`thread_local`変数に持たせないでください。
|
||||||
|
- 接続数がスレッド数以下で、ハンドラがとても軽い場合は、接続の受け渡しのぶん毎秒の処理数が下がることがあります。
|
||||||
|
- ハンドラ、コンテンツプロバイダ、WebSocketは、実行中は今までどおりスレッドを占有します。
|
||||||
|
|
||||||
|
すべての変更点は[README](https://github.com/yhirose/cpp-httplib/blob/master/README.md#keep-alive-parking)にあります。
|
||||||
|
|
||||||
> **Note:** クライアント側の挙動は[C14. 接続の再利用とKeep-Aliveの挙動を理解する](../c14-keep-alive)を参照してください。サーバーがタイムアウトで接続を切っても、クライアントは自動で再接続します。
|
> **Note:** クライアント側の挙動は[C14. 接続の再利用とKeep-Aliveの挙動を理解する](../c14-keep-alive)を参照してください。サーバーがタイムアウトで接続を切っても、クライアントは自動で再接続します。
|
||||||
Reference in new issue
Block a user