kraky 0.1.2

Lightweight, production-ready Rust SDK for Kraken Exchange WebSocket API v2 with unique orderbook imbalance detection and WebSocket trading
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Subscription stream handling with backpressure control.
//!
//! This module provides the [`Subscription`] type, which represents a stream of updates
//! from a Kraken WebSocket channel. Subscriptions implement the `Stream` trait from
//! `futures` and can be used with async/await.
//!
//! # Backpressure Control
//!
//! Subscriptions use **bounded channels** to prevent memory issues during high message throughput.
//! If your application cannot process messages fast enough, messages will be dropped and
//! you can monitor the drop rate using [`SubscriptionStats`].
//!
//! Default buffer size: 1000 messages
//!
//! # Example Usage
//!
//! ```no_run
//! # #[cfg(feature = "orderbook")]
//! # {
//! use kraky::KrakyClient;
//! use futures_util::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = KrakyClient::connect().await?;
//!
//! // Create a subscription
//! let mut orderbook = client.subscribe_orderbook("BTC/USD", 10).await?;
//!
//! // Process messages as they arrive
//! while let Some(update) = orderbook.next().await {
//!     println!("Received update: {:?}", update);
//! }
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! # Monitoring Backpressure
//!
//! ```no_run
//! # #[cfg(feature = "orderbook")]
//! # {
//! use kraky::KrakyClient;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = KrakyClient::connect().await?;
//! let mut orderbook = client.subscribe_orderbook("BTC/USD", 10).await?;
//!
//! // Check backpressure stats
//! let stats = orderbook.stats();
//! println!("Delivered: {}", stats.delivered());
//! println!("Dropped: {}", stats.dropped());
//! println!("Drop rate: {:.2}%", stats.drop_rate());
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! # Concurrent Subscriptions
//!
//! You can have multiple active subscriptions simultaneously:
//!
//! ```no_run
//! # #[cfg(all(feature = "orderbook", feature = "trades"))]
//! # {
//! use kraky::KrakyClient;
//! use futures_util::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = KrakyClient::connect().await?;
//!
//! let mut btc_orderbook = client.subscribe_orderbook("BTC/USD", 10).await?;
//! let mut btc_trades = client.subscribe_trades("BTC/USD").await?;
//!
//! // Process both streams concurrently
//! loop {
//!     tokio::select! {
//!         Some(ob) = btc_orderbook.next() => {
//!             println!("Orderbook: {:?}", ob);
//!         }
//!         Some(trade) = btc_trades.next() => {
//!             println!("Trade: {:?}", trade);
//!         }
//!     }
//! }
//! # }
//! # }
//! ```

use crate::error::{KrakyError, Result};
use futures_util::Stream;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::sync::mpsc;

/// Default buffer size for subscription channels
pub const DEFAULT_BUFFER_SIZE: usize = 1000;

/// Backpressure configuration for subscriptions
#[derive(Debug, Clone)]
pub struct BackpressureConfig {
    /// Maximum number of messages to buffer before dropping
    pub buffer_size: usize,
}

impl Default for BackpressureConfig {
    fn default() -> Self {
        Self {
            buffer_size: DEFAULT_BUFFER_SIZE,
        }
    }
}

impl BackpressureConfig {
    /// Create a new backpressure config with custom buffer size
    pub fn with_buffer_size(buffer_size: usize) -> Self {
        Self { buffer_size }
    }
}

/// Statistics for a subscription
#[derive(Debug, Default)]
pub struct SubscriptionStats {
    /// Number of messages successfully delivered
    pub delivered: AtomicU64,
    /// Number of messages dropped due to backpressure
    pub dropped: AtomicU64,
}

impl SubscriptionStats {
    /// Get the number of delivered messages
    pub fn delivered(&self) -> u64 {
        self.delivered.load(Ordering::Relaxed)
    }

    /// Get the number of dropped messages
    pub fn dropped(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }

    /// Get the drop rate as a percentage
    pub fn drop_rate(&self) -> f64 {
        let delivered = self.delivered() as f64;
        let dropped = self.dropped() as f64;
        let total = delivered + dropped;
        if total == 0.0 {
            0.0
        } else {
            (dropped / total) * 100.0
        }
    }
}

