Files
cpp-httplib/docs-src/pages/ja/cookbook/c12-timeouts.md
yhirose 0fa4912891 Fix broken relative links in cookbook docs (Fix #2490)
Cookbook body links referenced sibling pages with a bare slug
(e.g. `c14-keep-alive`). Under the pretty-URL layout each page lives
in its own directory, so these resolve against the page's own
directory and 404. Prefix them with `../` to match the convention
already used in the tour and llm-app sections.

Verified clean with `docs-gen check`.
2026-07-08 22:45:29 -04:00

51 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "C12. タイムアウトを設定する"
order: 12
status: "draft"
---
クライアントには3種類のタイムアウトがあります。それぞれ別々に設定できます。
| 種類 | API | デフォルト | 意味 |
| --- | --- | --- | --- |
| 接続タイムアウト | `set_connection_timeout` | 300秒 | TCP接続の確立までの待ち時間 |
| 読み取りタイムアウト | `set_read_timeout` | 300秒 | レスポンスを受信する際の1回の`recv`待ち時間 |
| 書き込みタイムアウト | `set_write_timeout` | 5秒 | リクエストを送信する際の1回の`send`待ち時間 |
## 基本の使い方
```cpp
httplib::Client cli("http://localhost:8080");
cli.set_connection_timeout(5, 0); // 5秒
cli.set_read_timeout(10, 0); // 10秒
cli.set_write_timeout(10, 0); // 10秒
auto res = cli.Get("/api/data");
```
秒数とマイクロ秒を2引数で渡します。細かい指定が不要なら第2引数は省略できます。
## `std::chrono`で指定する
`std::chrono`の期間を直接渡すオーバーロードもあります。こちらのほうが読みやすいのでおすすめです。
```cpp
using namespace std::chrono_literals;
cli.set_connection_timeout(5s);
cli.set_read_timeout(10s);
cli.set_write_timeout(500ms);
```
## デフォルトでは300秒と長めな点に注意
接続タイムアウトと読み取りタイムアウトはデフォルトで**300秒5分**です。サーバーが反応しない場合、このままだと5分待たされます。短めに設定したほうが良いことが多いです。
```cpp
cli.set_connection_timeout(3s);
cli.set_read_timeout(10s);
```
> **Warning:** 読み取りタイムアウトは「1回の受信待ち」に対するタイムアウトです。大きなファイルのダウンロードで途中ずっとデータが流れている限り、リクエスト全体で30分かかっても発火しません。リクエスト全体の時間制限を設けたい場合は[C13. 全体タイムアウトを設定する](../c13-max-timeout)を使ってください。