91 : reactor_(reactor), cfg_(std::move(cfg)), resolver_(reactor) {
93 if (!url)
throw std::invalid_argument(
"RestChannel: bad base url '" + cfg_.
base_url +
"'");
94 host_ = std::string(url->host);
97 prefix_ = url->path ==
"/" ? std::string{} : std::string(url->path);
98 while (!prefix_.empty() && prefix_.back() ==
'/') prefix_.pop_back();
100 if (net::EndpointSelector& sel = net::EndpointSelector::instance();
102 endpoints_ = sel.host(host_, port_, cfg_.
source);
106 cancel_resolve_timer();
115 bool request(std::string_view method,
116 std::string_view target,
117 std::string_view extra_headers,
118 std::string_view body,
122 p.target = prefix_ + std::string(target);
123 return enqueue(lane, method, extra_headers, body, std::move(p), std::move(cb));
129 bool request(std::string_view method,
131 std::string_view extra_headers,
132 std::string_view body,
135 if (!make_target)
return false;
137 p.make_target = [prefix = prefix_, make = std::move(make_target)] {
138 std::string target = make();
139 return target.empty() ? target : prefix + target;
141 return enqueue(lane, method, extra_headers, body, std::move(p), std::move(cb));
146 void reset() noexcept {
147 for (Lane& l : lanes_) {
148 std::deque<Pending> waiting;
149 waiting.swap(l.waiting);
150 for (Pending& p : waiting) fail(p, net::NetError::
Canceled,
"closed");
162 const Lane& l = lanes_[index(lane)];
163 return l.waiting.size() + (l.busy ? 1 : 0);
168 const Lane& l = lanes_[index(lane)];
169 return l.plain !=
nullptr || l.tls !=
nullptr;
171 [[nodiscard]] std::uint64_t
requests() const noexcept {
return requests_; }
172 [[nodiscard]] std::uint64_t
errors() const noexcept {
return errors_; }
173 [[nodiscard]] std::uint64_t
connects() const noexcept {
return connects_; }
174 [[nodiscard]] std::uint64_t
lookups() const noexcept {
return lookups_; }
175 [[nodiscard]]
const std::string&
host() const noexcept {
return host_; }
176 [[nodiscard]]
bool tls() const noexcept {
return use_tls_; }
179 static constexpr std::size_t kLanes = 2;
191 std::deque<Pending> waiting;
192 std::unique_ptr<PlainClient> plain;
193 std::unique_ptr<TlsClient> tls;
198 static constexpr std::size_t index(
RestLane lane)
noexcept {
199 return static_cast<std::size_t
>(lane);
203 std::string_view method,
204 std::string_view headers,
205 std::string_view body,
215 p.cb = std::move(cb);
216 Lane& l = lanes_[index(lane)];
217 l.waiting.push_back(std::move(p));
223 const bool ready = (l.plain && l.plain->is_ready()) || (l.tls && l.tls->is_ready());
224 if (ready && !l.busy) {
226 }
else if (!l.busy) {
232 void fail(Pending& p, net::NetError err, std::string_view detail) {
236 r.error_detail = detail;
242 void fail_waiting(Lane& l, net::NetError err, std::string_view detail) {
243 std::deque<Pending> waiting;
244 waiting.swap(l.waiting);
245 for (Pending& p : waiting) fail(p, err, detail);
248 void schedule_pump() {
249 if (pump_pending_)
return;
250 pump_pending_ =
true;
251 std::weak_ptr<int> alive = alive_;
252 reactor_.
post([
this, alive] {
253 if (alive.expired())
return;
254 pump_pending_ =
false;
264 Lane& l = lanes_[index(lane)];
265 if (l.busy || l.waiting.empty())
return;
268 if (!l.plain && !l.tls && !open(lane))
return;
269 Pending p = std::move(l.waiting.front());
270 l.waiting.pop_front();
272 auto cb = [
this, lane, alive = std::weak_ptr<int>(alive_), user = std::move(p.cb)](
273 const net::HttpResponse& r) {
274 if (alive.expired())
return;
277 const bool taken = p.make_target ? hand(l, p.method, std::move(p.make_target), p, cb)
278 : hand(l, p.method, std::string_view(p.target), p, cb);
283 r.error = net::NetError::InvalidState;
284 r.error_detail =
"connection refused the request";
289 template <
class Target,
class Cb>
290 bool hand(Lane& l, std::string_view method, Target&& target,
const Pending& p, Cb& cb) {
292 return l.plain->request(method, std::forward<Target>(target), p.headers, p.body, cb);
293 return l.tls->request(method, std::forward<Target>(target), p.headers, p.body, cb);
297 void done(
RestLane lane,
const net::HttpResponse& r,
const Callback& user) {
298 Lane& l = lanes_[index(lane)];
300 if (r.error != net::NetError::None) {
306 const bool established =
307 (l.plain && l.plain->established()) || (l.tls && l.tls->established());
308 if (closed && !established && r.error != net::NetError::Canceled) {
310 if (endpoints_) endpoints_->failed(l.addr);
312 fail_waiting(l, r.error, r.error_detail);
324 Lane& l = lanes_[index(lane)];
325 if (addrs_.empty() || addrs_stale_) {
330 net::SockAddr addr = addrs_[addr_index_++ % addrs_.size()];
332 if (
const std::optional<net::EndpointChoice> c = endpoints_->acquire(
false)) {
334 if (!same_peer(addr, logged_)) {
336 net::log_endpoint_choice(
342 const std::optional<net::SockAddr> local = net::source_bind_address(cfg_.
source, addr.family());
343 if (sock.connect(addr, local ? &*local : nullptr) == net::ConnectStatus::
Error) {
345 if (endpoints_) endpoints_->failed(addr);
346 fail_waiting(l, net::NetError::Syscall,
"connect failed");
349 sock.set_nodelay(
true);
350 net::HttpClientConfig hc;
354 bool started =
false;
356 l.tls = std::make_unique<TlsClient>(
358 net::TlsStream<net::PlainStream>(*tls_ctx_, net::PlainStream(std::move(sock)), host_),
363 started = l.tls->start();
364 if (!started) l.tls.reset();
366 l.plain = std::make_unique<PlainClient>(
367 reactor_, net::PlainStream(std::move(sock)), host_, port_,
false, hc);
368 started = l.plain->start();
369 if (!started) l.plain.reset();
372 fail_waiting(l, net::NetError::Syscall,
"connection not registered");
380 if (endpoints_) endpoints_->failed(addr);
381 fail_waiting(l, net::NetError::Syscall,
"connect failed");
389 void start_resolve() {
390 if (resolving_)
return;
393 std::weak_ptr<int> alive = alive_;
394 auto done = [
this, alive](net::ResolveResult r) {
395 if (alive.expired())
return;
397 cancel_resolve_timer();
399 net::keep_reachable(cfg_.
source, r.addrs);
400 if (!r.addrs.empty()) addrs_ = std::move(r.addrs);
402 if (addrs_.empty()) {
403 const std::string_view why =
404 r.ok() ?
"no address of the source's family" :
"host lookup failed";
405 for (Lane& l : lanes_) fail_waiting(l, net::NetError::Resolve, why);
409 addrs_stale_ =
false;
414 resolver_.resolve(endpoints_, std::move(done));
416 resolver_.resolve(host_, port_, std::move(done));
420 static bool same_peer(
const net::SockAddr& a,
const net::SockAddr& b)
noexcept {
421 return a.len == b.len && std::memcmp(&a.storage, &b.storage, a.len) == 0;
426 void wait_for_lookup() {
431 resolve_timer_ = net::kInvalidTimer;
432 if (!resolving_) return;
433 for (Lane& l : lanes_) fail_waiting(l, net::NetError::Timeout,
"host lookup timed out");
437 void cancel_resolve_timer() noexcept {
444 net::Reactor& reactor_;
445 RestChannelConfig cfg_;
447 std::uint16_t port_ = 0;
448 bool use_tls_ =
false;
450 std::shared_ptr<const net::TlsContext> tls_ctx_;
451 std::array<Lane, kLanes> lanes_;
452 net::AsyncResolver resolver_;
453 std::shared_ptr<net::EndpointHost> endpoints_;
454 net::SockAddr logged_;
455 std::vector<net::SockAddr> addrs_;
456 std::size_t addr_index_ = 0;
457 bool addrs_stale_ =
false;
458 bool resolving_ =
false;
460 bool pump_pending_ =
false;
461 std::shared_ptr<int> alive_ = std::make_shared<int>(0);
462 std::uint64_t requests_ = 0;
463 std::uint64_t errors_ = 0;
464 std::uint64_t connects_ = 0;
465 std::uint64_t lookups_ = 0;