/// A subscription to a Kraken data stream
///
/// Subscriptions are async streams that yield data as it arrives from
/// the Kraken WebSocket API. They can be consumed using the `next()` method
/// or by treating them as a `Stream`.
///
/// # Backpressure
///
/// Subscriptions use bounded channels to prevent memory issues. If the consumer
/// is too slow, older messages will be dropped to keep the most recent data.
/// Use `stats()` to monitor dropped messages.
///
/// # Example
///
/// ```no_run
/// use kraky::KrakyClient;
/// use futures_util::StreamExt;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = KrakyClient::connect().await?;
/// let mut orderbook = client.subscribe_orderbook("BTC/USD", 10).await?;
///
/// while let Some(update) = orderbook.next().await {
///     println!("Orderbook update: {:?}", update.data[0].symbol);
/// }
///
/// // Check for dropped messages
/// println!("Dropped: {} ({:.2}%)", orderbook.stats().dropped(), orderbook.stats().drop_rate());
/// # Ok(())
/// # }
/// ```
pub struct Subscription<T> {
    /// Receiver for subscription data
    receiver: mpsc::Receiver<T>,
    /// Subscription ID for tracking
    id: String,
    /// Statistics for this subscription
    stats: Arc<SubscriptionStats>,
}

impl<T> Subscription<T> {
    /// Create a new subscription
    pub(crate) fn new(
        receiver: mpsc::Receiver<T>,
        id: String,
        stats: Arc<SubscriptionStats>,
    ) -> Self {
        Self {
            receiver,
            id,
            stats,
        }
    }

    /// Get the next item from the subscription
    ///
    /// Returns `None` if the subscription has been closed.
    pub async fn next(&mut self) -> Option<T> {
        self.receiver.recv().await
    }

    /// Get the subscription ID
    ///
    /// The ID is a unique identifier for this subscription instance.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get subscription statistics
    ///
    /// Returns stats including delivered and dropped message counts.
    /// Use this to monitor backpressure and adjust consumption rate if needed.
    pub fn stats(&self) -> &SubscriptionStats {
        &self.stats
    }
}

impl<T> Stream for Subscription<T> {
    type Item = T;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(&mut self.receiver).poll_recv(cx)
    }
}

/// Subscription sender for internal use
pub(crate) struct SubscriptionSender<T> {
    sender: mpsc::Sender<T>,
    #[allow(dead_code)]
    id: String,
    #[allow(dead_code)]
    channel: String,
    pub(crate) symbol: String,
    /// Statistics shared with the subscription receiver
    stats: Arc<SubscriptionStats>,
}

impl<T> SubscriptionSender<T> {
    /// Create a new subscription pair (sender + receiver) with default backpressure config
    pub fn new(channel: String, symbol: String) -> (Self, Subscription<T>) {
        Self::with_config(channel, symbol, BackpressureConfig::default())
    }

    /// Create a new subscription pair with custom backpressure config
    pub fn with_config(
        channel: String,
        symbol: String,
        config: BackpressureConfig,
    ) -> (Self, Subscription<T>) {
        let (sender, receiver) = mpsc::channel(config.buffer_size);
        let id = format!("{}-{}-{}", channel, symbol, uuid::Uuid::new_v4());
        let stats = Arc::new(SubscriptionStats::default());

        let subscription = Subscription::new(receiver, id.clone(), Arc::clone(&stats));
        let sender = Self {
            sender,
            id,
            channel,
            symbol,
            stats,
        };

        (sender, subscription)
    }

    /// Send data to the subscription (non-blocking with backpressure)
    ///
    /// If the channel buffer is full, this will drop the message and
    /// increment the dropped counter. The WebSocket handler is never blocked.
    pub fn send(&self, data: T) -> Result<()> {
        match self.sender.try_send(data) {
            Ok(()) => {
                self.stats.delivered.fetch_add(1, Ordering::Relaxed);
                Ok(())
            }
            Err(mpsc::error::TrySendError::Full(_)) => {
                // Backpressure: drop the message to avoid blocking
                self.stats.dropped.fetch_add(1, Ordering::Relaxed);
                Ok(()) // Not an error - this is expected behavior
            }
            Err(mpsc::error::TrySendError::Closed(_)) => {
                Err(KrakyError::ChannelSend("subscription closed".to_string()))
            }
        }
    }

    /// Check if the subscription is still active
    #[allow(dead_code)]
    pub fn is_closed(&self) -> bool {
        self.sender.is_closed()
    }
}

/// Manager for multiple subscriptions
pub(crate) struct SubscriptionManager {
    /// Active orderbook subscriptions
    #[cfg(feature = "orderbook")]
    pub orderbook: Vec<SubscriptionSender<crate::models::OrderbookUpdate>>,
    /// Active trade subscriptions
    #[cfg(feature = "trades")]
    pub trades: Vec<SubscriptionSender<crate::models::Trade>>,
    /// Active ticker subscriptions
    #[cfg(feature = "ticker")]
    pub ticker: Vec<SubscriptionSender<crate::models::Ticker>>,
    /// Active OHLC subscriptions
    #[cfg(feature = "ohlc")]
    pub ohlc: Vec<SubscriptionSender<crate::models::OHLC>>,
}

impl Default for SubscriptionManager {
    fn default() -> Self {
        Self::new()
    }
}

