diff --git a/docs/dev/PERFORMANCE_TESTING_STANDARD.md b/docs/dev/PERFORMANCE_TESTING_STANDARD.md index 5a54d560..7951b64e 100644 --- a/docs/dev/PERFORMANCE_TESTING_STANDARD.md +++ b/docs/dev/PERFORMANCE_TESTING_STANDARD.md @@ -181,9 +181,9 @@ ls -lh build/dist/lib/libuvhttp.a ```c // Recommended configuration config.max_connections = 10000; -config.read_buffer_size = 16384; +config.connection_timeout = 60; config.write_buffer_size = 16384; -config.keepalive_timeout = 60; +config.backlog = 256; config.connection_timeout = 30; ``` diff --git a/docs/guide/FAQ.md b/docs/guide/FAQ.md index 10442021..39bc95ed 100644 --- a/docs/guide/FAQ.md +++ b/docs/guide/FAQ.md @@ -341,10 +341,13 @@ int on_message(uvhttp_ws_connection_t* ws_conn, const char* data, **Answer:** ```c -// Enable Keep-Alive by setting configuration +// Keep-Alive is on by default. The idle timeout is connection_timeout +// (seconds, valid 5-300); the old keepalive_timeout / request_timeout +// fields were removed in v2.10 because no code path ever read them. uvhttp_config_t* config = NULL; uvhttp_config_new(&config); -config->keepalive_timeout = 60; // 60 seconds +uvhttp_config_set_defaults(config); +config->connection_timeout = 60; // 60 seconds uvhttp_server_t* server = NULL; uvhttp_server_new(loop, &server); @@ -415,11 +418,13 @@ uvhttp_server_listen(server, "0.0.0.0", 8081); **Answer:** ```c -// Increase timeout values +// Increase the idle timeout (connection_timeout: 5-300 seconds). +// There is no separate request_timeout / keepalive_timeout — v2.10 removed +// both fields because the connection timer is driven by connection_timeout. uvhttp_config_t* config = NULL; uvhttp_config_new(&config); -config->keepalive_timeout = 300; // 5 minutes -config->request_timeout = 60; // 1 minute +uvhttp_config_set_defaults(config); +config->connection_timeout = 300; // 5 minutes uvhttp_server_t* server = NULL; uvhttp_server_new(loop, &server); diff --git a/docs/guide/TUTORIAL.md b/docs/guide/TUTORIAL.md index 2e59d124..820a41e5 100644 --- a/docs/guide/TUTORIAL.md +++ b/docs/guide/TUTORIAL.md @@ -2308,9 +2308,10 @@ void* memory_pool_alloc(memory_pool_t* pool) { #### 11.2 Connection Optimization ```c -// Enable Keep-Alive +// Keep-Alive is on by default; connection_timeout is the knob the +// connection timer reads (5-300 seconds). uvhttp_config_t* config = uvhttp_config_new(); -config->keepalive_timeout = 30; // 30 seconds +config->connection_timeout = 30; // 30 seconds server->config = config; // Connection reuse @@ -2523,13 +2524,25 @@ if (result != UVHTTP_OK) { ### B. Configuration Parameters +Every parameter below is read by the server at runtime. Limits that look +configurable but are not (body/header/URL size, read buffer size, request +timeout, requests per connection) are compile-time constants in +`uvhttp_constants.h` — v2.10 removed the config fields that never took effect. + | Parameter | Default | Description | |------|--------|------| -| max_connections | 1000 | Maximum number of connections | -| max_body_size | 1048576 | Maximum request body size (1MB) | -| read_buffer_size | 8192 | Read buffer size | -| keepalive_timeout | 30 | Keep-Alive timeout (seconds) | -| request_timeout | 60 | Request timeout (seconds) | +| max_connections | 2048 | Maximum number of connections | +| backlog | 256 | Listen queue length | +| connection_timeout | 60 | Idle connection timeout (seconds, 5-300) | +| max_file_size | 10MB | Maximum static file response size | +| trust_proxy_headers | 0 | Honour X-Forwarded-For for client IP | +| websocket_max_frame_size | 16MB | Maximum WebSocket frame size | +| websocket_max_message_size | 64MB | Maximum WebSocket message size | +| websocket_ping_interval | 30 | Ping interval (seconds) | +| websocket_ping_timeout | 10 | Ping timeout (seconds) | +| tcp_keepalive_timeout | 60 | TCP keepalive idle time (seconds) | +| sendfile_timeout_ms | 30000 | sendfile wait timeout | +| sendfile_max_retry | 2 | sendfile retry count | ### C. HTTP Status Codes diff --git a/docs/guide/introduction.md b/docs/guide/introduction.md index 7cf1613b..44537e67 100644 --- a/docs/guide/introduction.md +++ b/docs/guide/introduction.md @@ -155,9 +155,11 @@ uvhttp_tls_context_load_cert_chain(tls_ctx, "server.crt"); uvhttp_tls_context_load_private_key(tls_ctx, "server.key"); server->tls_ctx = tls_ctx; -// Example: Set reasonable resource limits -server->max_connections = 1000; -server->config->max_body_size = 10 * 1024 * 1024; // 10MB +// Example: Set reasonable resource limits (fields an execution path reads). +// Body/header/URL limits are compile-time constants (UVHTTP_MAX_BODY_SIZE etc.), +// not config entries — v2.10 removed the config fields that never took effect. +server->config->max_connections = 1000; +server->config->max_file_size = 10 * 1024 * 1024; // 10MB ``` --- diff --git a/docs/guide/performance.md b/docs/guide/performance.md index 8af895fb..cebd4a54 100644 --- a/docs/guide/performance.md +++ b/docs/guide/performance.md @@ -190,12 +190,14 @@ uvhttp_router_add_route(router, "/api/posts", posts_handler); ### 5. Configure Keep-Alive -Adjust keep-alive timeout based on your workload: +Adjust the idle timeout based on your workload. `connection_timeout` is the +field the connection timer actually reads (valid range 5-300 seconds): ```c uvhttp_config_t* config = NULL; uvhttp_config_new(&config); -config->keepalive_timeout = 60; // seconds +uvhttp_config_set_defaults(config); +config->connection_timeout = 60; // seconds ``` ## Performance Testing diff --git a/docs/spec/config-api.md b/docs/spec/config-api.md index 6571015a..8d1ca3ea 100644 --- a/docs/spec/config-api.md +++ b/docs/spec/config-api.md @@ -46,21 +46,45 @@ set at the server level and can be shared across multiple server instances. ## Configurable Options -| Field | Type | Default | Range | Description | -|-------|------|---------|-------|-------------| -| `max_connections` | size_t | 2048 | 1-65535 | Max concurrent connections | -| `keepalive_timeout` | int | 60 | 0-3600 | Keep-alive timeout (seconds) | -| `request_timeout` | int | 30 | 1-3600 | Request timeout (seconds) | -| `max_header_size` | size_t | 8192 | 256-65536 | Max header size (bytes) | -| `max_body_size` | size_t | 10485760 | 1024-1073741824 | Max body size (bytes) | -| `max_headers` | int | 64 | 8-256 | Max header count | -| `read_buffer_size` | size_t | 4096 | 256-65536 | Read buffer size (bytes) | -| `backlog` | int | 128 | 0-65535 | TCP listen backlog | -| `tcp_nodelay` | int | 1 | 0-1 | TCP_NODELAY | -| `tcp_keepalive` | int | 1 | 0-1 | TCP keepalive | -| `websocket_max_frame_size` | size_t | 65536 | 1024-1048576 | Max WebSocket frame (bytes) | -| `websocket_ping_interval` | int | 30 | 5-300 | Ping interval (seconds) | -| `websocket_ping_timeout` | int | 10 | 3-60 | Ping timeout (seconds) | +**Rule: a field is either authoritative or absent.** Every field below is read +by at least one execution path. v2.10 deleted the 14 fields that were set, +validated, printed — and then ignored by the whole library: + +| Removed field (v2.10) | What actually owned that limit | +|---|---| +| `read_buffer_size` | compile-time `UVHTTP_READ_BUFFER_SIZE` (per-connection buffer, `connection.c`) | +| `max_body_size` | compile-time `UVHTTP_MAX_BODY_SIZE` (`request.c` / `response.c` / `utils.c`) | +| `max_header_size` | nothing enforced it at all | +| `max_url_size` | `MAX_URL_LEN` (2048) in `request.h` | +| `max_requests_per_connection` | nothing enforced it at all | +| `request_timeout`, `keepalive_timeout` | `connection_timeout` (`connection.c` idle timer) — these were two extra names for the same knob | +| `rate_limit_window`, `rate_limit_max_requests`, `rate_limit_max_window_seconds`, `rate_limit_min_timeout_seconds` | `server->rate_limit_*`, set by `uvhttp_server_enable_rate_limit()` | +| `cache_default_max_entries`, `cache_default_ttl` | compile-time `UVHTTP_CACHE_DEFAULT_MAX_ENTRIES` / `_TTL` | +| `lru_cache_batch_eviction_size` | compile-time `UVHTTP_LRU_CACHE_*_BATCH_EVICTION_SIZE` | + +`uvhttp_set_timeout()`, `uvhttp_set_max_body_size()`, +`uvhttp_config_update_size_limits()` and +`uvhttp_config_update_read_buffer_size()` were removed with them: they wrote +those fields and reported success. If you need one of these limits to be +runtime-tunable, add the field back **together with the code that reads it** — +a config entry nobody reads is documentation, not control. + +### Surviving fields + +| Field | Type | Default | Valid range | Read by | +|-------|------|---------|-------------|---------| +| `max_connections` | int | 2048 | 1-65535 | `server.c` connection cap | +| `backlog` | int | 256 | validated only as `<= max_connections` (warn) | `server.c` listen | +| `connection_timeout` | int | 60 | 5-300 | `connection.c` idle timer | +| `max_file_size` | size_t | 10 MB | not range-checked | `static.c` file responses | +| `trust_proxy_headers` | int | 0 | 0 or 1 | `request.c` client-IP resolution | +| `websocket_max_frame_size` | int | 16 MB | 1 KB-256 MB | `websocket.c` | +| `websocket_max_message_size` | int | 64 MB | 1 KB-1 GB, `>= frame_size` | `websocket.c` | +| `websocket_ping_interval` | int | 30 | 1-3600 | `websocket.c` | +| `websocket_ping_timeout` | int | 10 | 1-3600 | `websocket.c` | +| `tcp_keepalive_timeout` | int | 60 | 1-7200 | `server.c` TCP keepalive | +| `sendfile_timeout_ms` | int | 30000 | 1000-300000 | `static.c` sendfile wait | +| `sendfile_max_retry` | int | 2 | `>= 0` | `static.c` sendfile retry | ## Behavior Rules diff --git a/docs/zh/dev/PERFORMANCE_TESTING_STANDARD.md b/docs/zh/dev/PERFORMANCE_TESTING_STANDARD.md index 5bc81773..95ff90ff 100644 --- a/docs/zh/dev/PERFORMANCE_TESTING_STANDARD.md +++ b/docs/zh/dev/PERFORMANCE_TESTING_STANDARD.md @@ -181,9 +181,9 @@ ls -lh build/dist/lib/libuvhttp.a ```c // 推荐配置 config.max_connections = 10000; -config.read_buffer_size = 16384; +config.backlog = 256; config.write_buffer_size = 16384; -config.keepalive_timeout = 60; +config.connection_timeout = 60; config.connection_timeout = 30; ``` diff --git a/docs/zh/guide/FAQ.md b/docs/zh/guide/FAQ.md index 356e038f..7b953a0e 100644 --- a/docs/zh/guide/FAQ.md +++ b/docs/zh/guide/FAQ.md @@ -318,9 +318,13 @@ int on_message(uvhttp_ws_connection_t* ws_conn, const char* data, ### Q21: 如何启用连接池? ```c +// Keep-Alive 默认开启。空闲超时由 connection_timeout 决定(秒,5-300); +// 原来的 keepalive_timeout / request_timeout 字段在 v2.10 被删除, +// 因为没有任何代码路径读取它们。 uvhttp_config_t* config = NULL; uvhttp_config_new(&config); -config->keepalive_timeout = 60; // 60 秒 +uvhttp_config_set_defaults(config); +config->connection_timeout = 60; // 60 秒 uvhttp_server_t* server = NULL; uvhttp_server_new(loop, &server); @@ -382,10 +386,13 @@ uvhttp_server_listen(server, "0.0.0.0", 8081); ### Q26: 连接超时错误 ```c +// 调大空闲超时(connection_timeout,合法范围 5-300 秒)。 +// 没有独立的 request_timeout / keepalive_timeout —— v2.10 删除了这两个字段, +// 连接计时器读的是 connection_timeout。 uvhttp_config_t* config = NULL; uvhttp_config_new(&config); -config->keepalive_timeout = 300; // 5 分钟 -config->request_timeout = 60; // 1 分钟 +uvhttp_config_set_defaults(config); +config->connection_timeout = 300; // 5 分钟 uvhttp_server_t* server = NULL; uvhttp_server_new(loop, &server); diff --git a/docs/zh/guide/TUTORIAL.md b/docs/zh/guide/TUTORIAL.md index 8d31a5d7..a0012f8b 100644 --- a/docs/zh/guide/TUTORIAL.md +++ b/docs/zh/guide/TUTORIAL.md @@ -2305,7 +2305,7 @@ void* memory_pool_alloc(memory_pool_t* pool) { ```c // 启用 Keep-Alive uvhttp_config_t* config = uvhttp_config_new(); -config->keepalive_timeout = 30; // 30秒 +config->connection_timeout = 30; // 30秒(Keep-Alive 默认开启) server->config = config; // 连接复用 @@ -2520,13 +2520,24 @@ if (result != UVHTTP_OK) { ### B. 配置参数 +以下参数都会被服务器在运行时读取。看起来可配置、实际不是的限额(请求体 / +请求头 / URL 大小、读缓冲大小、请求超时、每连接请求数)由 +`uvhttp_constants.h` 的编译期常量决定 —— v2.10 删除了那些从未生效的配置字段。 + | 参数 | 默认值 | 说明 | |------|--------|------| -| max_connections | 1000 | 最大连接数 | -| max_body_size | 1048576 | 最大请求体大小 (1MB) | -| read_buffer_size | 8192 | 读取缓冲区大小 | -| keepalive_timeout | 30 | Keep-Alive 超时 (秒) | -| request_timeout | 60 | 请求超时 (秒) | +| max_connections | 2048 | 最大连接数 | +| backlog | 256 | 监听队列长度 | +| connection_timeout | 60 | 空闲连接超时(秒,5-300) | +| max_file_size | 10MB | 静态文件响应大小上限 | +| trust_proxy_headers | 0 | 是否采信 X-Forwarded-For 作为客户端 IP | +| websocket_max_frame_size | 16MB | WebSocket 单帧上限 | +| websocket_max_message_size | 64MB | WebSocket 消息上限 | +| websocket_ping_interval | 30 | ping 间隔(秒) | +| websocket_ping_timeout | 10 | ping 超时(秒) | +| tcp_keepalive_timeout | 60 | TCP keepalive 空闲时间(秒) | +| sendfile_timeout_ms | 30000 | sendfile 等待超时 | +| sendfile_max_retry | 2 | sendfile 重试次数 | ### C. HTTP 状态码 diff --git a/docs/zh/guide/introduction.md b/docs/zh/guide/introduction.md index 0f0421b7..4d627ba5 100644 --- a/docs/zh/guide/introduction.md +++ b/docs/zh/guide/introduction.md @@ -149,9 +149,11 @@ uvhttp_tls_context_load_cert_chain(tls_ctx, "server.crt"); uvhttp_tls_context_load_private_key(tls_ctx, "server.key"); server->tls_ctx = tls_ctx; -// 设置资源限制 -server->max_connections = 1000; -server->config->max_body_size = 10 * 1024 * 1024; // 10MB +// 设置资源限制(只列执行路径真正读取的字段)。请求体 / 请求头 / URL 上限 +// 是编译期常量(UVHTTP_MAX_BODY_SIZE 等),不是配置项 —— v2.10 删除了那些 +// 从未生效的配置字段。 +server->config->max_connections = 1000; +server->config->max_file_size = 10 * 1024 * 1024; // 10MB ``` --- diff --git a/docs/zh/guide/performance.md b/docs/zh/guide/performance.md index 9c276893..4ce3719a 100644 --- a/docs/zh/guide/performance.md +++ b/docs/zh/guide/performance.md @@ -189,12 +189,14 @@ uvhttp_router_add_route(router, "/api/posts", posts_handler); ### 5. 配置 Keep-Alive -根据工作负载调整 keep-alive 超时: +根据工作负载调整空闲超时。连接计时器实际读取的是 `connection_timeout` +(合法范围 5-300 秒): ```c uvhttp_config_t* config = NULL; uvhttp_config_new(&config); -config->keepalive_timeout = 60; // 秒 +uvhttp_config_set_defaults(config); +config->connection_timeout = 60; // 秒 ``` ## 性能测试 diff --git a/examples/01_basics/helloworld.c b/examples/01_basics/helloworld.c index 46d2b273..e056405a 100644 --- a/examples/01_basics/helloworld.c +++ b/examples/01_basics/helloworld.c @@ -96,16 +96,16 @@ static int hello_handler(uvhttp_request_t* request, uvhttp_response_t* response) "Hello, World!\n\n" "=== Server Configuration ===\n" "Max connections: %d\n" - "Max requests per connection: %d\n" + "Backlog: %d\n" "Active connections: %zu\n" - "Max body size: %zuMB\n" - "Read buffer size: %zuKB\n" + "Connection timeout: %ds\n" + "Max body size (compile-time): %zuMB\n" "===========================\n", config->max_connections, - config->max_requests_per_connection, + config->backlog, active_connections, - config->max_body_size / (1024 * 1024), - (size_t)config->read_buffer_size / 1024 + config->connection_timeout, + (size_t)UVHTTP_MAX_BODY_SIZE / (1024 * 1024) ); uvhttp_response_set_status(response, 200); @@ -146,13 +146,15 @@ int main() { /* Apply defaults, then override a few values. */ uvhttp_config_set_defaults(config); + /* Every field below is read by the server: max_connections caps + * concurrent connections, backlog sizes the listen queue, and + * connection_timeout drives the per-connection idle timer + * (connection.c). Limits that look configurable but are not were + * removed in v2.10 - body/header/URL/read-buffer sizes are owned by + * the compile-time constants in uvhttp_constants.h. */ config->max_connections = 500; - config->max_requests_per_connection = 100; config->backlog = 128; - config->max_body_size = 1048576; /* 1MB */ - config->read_buffer_size = 8192; - config->keepalive_timeout = 30; - config->request_timeout = 60; + config->connection_timeout = 60; if (uvhttp_config_validate(config) != UVHTTP_OK) { fprintf(stderr, "Configuration validation failed\n"); @@ -162,10 +164,8 @@ int main() { printf("Configuration loaded successfully:\n"); printf(" Max connections: %d\n", config->max_connections); - printf(" Max requests per connection: %d\n", - config->max_requests_per_connection); - printf(" Max body size: %zu bytes\n", config->max_body_size); - printf(" Read buffer size: %d bytes\n", config->read_buffer_size); + printf(" Backlog: %d\n", config->backlog); + printf(" Connection timeout: %d s\n", config->connection_timeout); /* Get the default loop. */ uv_loop_t* loop = uv_default_loop(); diff --git a/examples/01_basics/helloworld_complete.c b/examples/01_basics/helloworld_complete.c index a132a09d..ed04d2c7 100644 --- a/examples/01_basics/helloworld_complete.c +++ b/examples/01_basics/helloworld_complete.c @@ -99,16 +99,16 @@ static int hello_handler(uvhttp_request_t* request, "Hello, World!\n\n" "=== Server Configuration ===\n" "Max connections: %d\n" - "Max requests per connection: %d\n" + "Backlog: %d\n" "Active connections: %zu\n" - "Max body size: %zuMB\n" - "Read buffer size: %zuKB\n" + "Connection timeout: %ds\n" + "Max body size (compile-time): %zuMB\n" "===========================\n", config->max_connections, - config->max_requests_per_connection, + config->backlog, active_connections, - config->max_body_size / (1024 * 1024), - (size_t)config->read_buffer_size / 1024 + config->connection_timeout, + (size_t)UVHTTP_MAX_BODY_SIZE / (1024 * 1024) ); uvhttp_response_set_status(response, 200); @@ -145,13 +145,15 @@ int main() { int cleanup_needed = 1; /* Set values directly (no defaults applied first). */ + /* Every field below is read by the server: max_connections caps + * concurrent connections, backlog sizes the listen queue, and + * connection_timeout drives the per-connection idle timer + * (connection.c). Limits that look configurable but are not were + * removed in v2.10 - body/header/URL/read-buffer sizes are owned by + * the compile-time constants in uvhttp_constants.h. */ config->max_connections = 500; - config->max_requests_per_connection = 100; config->backlog = 128; - config->max_body_size = 1048576; /* 1MB */ - config->read_buffer_size = 8192; - config->keepalive_timeout = 30; - config->request_timeout = 60; + config->connection_timeout = 60; if (uvhttp_config_validate(config) != UVHTTP_OK) { fprintf(stderr, "Configuration validation failed\n"); @@ -164,10 +166,8 @@ int main() { printf("Configuration loaded successfully:\n"); printf(" Max connections: %d\n", config->max_connections); - printf(" Max requests per connection: %d\n", - config->max_requests_per_connection); - printf(" Max body size: %zu bytes\n", config->max_body_size); - printf(" Read buffer size: %d bytes\n", config->read_buffer_size); + printf(" Backlog: %d\n", config->backlog); + printf(" Connection timeout: %d s\n", config->connection_timeout); /* Get the default loop. */ uv_loop_t* loop = uv_default_loop(); diff --git a/examples/06_advanced/config_demo.c b/examples/06_advanced/config_demo.c index 447373d0..8fc37150 100644 --- a/examples/06_advanced/config_demo.c +++ b/examples/06_advanced/config_demo.c @@ -79,20 +79,18 @@ int demo_handler(uvhttp_request_t* request, uvhttp_response_t* response) { "
请求时间: %s
" "