kalshi-cpp 0.6.2
C++23 client for Kalshi's Predictions API
Loading...
Searching...
No Matches
retry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "kalshi/error.hpp"
5
6#include <chrono>
7#include <cstdint>
8#include <memory>
9#include <string_view>
10
11namespace kalshi {
12
14 std::chrono::milliseconds initial_delay{100};
15 std::chrono::milliseconds max_delay{std::chrono::seconds{10}};
16 double backoff_multiplier{2.0};
18 double jitter_factor{0.1};
20 std::uint8_t max_attempts{3};
29 bool retry_writes{false};
30};
31
32[[nodiscard]] bool should_retry(HttpMethod method, const HttpResponse& response,
33 const RetryPolicy& policy) noexcept;
34[[nodiscard]] bool should_retry(HttpMethod method, const Error& error,
35 const RetryPolicy& policy) noexcept;
36
38[[nodiscard]] std::chrono::milliseconds retry_delay(std::uint8_t attempt,
39 const RetryPolicy& policy);
40
46class RetryingTransport final : public HttpTransport {
47public:
48 explicit RetryingTransport(std::shared_ptr<const HttpTransport> inner, RetryPolicy policy = {});
49
50 [[nodiscard]] Result<HttpResponse> request(HttpMethod method, std::string_view path,
51 std::string_view body = {}) const override;
52
53 [[nodiscard]] const RetryPolicy& policy() const noexcept { return policy_; }
54
55private:
56 std::shared_ptr<const HttpTransport> inner_;
57 RetryPolicy policy_;
58};
59
60} // namespace kalshi
Request boundary used by KalshiClient.
Definition http_client.hpp:62
Transport decorator that retries transient failures with exponential backoff.
Definition retry.hpp:46
Result< HttpResponse > request(HttpMethod method, std::string_view path, std::string_view body={}) const override
path is relative to the base URL and may carry a query string.
RetryingTransport(std::shared_ptr< const HttpTransport > inner, RetryPolicy policy={})
const RetryPolicy & policy() const noexcept
Definition retry.hpp:53
Definition api.hpp:15
bool should_retry(HttpMethod method, const HttpResponse &response, const RetryPolicy &policy) noexcept
std::chrono::milliseconds retry_delay(std::uint8_t attempt, const RetryPolicy &policy)
Backoff before the attempt after attempt (1-based), with jitter applied.
std::expected< T, Error > Result
Result type for SDK operations.
Definition error.hpp:50
HttpMethod
HTTP methods.
Definition http_client.hpp:20
Definition error.hpp:23
Definition http_client.hpp:36
Definition retry.hpp:13
std::uint8_t max_attempts
Total attempts, including the first.
Definition retry.hpp:20
double jitter_factor
Random spread applied to each delay, as a fraction of it. 0 disables jitter.
Definition retry.hpp:18
bool retry_on_server_error
Definition retry.hpp:23
bool retry_writes
Also retry POST, PUT, and DELETE after network errors and 5xx responses.
Definition retry.hpp:29
std::chrono::milliseconds max_delay
Definition retry.hpp:15
std::chrono::milliseconds initial_delay
Definition retry.hpp:14
bool retry_on_network_error
Definition retry.hpp:21
double backoff_multiplier
Definition retry.hpp:16
bool retry_on_rate_limit
Definition retry.hpp:22