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 <utility>
41
42#include <absl/container/flat_hash_map.h>
43#include <absl/status/statusor.h>
44
46#include "a11/net/http/url.h"
47#include "a11/net/http2.h"
48#include "thread/boost_primitives.h"
49
50namespace a11::net {
51
52class HttpConnectionPool;
53
68 public:
70
72 [[nodiscard]] const std::shared_ptr<Http2Client>& client() const {
73 return client_;
74 }
75
77 [[nodiscard]] bool reused() const { return reused_; }
78
80 [[nodiscard]] bool multiplexed() const;
81
85 void Release() { client_.reset(); }
86
87 private:
88 friend class HttpConnectionPool;
89
90 HttpConnectionLease(std::shared_ptr<Http2Client> client, bool reused)
91 : client_(std::move(client)), reused_(reused) {}
92
93 std::shared_ptr<Http2Client> client_;
94 bool reused_ = false;
95};
96
106 : public std::enable_shared_from_this<HttpConnectionPool> {
107 public:
109 static std::shared_ptr<HttpConnectionPool> Create();
117 static const std::shared_ptr<HttpConnectionPool>& Shared();
118
135 Http2Options options);
136
150 const ParsedUrl& origin, Http2Options options);
151
153 [[nodiscard]] size_t size() const;
154
155 private:
157 struct Entry {
159 std::weak_ptr<Http2Client> client;
161 std::shared_ptr<a11::Promise<std::shared_ptr<Http2Client>>> dialling;
163 };
164
167 static std::string KeyFor(const ParsedUrl& origin,
168 const Http2Options& options);
169
171 void Forget(const std::string& key);
172
174 std::shared_ptr<Http2Client> Adopt(std::string key,
175 std::shared_ptr<Http2Client> client);
176
177 mutable thread::Mutex mu_;
178 absl::flat_hash_map<std::string, Entry> entries_ ABSL_GUARDED_BY(mu_);
179};
180
181} // namespace a11::net
182
183#endif // A11_NET_HTTP_CONNECTION_POOL_H_
Shared handle to one asynchronous result.
Definition future.h:126
A borrowed connection, held for as long as a caller needs it.
Definition connection_pool.h:67
bool multiplexed() const
Definition connection_pool.cc:73
const std::shared_ptr< Http2Client > & client() const
Definition connection_pool.h:72
bool reused() const
Definition connection_pool.h:77
void Release()
Releases this share of the connection early; the destructor does it too.
Definition connection_pool.h:85
Hands out connections to origins, sharing them while they are in use.
Definition connection_pool.h:106
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
std::string key
Definition discover.cc:782
Completion values used by every asynchronous A11 operation.
A11's nghttp2 HTTP/2 client, server, and streaming primitives.
Definition action.h:65
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.