Skip to content

Commit 31a5cf4

Browse files
authored
server: use httplib dynamic threads (#20817)
* server: use httplib dynamic threads * change to n_threads_http + 1024
1 parent e32d243 commit 31a5cf4

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

tools/server/server-http.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,17 @@ bool server_http_context::init(const common_params & params) {
227227

228228
int n_threads_http = params.n_threads_http;
229229
if (n_threads_http < 1) {
230-
// +2 threads for monitoring endpoints
231-
n_threads_http = std::max(params.n_parallel + 2, (int32_t) std::thread::hardware_concurrency() - 1);
230+
// +4 threads for monitoring, health and some threads reserved for MCP and other tasks in the future
231+
n_threads_http = std::max(params.n_parallel + 4, (int32_t) std::thread::hardware_concurrency() - 1);
232232
}
233233
LOG_INF("%s: using %d threads for HTTP server\n", __func__, n_threads_http);
234-
srv->new_task_queue = [n_threads_http] { return new httplib::ThreadPool(n_threads_http); };
234+
srv->new_task_queue = [n_threads_http] {
235+
// spawn n_threads_http fixed thread (always alive), while allow up to 1024 max possible additional threads
236+
// when n_threads_http is used, server will create new "dynamic" threads that will be destroyed after processing each request
237+
// ref: https://github.com/yhirose/cpp-httplib/pull/2368
238+
size_t max_threads = (size_t)n_threads_http + 1024;
239+
return new httplib::ThreadPool(n_threads_http, max_threads);
240+
};
235241

236242
//
237243
// Web UI setup

0 commit comments

Comments
 (0)