Android-cuttlefish cvd tool
http_client_pool.h
Go to the documentation of this file.
1//
2// Copyright (C) 2026 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <stddef.h>
19
20#include <condition_variable>
21#include <functional>
22#include <memory>
23#include <mutex>
24#include <stack>
25
28
29namespace cuttlefish {
30
31class HttpClientPool : public HttpClient {
32 public:
33 using CreateClient = std::function<Result<std::unique_ptr<HttpClient>>()>;
34
35 // max_clients = 0 implies an unlimited number of clients internally.
36 explicit HttpClientPool(CreateClient create, size_t max_clients = 0);
37
39 HttpRequest, ResultDataCallback callback) override;
40
41 private:
43 const size_t max_clients_;
44 std::condition_variable condition_variable_;
45 std::mutex mutex_;
46 std::stack<std::unique_ptr<HttpClient>> free_clients_;
48};
49
50} // namespace cuttlefish
Definition: http_client_pool.h:31
std::mutex mutex_
Definition: http_client_pool.h:45
const size_t max_clients_
Definition: http_client_pool.h:43
const CreateClient create_
Definition: http_client_pool.h:42
size_t clients_in_use_
Definition: http_client_pool.h:47
std::stack< std::unique_ptr< HttpClient > > free_clients_
Definition: http_client_pool.h:46
HttpClientPool(CreateClient create, size_t max_clients=0)
Definition: http_client_pool.cc:32
std::condition_variable condition_variable_
Definition: http_client_pool.h:44
Result< HttpResponse< void > > DownloadToCallback(HttpRequest, ResultDataCallback callback) override
Definition: http_client_pool.cc:43
std::function< Result< std::unique_ptr< HttpClient > >()> CreateClient
Definition: http_client_pool.h:33
Definition: http_client.h:100
std::function< Result< void >(char *, size_t)> ResultDataCallback
Definition: http_client.h:103
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
Definition: http_client.h:93