impl SubscriptionManager {
    /// Create a new subscription manager
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "orderbook")]
            orderbook: Vec::new(),
            #[cfg(feature = "trades")]
            trades: Vec::new(),
            #[cfg(feature = "ticker")]
            ticker: Vec::new(),
            #[cfg(feature = "ohlc")]
            ohlc: Vec::new(),
        }
    }

    /// Clean up closed subscriptions
    #[allow(dead_code)]
    pub fn cleanup(&mut self) {
        #[cfg(feature = "orderbook")]
        self.orderbook.retain(|s| !s.is_closed());
        #[cfg(feature = "trades")]
        self.trades.retain(|s| !s.is_closed());
        #[cfg(feature = "ticker")]
        self.ticker.retain(|s| !s.is_closed());
        #[cfg(feature = "ohlc")]
        self.ohlc.retain(|s| !s.is_closed());
    }

    /// Dispatch orderbook update to relevant subscriptions
    #[cfg(feature = "orderbook")]
    pub fn dispatch_orderbook(&self, update: &crate::models::OrderbookUpdate) {
        for data in &update.data {
            for sub in &self.orderbook {
                if sub.symbol == data.symbol || sub.symbol == "*" {
                    let _ = sub.send(update.clone());
                }
            }
        }
    }

    /// Dispatch trade to relevant subscriptions
    #[cfg(feature = "trades")]
    pub fn dispatch_trade(&self, update: &crate::models::TradeUpdate) {
        for data in &update.data {
            let trade = data.to_trade();
            for sub in &self.trades {
                if sub.symbol == trade.symbol || sub.symbol == "*" {
                    let _ = sub.send(trade.clone());
                }
            }
        }
    }

    /// Dispatch ticker to relevant subscriptions
    #[cfg(feature = "ticker")]
    pub fn dispatch_ticker(&self, update: &crate::models::TickerUpdate) {
        for data in &update.data {
            let ticker = data.to_ticker();
            for sub in &self.ticker {
                if sub.symbol == ticker.symbol || sub.symbol == "*" {
                    let _ = sub.send(ticker.clone());
                }
            }
        }
    }

    /// Dispatch OHLC to relevant subscriptions
    #[cfg(feature = "ohlc")]
    pub fn dispatch_ohlc(&self, update: &crate::models::OHLCUpdate) {
        for data in &update.data {
            let ohlc = data.to_ohlc();
            for sub in &self.ohlc {
                if sub.symbol == ohlc.symbol || sub.symbol == "*" {
                    let _ = sub.send(ohlc.clone());
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_subscription_sender_receiver() {
        let (sender, mut subscription) =
            SubscriptionSender::<String>::new("test".to_string(), "BTC/USD".to_string());

        // Send a message
        sender.send("hello".to_string()).unwrap();

        // Receive the message
        let msg = subscription.next().await;
        assert_eq!(msg, Some("hello".to_string()));
    }

    #[test]
    fn test_subscription_id_format() {
        let (sender, subscription) =
            SubscriptionSender::<String>::new("book".to_string(), "BTC/USD".to_string());

        assert!(subscription.id().starts_with("book-BTC/USD-"));
        assert!(sender.symbol == "BTC/USD");
    }

    #[tokio::test]
    async fn test_backpressure_drops_messages() {
        // Create a subscription with a small buffer
        let config = BackpressureConfig::with_buffer_size(3);
        let (sender, mut subscription) = SubscriptionSender::<String>::with_config(
            "test".to_string(),
            "BTC/USD".to_string(),
            config,
        );

        // Fill the buffer
        sender.send("msg1".to_string()).unwrap();
        sender.send("msg2".to_string()).unwrap();
        sender.send("msg3".to_string()).unwrap();

        // This should be dropped (buffer full)
        sender.send("msg4".to_string()).unwrap();
        sender.send("msg5".to_string()).unwrap();

        // Check stats
        assert_eq!(subscription.stats().delivered(), 3);
        assert_eq!(subscription.stats().dropped(), 2);

        // Consume the buffered messages
        assert_eq!(subscription.next().await, Some("msg1".to_string()));
        assert_eq!(subscription.next().await, Some("msg2".to_string()));
        assert_eq!(subscription.next().await, Some("msg3".to_string()));
    }

    #[test]
    fn test_drop_rate_calculation() {
        let stats = SubscriptionStats::default();

        // No messages yet
        assert_eq!(stats.drop_rate(), 0.0);

        // Simulate some deliveries and drops
        stats.delivered.store(80, Ordering::Relaxed);
        stats.dropped.store(20, Ordering::Relaxed);

        // 20 / 100 = 20%
        assert!((stats.drop_rate() - 20.0).abs() < 0.001);
    }
}