github-actions[bot] commented on code in PR #68816: URL: https://github.com/apache/doris/pull/68816#discussion_r4253824526
########## be/src/exprs/function/ai/ai_request_executor.cpp: ########## @@ -0,0 +1,300 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "exprs/function/ai/ai_request_executor.h" + +#include <algorithm> +#include <charconv> +#include <chrono> +#include <limits> +#include <mutex> +#include <thread> +#include <vector> + +#include "exprs/function/ai/ai_adapter.h" +#include "runtime/query_context.h" +#include "service/http/http_client.h" +#include "util/defer_op.h" +#include "util/security.h" + +namespace doris { +// Borrowing removes a client from idle, ensuring exclusive use of its easy handle. +struct AIRequestExecutor::ClientCache { + std::unique_ptr<HttpClient> borrow() { + { + std::lock_guard lock(mutex); + if (!idle.empty()) { + auto client = std::move(idle.back()); + idle.pop_back(); + return client; + } + } + return std::make_unique<HttpClient>(); + } + + void release(std::unique_ptr<HttpClient> client) { + // Bound idle clients, not TCP connections or in-flight requests. + // Excess clients are destroyed after releasing the lock. + constexpr size_t max_idle_clients = 8; + std::lock_guard lock(mutex); + if (idle.size() < max_idle_clients) { + idle.push_back(std::move(client)); Review Comment: [P2] Release the copied POST body before caching an idle client. `HttpClient::set_payload` uses `CURLOPT_COPYPOSTFIELDS`, so parking this handle retains the entire last request body until its next `init()` or query teardown. Scalar/EMBED batching permits an over-window input as a singleton, and `ai_context_window_size` has no upper bound; for example, 32 scalar task contexts ending on a 32 MiB LOCAL request can retain about 1 GiB while downstream operators continue. The eight-client limit does not bound these bytes, and [libcurl's malloc copies](https://github.com/curl/curl/blob/curl-8_2_1/lib/setopt.c#L502-L526) are not charged to the query MemTracker. Clear/reset request-owned payload before returning the client to idle; [`curl_easy_reset`](https://curl.se/libcurl/c/curl_easy_reset.html) frees the copied options while preserving live connections. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
