FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
blocking_http.hpp
Go to the documentation of this file.
1#pragma once
2// BlockingHttp: synchronous HTTP(S) requests on a private Reactor + HttpClient. Used only on
3// control paths that must not depend on the venue's reactor thread being alive: reference data at
4// startup, server-time probes, and the kill-switch cancel_all (6.7: "independent REST
5// connection"). Allocates freely; never touched by the hot path.
6//
7// One object keeps its connection between requests (HTTP/1.1 keep-alive) and the addresses it
8// looked up: the kill switch cancels symbol after symbol, and a connection per request paid a
9// lookup, a CA bundle load, the TCP and TLS handshakes and the request each time. The TLS context
10// is the process's (net::TlsContext::shared_client).
11// A connection idle for more than kReuseIdleMs is not reused, nor one the server has closed. A
12// request whose reused connection turns out to be gone before any answer is sent again on a new
13// one, unless it is a POST (it may have been carried out). Not thread-safe: one thread at a time.
14#include "fastmm/net/http_message.hpp"
15#include "fastmm/net/source_address.hpp"
16#include "fastmm/net/tcp_socket.hpp"
17
18#include <cstddef>
19#include <cstdint>
20#include <memory>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <utility>
25#include <vector>
26
27namespace fastmm::venues {
28
29struct HttpReply {
30 int status = 0;
31 std::string body;
32 std::vector<std::pair<std::string, std::string>> headers;
33 std::string error; // non-empty on transport failure (status == 0)
35 [[nodiscard]] bool ok() const noexcept { return error.empty() && status >= 200 && status < 300; }
36 [[nodiscard]] std::string_view header(std::string_view name) const noexcept {
37 for (const auto& [k, v] : headers) {
38 if (net::iequals(k, name)) return v;
39 }
40 return {};
41 }
42};
43
44struct BlockingHttpOptions {
45 std::string ca_file;
46 bool insecure_tls = false;
47 std::uint32_t timeout_ms = 5000;
48 std::uint32_t connect_timeout_ms = 5000;
49 // Largest response body. Binance USD-M's exchangeInfo has no symbol filter and was 1.1 MB on
50 // production in 2026-09 (1 MiB is the streaming client's default).
51 std::size_t recv_capacity = std::size_t{16} * 1024 * 1024;
52 net::SourceAddress source; // where the connection leaves this host from; empty: anywhere
53 // Endpoint selection (net/endpoint_selector.hpp): the fastest address first; nullopt: the
54 // process's default.
55 std::optional<bool> select_endpoint;
56};
58class BlockingHttp {
59 public:
60 // A connection idle this long is closed rather than reused: well inside the keep-alive timeout
61 // of the venues' front ends, so the server does not close it as the request goes out.
62 static constexpr std::int64_t kReuseIdleMs = 10'000;
63
64 explicit BlockingHttp(std::string base_url, BlockingHttpOptions opts = {});
66 BlockingHttp(const BlockingHttp&) = delete;
69 // `target` is the path plus query ("/api/v3/depth?symbol=BTCUSDT"); `extra_headers` is a
70 // pre-formatted "Name: value\r\n" block.
71 HttpReply request(std::string_view method,
72 std::string_view target,
73 std::string_view extra_headers = {},
74 std::string_view body = {});
75 HttpReply get(std::string_view target, std::string_view extra_headers = {}) {
76 return request("GET", target, extra_headers);
77 }
78
79 [[nodiscard]] const std::string& base_url() const noexcept { return base_url_; }
80 [[nodiscard]] std::string_view host() const noexcept { return host_; }
81 // Connections opened so far.
82 [[nodiscard]] std::uint64_t connects() const noexcept { return connects_; }
83
84 private:
85 struct Session; // the reactor, the TLS context and the open connection
86
87 // One attempt on the open connection, connecting first when there is none. `reused`: the
88 // connection was there before the attempt; `lost`: it closed or failed under the request.
89 HttpReply attempt(std::string_view method,
90 std::string_view target,
91 std::string_view extra_headers,
92 std::string_view body,
93 bool& reused,
94 bool& lost);
95 std::string connect(); // opens the connection; the error, empty on success
96
97 std::string base_url_;
98 std::string host_;
99 std::uint16_t port_ = 0;
100 bool tls_ = false;
101 std::string base_path_; // path prefix from the base URL ("" or "/api")
103 std::vector<net::SockAddr> addrs_; // looked up on first use, again after a failed connect
104 std::unique_ptr<Session> session_;
105 std::uint64_t connects_ = 0;
106};
107
108} // namespace fastmm::venues
Definition blocking_http.hpp:60
BlockingHttp & operator=(const BlockingHttp &)=delete
HttpReply get(std::string_view target, std::string_view extra_headers={})
Definition blocking_http.hpp:77
HttpReply request(std::string_view method, std::string_view target, std::string_view extra_headers={}, std::string_view body={})
target is the path plus query ("/api/v3/depth?symbol=BTCUSDT"); extra_headers is a pre-formatted "Nam...
std::uint64_t connects() const noexcept
Connections opened so far.
Definition blocking_http.hpp:84
static constexpr std::int64_t kReuseIdleMs
A connection idle this long is closed rather than reused: well inside the keep-alive timeout of the v...
Definition blocking_http.hpp:64
BlockingHttp(std::string base_url, BlockingHttpOptions opts={})
const std::string & base_url() const noexcept
Definition blocking_http.hpp:81
std::string_view host() const noexcept
Definition blocking_http.hpp:82
Definition venue.hpp:39
Definition blocking_http.hpp:46
std::string ca_file
Definition blocking_http.hpp:47
std::uint32_t timeout_ms
Definition blocking_http.hpp:49
std::optional< bool > select_endpoint
Endpoint selection (net/endpoint_selector.hpp): the fastest address first; nullopt: the process's def...
Definition blocking_http.hpp:57
std::size_t recv_capacity
Largest response body.
Definition blocking_http.hpp:53
bool insecure_tls
Definition blocking_http.hpp:48
net::SourceAddress source
where the connection leaves this host from; empty: anywhere
Definition blocking_http.hpp:54
std::uint32_t connect_timeout_ms
Definition blocking_http.hpp:50
Definition blocking_http.hpp:31
std::string_view header(std::string_view name) const noexcept
Definition blocking_http.hpp:38
int status
Definition blocking_http.hpp:32
bool ok() const noexcept
Definition blocking_http.hpp:37
std::vector< std::pair< std::string, std::string > > headers
Definition blocking_http.hpp:34
std::string body
Definition blocking_http.hpp:33
std::string error
non-empty on transport failure (status == 0)
Definition blocking_http.hpp:35