algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::AsyncHttpClient Class Reference

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>

Inheritance diagram for algotradelib::network::AsyncHttpClient:
Collaboration diagram for algotradelib::network::AsyncHttpClient:

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.

Detailed Description

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.

Member Typedef Documentation

◆ CompletionHandler

The completion handler type: called once with the response or an error.

Definition at line 370 of file http_client.hpp.

Constructor & Destructor Documentation

◆ AsyncHttpClient()

algotradelib::network::AsyncHttpClient::AsyncHttpClient ( net::any_io_executor executor,
ssl::context & context,
std::string host,
std::string port,
HttpClientOptions options = {} )
inline

Constructs a client; it connects on the first request.

Parameters
executorThe executor its strand runs on.
contextThe 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.
hostThe host name (also used for SNI and certificate verification).
portThe port.
optionsThe options.
Exceptions
std::invalid_argumentif options.rate_limit_ has a burst or rate below 1.

Definition at line 383 of file http_client.hpp.

Member Function Documentation

◆ admission_waits()

std::uint64_t algotradelib::network::AsyncHttpClient::admission_waits ( ) const
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.

Returns
The count.

Definition at line 430 of file http_client.hpp.

◆ async_request()

template<typename CompletionToken>
auto algotradelib::network::AsyncHttpClient::async_request ( HttpRequest request_value,
CompletionToken && token )
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.

Template Parameters
CompletionTokenAn Asio completion token.
Parameters
request_valueThe request.
tokenThe completion token, called with Result<HttpResponse>.
Returns
Whatever the completion token returns.

Definition at line 446 of file http_client.hpp.

◆ close()

void algotradelib::network::AsyncHttpClient::close ( )
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.

◆ rate_limits() [1/2]

const RateLimitController * algotradelib::network::AsyncHttpClient::rate_limits ( ) const
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.

Returns
The limiter, or null if the client has no budget.

Definition at line 417 of file http_client.hpp.

◆ rate_limits() [2/2]

RateLimitController * algotradelib::network::AsyncHttpClient::rate_limits ( )
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.

Returns
The limiter, or null if the client has no budget.

Definition at line 406 of file http_client.hpp.


The documentation for this class was generated from the following file: