![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
An asynchronous HTTP/1.1 client over TLS that keeps one connection to one host, used by the broker clients. More...
#include <algotradelib/src/network/http_client.hpp>
Public Types | |
| using | CompletionHandler = net::any_completion_handler<void(Result<HttpResponse>)> |
| The completion handler type: called once with the response or an error. | |
Public Member Functions | |
| AsyncHttpClient (net::any_io_executor executor, ssl::context &context, std::string host, std::string port, HttpClientOptions options={}) | |
| Constructs a client; it connects on the first request. | |
| RateLimitController * | rate_limits () noexcept |
| Returns the connection's rate limiter. | |
| const RateLimitController * | rate_limits () const noexcept |
| Returns the connection's rate limiter (read-only). | |
| std::uint64_t | admission_waits () const noexcept |
| Returns how many times the client started a rate-limit admission wait. | |
| template<typename CompletionToken> | |
| auto | async_request (HttpRequest request_value, CompletionToken &&token) |
| Queues a request; completes with the response (any status) or an error. | |
| void | close () |
| Closes the client: cancels the connection and completes every queued request with CANCELLED. Later requests fail at once. | |
An asynchronous HTTP/1.1 client over TLS that keeps one connection to one host, used by the broker clients.
Requests are queued by priority (HttpRequest::group_, then arrival) and sent one at a time on a strand. Each completion runs on the handler's associated executor, or on the client's strand if it has none.
A GET that fails on the connection is sent once more; any other request never is. Before a request other than a GET is admitted, a connection idle longer than HttpClientOptions::unreplayed_idle_limit_ is replaced by a fresh one, once per request: if the request then waits for rate-limit admission longer than that limit, it is still written on the fresh connection. With a rate-limit budget, a request waits for admission, and fails with admission_deadline if the wait would exceed the timeout.
A failed request's BrokerError::reached_broker_ is false only if no attempt at it began writing (a failure while resolving, connecting or handshaking, or a refusal in the queue). true means a write was begun, by this attempt or an earlier one (a retried GET), so the broker may have received it; it does not prove it did.
Create it with std::make_shared: it uses shared_from_this.
Definition at line 367 of file http_client.hpp.
| using algotradelib::network::AsyncHttpClient::CompletionHandler = net::any_completion_handler<void(Result<HttpResponse>)> |
The completion handler type: called once with the response or an error.
Definition at line 370 of file http_client.hpp.
|
inline |
Constructs a client; it connects on the first request.
| executor | The executor its strand runs on. |
| context | The TLS context; borrowed, and must outlive the client. The client installs SNI and a host-name check; for the peer to be verified, the context must enable ssl::verify_peer and hold trusted roots. |
| host | The host name (also used for SNI and certificate verification). |
| port | The port. |
| options | The options. |
| std::invalid_argument | if options.rate_limit_ has a burst or rate below 1. |
Definition at line 383 of file http_client.hpp.
|
inlinenoexcept |
Returns how many times the client started a rate-limit admission wait.
It counts waits started, not requests delayed (a request can wait more than once); for delayed requests read RateLimitStats::delayed_. Always zero for a client without a budget. Read it on the client's strand or after the event loop has stopped.
Definition at line 430 of file http_client.hpp.
|
inline |
Queues a request; completes with the response (any status) or an error.
Errors: queue_full (unsent) when the queue is full; CANCELLED if the client is closed or the admission wait would exceed the timeout (admission_deadline); TRANSPORT or CANCELLED from the connection. See the class description for reached_broker_. The handler runs on its associated executor, or on the client's strand if it has none.
| CompletionToken | An Asio completion token. |
| request_value | The request. |
| token | The completion token, called with Result<HttpResponse>. |
Definition at line 446 of file http_client.hpp.
|
inline |
Closes the client: cancels the connection and completes every queued request with CANCELLED. Later requests fail at once.
Definition at line 465 of file http_client.hpp.
|
inlinenoexcept |
Returns the connection's rate limiter (read-only).
Not synchronized: read it on the client's strand, or while no request is in progress.
Definition at line 417 of file http_client.hpp.
|
inlinenoexcept |
Returns the connection's rate limiter.
It is not synchronized: use it on the client's strand, or while no request is in progress. A completion handler bound to another executor is not on the strand.
Definition at line 406 of file http_client.hpp.