A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
connection_pool.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
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
35#ifndef A11_NET_HTTP_CONNECTION_POOL_H_
36#define A11_NET_HTTP_CONNECTION_POOL_H_
37
38#include <memory>
39#include <string>
40#include <string_view>
41#include <utility>
42
43#include <absl/container/flat_hash_map.h>
44#include <absl/status/statusor.h>
45
47#include "a11/net/http/url.h"
48#include "a11/net/http2.h"
50
51namespace a11::net {
52
53class HttpConnectionPool;
54
69 public:
71
73 [[nodiscard]] const std::shared_ptr<Http2Client>& client() const {
74 return client_;
75 }
76
78 [[nodiscard]] bool reused() const { return reused_; }
79
81 [[nodiscard]] bool multiplexed() const;
82
86 void Release() { client_.reset(); }
87
88 private:
89 friend class HttpConnectionPool;
90
91 HttpConnectionLease(std::shared_ptr<Http2Client> client, bool reused)
92 : client_(std::move(client)), reused_(reused) {}
93
94 std::shared_ptr<Http2Client> client_;
95 bool reused_ = false;
96};
97
107 : public std::enable_shared_from_this<HttpConnectionPool> {
108 public:
110 static std::shared_ptr<HttpConnectionPool> Create();
118 static const std::shared_ptr<HttpConnectionPool>& Shared();
119
137
151 const ParsedUrl& origin, Http2Options options);
152
154 [[nodiscard]] size_t size() const;
155
156 private:
158 struct Entry {
160 std::weak_ptr<Http2Client> client;
162 std::shared_ptr<a11::Promise<std::shared_ptr<Http2Client>>> dialling;
164 };
165
168 static std::string KeyFor(const ParsedUrl& origin,
169 const Http2Options& options);
170
172 void Forget(std::string_view key);
173
175 std::shared_ptr<Http2Client> Adopt(std::string key,
176 std::shared_ptr<Http2Client> client);
177
178 mutable thread::Mutex mu_;
179 absl::flat_hash_map<std::string, Entry> entries_ ABSL_GUARDED_BY(mu_);
180};
181
182} // namespace a11::net
183
184#endif // A11_NET_HTTP_CONNECTION_POOL_H_
Shared handle to one asynchronous result.
Definition future.h:125
A borrowed connection, held for as long as a caller needs it.
Definition connection_pool.h:68
bool multiplexed() const
Definition connection_pool.cc:73
const std::shared_ptr< Http2Client > & client() const
Definition connection_pool.h:73
bool reused() const
Definition connection_pool.h:78
void Release()
Releases this share of the connection early; the destructor does it too.
Definition connection_pool.h:86
Hands out connections to origins, sharing them while they are in use.
Definition connection_pool.h:107
size_t size() const
Definition connection_pool.cc:144
a11::Future< HttpConnectionLease > Acquire(const ParsedUrl &origin, Http2Options options)
Borrows a connection to origin, dialling one if needed.
Definition connection_pool.cc:156
static std::shared_ptr< HttpConnectionPool > Create()
Creates an independent pool.
Definition connection_pool.cc:77
static const std::shared_ptr< HttpConnectionPool > & Shared()
The process-wide pool.
Definition connection_pool.cc:83
static a11::Future< HttpConnectionLease > AcquireUnshared(const ParsedUrl &origin, Http2Options options)
Dials a connection nobody else will be given, and leases it.
Definition connection_pool.cc:127
Definition boost_primitives.h:21
std::string key
Definition discover.cc:764
Completion values used by every asynchronous A11 operation.
A11's nghttp2 HTTP/2 client, server, and streaming primitives.
Definition action.h:66
stores::ChunkStoreReaderOptions options
Definition runtime_streams.cc:31
Body-size limits, buffering thresholds, deadline, TLS, and HTTP protocol negotiation for an HTTP clie...
Definition http2.h:150
An absolute URL split into the parts a connection needs.
Definition url.h:50
Parsing and resolution for the URLs the HTTP and WebSocket transports dial.