pub struct RateLimitedKrakenClient<C>where
C: KrakenClient,{ /* private fields */ }
Expand description
A KrakenClient implementation that decorates a provided client, and applies rate limiting according to the Kraken API specs.
Loosely, this is:
- public endpoints are limited to 1 call per second
- private endpoints follow a token-bucket rate limiting scheme, with some endpoints having higher costs
- trading endpoints implement the Advanced version of Kraken’s rate limiting scheme
- this includes tracking order lifetimes and applying penalties to rapid cancels and edits of orders
The exact rate limit values and replenishment schedule are determined by a user’s
verification tier. Default new methods assume an Intermediate
verification, so Pro
users will
want to rely on methods that allow providing a custom verification tier if they want to take full
advantage of their increased rate limits (e.g. new_with_verification_tier
).
Calls made that violate the rate limiting policy are made to wait asynchronously, but no error handling is in place for receiving rate limit errors, these are to be handled/backed-off by the user.
Detailed documentation is available from several locations, including the overview rate-limiting page, api rate-limiting page and trading rate-limiting page. It’s worth noting that the token values used in this library are scaled to be 100x those of Kraken’s documentation to keep them as integers using semaphore permits instead of floating-point math.
RateLimitedKrakenClient
s are cloneable, which results in a new client that shares the same
rate limiting state. This is useful for giving many services access to a client while ensuring
that all will jointly respect the rate limits of the exchange.
Warning: This is not meant to be a comprehensive solution to all rate limiting, but is a best-effort attempt to match the API’s specifications. In some cases cloud providers, or server implementations may inject random errors to prevent coordinated attacks or abuse. As such, this cannot anticipate and mitigate all modes of failure.
See examples/live_retrieving_recent_trades.rs for usage that relies on rate limiting preventing request failures due to rapidly requesting public trade history.
Implementations§
Source§impl<C> RateLimitedKrakenClient<C>where
C: KrakenClient,
impl<C> RateLimitedKrakenClient<C>where
C: KrakenClient,
Sourcepub fn new_with_client(
client: C,
verification: VerificationTier,
) -> RateLimitedKrakenClient<C>
pub fn new_with_client( client: C, verification: VerificationTier, ) -> RateLimitedKrakenClient<C>
Create a new rate limited client that delegates calls to any type that implements KrakenClient.
Sourcepub fn new_with_verification_tier(
secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>,
nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>,
verification: VerificationTier,
) -> Self
pub fn new_with_verification_tier( secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>, nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>, verification: VerificationTier, ) -> Self
Create a new rate-limited client using the provided SecretsProvider and NonceProvider
Sourcepub fn new_with_verification_tier_and_url(
secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>,
nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>,
url: String,
verification: VerificationTier,
) -> Self
pub fn new_with_verification_tier_and_url( secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>, nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>, url: String, verification: VerificationTier, ) -> Self
Create a new client, specifying the user’s verification tier and the base URL.
Sourcepub fn get_private_rate_limiter(
user_verification: VerificationTier,
) -> TokenBucketRateLimiter
pub fn get_private_rate_limiter( user_verification: VerificationTier, ) -> TokenBucketRateLimiter
Get a private endpoint rate limiter, depending on the user’s verification level.
This implements a more involved scheme.
Sourcepub fn get_public_rate_limiter() -> SlidingWindowRateLimiter
pub fn get_public_rate_limiter() -> SlidingWindowRateLimiter
Get a public rate limiter, which limits calls to 1 per second.
Trait Implementations§
Source§impl<C> Clone for RateLimitedKrakenClient<C>where
C: KrakenClient + Clone,
impl<C> Clone for RateLimitedKrakenClient<C>where
C: KrakenClient + Clone,
Source§fn clone(&self) -> RateLimitedKrakenClient<C>
fn clone(&self) -> RateLimitedKrakenClient<C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<C> Debug for RateLimitedKrakenClient<C>where
C: KrakenClient + Debug,
impl<C> Debug for RateLimitedKrakenClient<C>where
C: KrakenClient + Debug,
Source§impl<C> KrakenClient for RateLimitedKrakenClient<C>where
C: KrakenClient,
impl<C> KrakenClient for RateLimitedKrakenClient<C>where
C: KrakenClient,
Source§async fn cancel_order_batch(
&mut self,
request: &CancelBatchOrdersRequest,
) -> Result<ResultErrorResponse<CancelOrder>, ClientError>
async fn cancel_order_batch( &mut self, request: &CancelBatchOrdersRequest, ) -> Result<ResultErrorResponse<CancelOrder>, ClientError>
Clients can request to cancel in batches using both ref-ids produced by Kraken (Strings), or user-refs generated by the user (i64), which are known before the order is placed.
Source§fn new(
secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>,
nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>,
) -> RateLimitedKrakenClient<C>
fn new( secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>, nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>, ) -> RateLimitedKrakenClient<C>
Source§fn new_with_url(
secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>,
nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>,
url: String,
) -> Self
fn new_with_url( secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>, nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>, url: String, ) -> Self
Source§fn new_with_tracing(
secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>,
nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>,
trace_inbound: bool,
) -> Self
fn new_with_tracing( secrets_provider: Box<Arc<Mutex<dyn SecretsProvider>>>, nonce_provider: Box<Arc<Mutex<dyn NonceProvider>>>, trace_inbound: bool, ) -> Self
Source§async fn set_user_agent(&mut self, user_agent: String)
async fn set_user_agent(&mut self, user_agent: String)
Source§async fn get_server_time(
&mut self,
) -> Result<ResultErrorResponse<SystemTime>, ClientError>
async fn get_server_time( &mut self, ) -> Result<ResultErrorResponse<SystemTime>, ClientError>
Source§async fn get_system_status(
&mut self,
) -> Result<ResultErrorResponse<SystemStatusInfo>, ClientError>
async fn get_system_status( &mut self, ) -> Result<ResultErrorResponse<SystemStatusInfo>, ClientError>
Source§async fn get_asset_info(
&mut self,
request: &AssetInfoRequest,
) -> Result<ResultErrorResponse<HashMap<String, AssetInfo>>, ClientError>
async fn get_asset_info( &mut self, request: &AssetInfoRequest, ) -> Result<ResultErrorResponse<HashMap<String, AssetInfo>>, ClientError>
Source§async fn get_tradable_asset_pairs(
&mut self,
request: &TradableAssetPairsRequest,
) -> Result<ResultErrorResponse<HashMap<String, TradableAssetPair>>, ClientError>
async fn get_tradable_asset_pairs( &mut self, request: &TradableAssetPairsRequest, ) -> Result<ResultErrorResponse<HashMap<String, TradableAssetPair>>, ClientError>
Source§async fn get_ticker_information(
&mut self,
request: &TickerRequest,
) -> Result<ResultErrorResponse<HashMap<String, RestTickerInfo>>, ClientError>
async fn get_ticker_information( &mut self, request: &TickerRequest, ) -> Result<ResultErrorResponse<HashMap<String, RestTickerInfo>>, ClientError>
Source§async fn get_ohlc(
&mut self,
request: &OHLCRequest,
) -> Result<ResultErrorResponse<OhlcResponse>, ClientError>
async fn get_ohlc( &mut self, request: &OHLCRequest, ) -> Result<ResultErrorResponse<OhlcResponse>, ClientError>
Source§async fn get_orderbook(
&mut self,
request: &OrderbookRequest,
) -> Result<ResultErrorResponse<HashMap<String, Orderbook>>, ClientError>
async fn get_orderbook( &mut self, request: &OrderbookRequest, ) -> Result<ResultErrorResponse<HashMap<String, Orderbook>>, ClientError>
Source§async fn get_recent_trades(
&mut self,
request: &RecentTradesRequest,
) -> Result<ResultErrorResponse<RecentTrades>, ClientError>
async fn get_recent_trades( &mut self, request: &RecentTradesRequest, ) -> Result<ResultErrorResponse<RecentTrades>, ClientError>
Source§async fn get_recent_spreads(
&mut self,
request: &RecentSpreadsRequest,
) -> Result<ResultErrorResponse<RecentSpreads>, ClientError>
async fn get_recent_spreads( &mut self, request: &RecentSpreadsRequest, ) -> Result<ResultErrorResponse<RecentSpreads>, ClientError>
Source§async fn get_account_balance(
&mut self,
) -> Result<ResultErrorResponse<AccountBalances>, ClientError>
async fn get_account_balance( &mut self, ) -> Result<ResultErrorResponse<AccountBalances>, ClientError>
Source§async fn get_extended_balances(
&mut self,
) -> Result<ResultErrorResponse<ExtendedBalances>, ClientError>
async fn get_extended_balances( &mut self, ) -> Result<ResultErrorResponse<ExtendedBalances>, ClientError>
Source§async fn get_trade_balances(
&mut self,
request: &TradeBalanceRequest,
) -> Result<ResultErrorResponse<TradeBalances>, ClientError>
async fn get_trade_balances( &mut self, request: &TradeBalanceRequest, ) -> Result<ResultErrorResponse<TradeBalances>, ClientError>
Source§async fn get_open_orders(
&mut self,
request: &OpenOrdersRequest,
) -> Result<ResultErrorResponse<OpenOrders>, ClientError>
async fn get_open_orders( &mut self, request: &OpenOrdersRequest, ) -> Result<ResultErrorResponse<OpenOrders>, ClientError>
Source§async fn get_closed_orders(
&mut self,
request: &ClosedOrdersRequest,
) -> Result<ResultErrorResponse<ClosedOrders>, ClientError>
async fn get_closed_orders( &mut self, request: &ClosedOrdersRequest, ) -> Result<ResultErrorResponse<ClosedOrders>, ClientError>
Source§async fn query_orders_info(
&mut self,
request: &OrderRequest,
) -> Result<ResultErrorResponse<HashMap<String, Order>>, ClientError>
async fn query_orders_info( &mut self, request: &OrderRequest, ) -> Result<ResultErrorResponse<HashMap<String, Order>>, ClientError>
async fn get_order_amends( &mut self, request: &OrderAmendsRequest, ) -> Result<ResultErrorResponse<OrderAmends>, ClientError>
Source§async fn get_trades_history(
&mut self,
request: &TradesHistoryRequest,
) -> Result<ResultErrorResponse<TradesHistory>, ClientError>
async fn get_trades_history( &mut self, request: &TradesHistoryRequest, ) -> Result<ResultErrorResponse<TradesHistory>, ClientError>
Source§async fn query_trades_info(
&mut self,
request: &TradeInfoRequest,
) -> Result<ResultErrorResponse<TradesInfo>, ClientError>
async fn query_trades_info( &mut self, request: &TradeInfoRequest, ) -> Result<ResultErrorResponse<TradesInfo>, ClientError>
Source§async fn get_open_positions(
&mut self,
request: &OpenPositionsRequest,
) -> Result<ResultErrorResponse<OpenPositions>, ClientError>
async fn get_open_positions( &mut self, request: &OpenPositionsRequest, ) -> Result<ResultErrorResponse<OpenPositions>, ClientError>
Source§async fn get_ledgers_info(
&mut self,
request: &LedgersInfoRequest,
) -> Result<ResultErrorResponse<LedgerInfo>, ClientError>
async fn get_ledgers_info( &mut self, request: &LedgersInfoRequest, ) -> Result<ResultErrorResponse<LedgerInfo>, ClientError>
Source§async fn query_ledgers(
&mut self,
request: &QueryLedgerRequest,
) -> Result<ResultErrorResponse<QueryLedgerInfo>, ClientError>
async fn query_ledgers( &mut self, request: &QueryLedgerRequest, ) -> Result<ResultErrorResponse<QueryLedgerInfo>, ClientError>
Source§async fn get_trade_volume(
&mut self,
request: &TradeVolumeRequest,
) -> Result<ResultErrorResponse<TradeVolume>, ClientError>
async fn get_trade_volume( &mut self, request: &TradeVolumeRequest, ) -> Result<ResultErrorResponse<TradeVolume>, ClientError>
Source§async fn request_export_report(
&mut self,
request: &ExportReportRequest,
) -> Result<ResultErrorResponse<ExportReport>, ClientError>
async fn request_export_report( &mut self, request: &ExportReportRequest, ) -> Result<ResultErrorResponse<ExportReport>, ClientError>
Source§async fn get_export_report_status(
&mut self,
request: &ExportReportStatusRequest,
) -> Result<ResultErrorResponse<Vec<ExportReportStatus>>, ClientError>
async fn get_export_report_status( &mut self, request: &ExportReportStatusRequest, ) -> Result<ResultErrorResponse<Vec<ExportReportStatus>>, ClientError>
Source§async fn retrieve_export_report(
&mut self,
request: &RetrieveExportReportRequest,
) -> Result<Vec<u8>, ClientError>
async fn retrieve_export_report( &mut self, request: &RetrieveExportReportRequest, ) -> Result<Vec<u8>, ClientError>
Source§async fn delete_export_report(
&mut self,
request: &DeleteExportRequest,
) -> Result<ResultErrorResponse<DeleteExportReport>, ClientError>
async fn delete_export_report( &mut self, request: &DeleteExportRequest, ) -> Result<ResultErrorResponse<DeleteExportReport>, ClientError>
Source§async fn add_order(
&mut self,
request: &AddOrderRequest,
) -> Result<ResultErrorResponse<AddOrder>, ClientError>
async fn add_order( &mut self, request: &AddOrderRequest, ) -> Result<ResultErrorResponse<AddOrder>, ClientError>
Source§async fn add_order_batch(
&mut self,
request: &AddBatchedOrderRequest,
) -> Result<ResultErrorResponse<AddOrderBatch>, ClientError>
async fn add_order_batch( &mut self, request: &AddBatchedOrderRequest, ) -> Result<ResultErrorResponse<AddOrderBatch>, ClientError>
Vec
.