oanda-rs 0.1.0

Async Rust SDK for the OANDA v20 REST and streaming API
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! The self-managing stream engine: connection state machine with
//! heartbeat watchdog, capped exponential backoff, and back-fill support.

use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

use bytes::Bytes;
use futures_core::Stream;
use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
use serde::de::DeserializeOwned;
use tokio::time::{Instant, Sleep};

use super::StreamConfig;
use super::json_lines::JsonLines;
use crate::error::Error;

pub(crate) type ByteStream = BoxStream<'static, reqwest::Result<Bytes>>;
type Lines<T> = JsonLines<ByteStream, T>;
type ConnectFuture = BoxFuture<'static, Result<ByteStream, Error>>;
type BackfillFuture<T> = BoxFuture<'static, Result<Vec<T>, Error>>;
/// `Some(event)` ends the current poll iteration; `None` continues the state machine.
type Terminal<T> = Option<Poll<Option<Result<T, Error>>>>;

/// Endpoint-specific behaviour plugged into [`ManagedStream`].
pub(crate) trait StreamKind: Send + Unpin + 'static {
    type Item: DeserializeOwned + Send + Unpin + 'static;

    /// Builds a future that opens the connection (waiting for a
    /// connection-limiter slot, sending the request, and checking the
    /// response status).
    fn connect(&mut self, reconnect: bool) -> ConnectFuture;

    /// Called for every item before it is yielded; returning `false` drops
    /// the item (used to deduplicate back-filled transactions).
    fn filter(&mut self, _item: &Self::Item) -> bool {
        true
    }

    /// Builds an optional back-fill future run after a successful
    /// reconnect, yielding items missed while disconnected.
    fn backfill(&mut self) -> Option<BackfillFuture<Self::Item>> {
        None
    }
}

/// A snapshot of a managed stream's connection statistics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct StreamStats {
    /// Number of successful reconnects performed since the stream was
    /// created (the initial connection is not counted).
    pub reconnects: u64,
    /// Number of failed connection attempts since the stream was created.
    pub failed_attempts: u64,
}

enum State<T> {
    Connecting(ConnectFuture),
    Backfilling {
        lines: Lines<T>,
        future: BackfillFuture<T>,
    },
    Draining {
        lines: Lines<T>,
        pending: VecDeque<T>,
    },
    Streaming {
        lines: Lines<T>,
        watchdog: Pin<Box<Sleep>>,
    },
    Sleeping(Pin<Box<Sleep>>),
    Done,
}

pub(crate) struct ManagedStream<K: StreamKind> {
    kind: K,
    config: StreamConfig,
    state: State<K::Item>,
    stats: StreamStats,
    attempts_since_success: u32,
    current_delay: Duration,
    connected_at: Option<Instant>,
}

impl<K: StreamKind> ManagedStream<K> {
    /// Wraps an already-established connection (the initial connect is
    /// performed by the endpoint builder so connection errors surface at
    /// `send()`).
    pub(crate) fn new(kind: K, config: StreamConfig, initial: ByteStream) -> Self {
        let heartbeat_timeout = config.heartbeat_timeout;
        ManagedStream {
            kind,
            current_delay: config.backoff_initial,
            config,
            state: State::Streaming {
                lines: JsonLines::new(initial),
                watchdog: Box::pin(tokio::time::sleep(heartbeat_timeout)),
            },
            stats: StreamStats::default(),
            attempts_since_success: 0,
            connected_at: Some(Instant::now()),
        }
    }

    pub(crate) fn stats(&self) -> StreamStats {
        self.stats
    }

    /// Handles a broken connection: either schedules a reconnect (returning
    /// `None`) or produces the caller-visible terminal event.
    fn connection_lost(&mut self, error: Option<Error>) -> Terminal<K::Item> {
        #[cfg(feature = "tracing")]
        tracing::debug!(error = ?error, "stream connection lost");

        if !self.config.auto_reconnect {
            self.state = State::Done;
            return Some(Poll::Ready(error.map(Err)));
        }
        // A connection that stayed healthy long enough resets the backoff;
        // one that died right after connecting keeps escalating it.
        if let Some(connected_at) = self.connected_at.take() {
            if connected_at.elapsed() >= self.config.backoff_reset_after {
                self.attempts_since_success = 0;
                self.current_delay = self.config.backoff_initial;
            }
        }
        self.schedule_reconnect(error)
    }

    /// Handles a failed reconnect attempt.
    fn connect_failed(&mut self, error: Error) -> Terminal<K::Item> {
        self.stats.failed_attempts += 1;

        #[cfg(feature = "tracing")]
        tracing::debug!(error = %error, "stream reconnect attempt failed");

        if is_fatal(&error) {
            self.state = State::Done;
            return Some(Poll::Ready(Some(Err(error))));
        }
        self.schedule_reconnect(Some(error))
    }

    fn schedule_reconnect(&mut self, error: Option<Error>) -> Terminal<K::Item> {
        if let Some(max) = self.config.max_reconnect_attempts {
            if self.attempts_since_success >= max {
                self.state = State::Done;
                return Some(Poll::Ready(Some(Err(error.unwrap_or_else(|| {
                    Error::Stream("reconnect attempts exhausted".into())
                })))));
            }
        }
        self.attempts_since_success += 1;
        let delay = jitter(self.current_delay);
        self.current_delay = (self.current_delay * 2).min(self.config.backoff_max);

        #[cfg(feature = "tracing")]
        tracing::debug!(delay = ?delay, attempt = self.attempts_since_success, "stream reconnect scheduled");

        self.state = State::Sleeping(Box::pin(tokio::time::sleep(delay)));
        None
    }
}

/// Only client-side errors are fatal; transport failures and server errors
/// are worth retrying.
fn is_fatal(error: &Error) -> bool {
    match error {
        Error::Api { status, .. } => status.is_client_error(),
        Error::Config(_) => true,
        _ => false,
    }
}

/// Applies ±25% pseudo-random jitter so reconnecting clients don't
/// synchronize.
fn jitter(delay: Duration) -> Duration {
    let nanos = Instant::now().elapsed().subsec_nanos() as u64 ^ delay.as_nanos() as u64;
    let factor = 0.75 + (nanos % 1000) as f64 / 2000.0; // 0.75..=1.25
    delay.mul_f64(factor)
}

impl<K: StreamKind> Stream for ManagedStream<K> {
    type Item = Result<K::Item, Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        loop {
            match &mut this.state {
                State::Connecting(future) => match future.as_mut().poll(cx) {
                    Poll::Ready(Ok(bytes)) => {
                        this.stats.reconnects += 1;
                        this.connected_at = Some(Instant::now());
                        let lines = JsonLines::new(bytes);

                        #[cfg(feature = "tracing")]
                        tracing::debug!(reconnects = this.stats.reconnects, "stream reconnected");

                        this.state = match this.kind.backfill() {
                            Some(future) => State::Backfilling { lines, future },
                            None => State::Streaming {
                                lines,
                                watchdog: Box::pin(tokio::time::sleep(
                                    this.config.heartbeat_timeout,
                                )),
                            },
                        };
                    }
                    Poll::Ready(Err(e)) => {
                        if let Some(result) = this.connect_failed(e) {
                            return result;
                        }
                    }
                    Poll::Pending => return Poll::Pending,
                },
                State::Backfilling { future, .. } => match future.as_mut().poll(cx) {
                    Poll::Ready(result) => {
                        let State::Backfilling { lines, .. } =
                            std::mem::replace(&mut this.state, State::Done)
                        else {
                            unreachable!()
                        };
                        match result {
                            Ok(items) => {
                                this.state = State::Draining {
                                    lines,
                                    pending: items.into(),
                                };
                            }
                            Err(e) => {
                                // Surface the failed back-fill (there may be
                                // a gap), but keep the live stream running.
                                this.state = State::Streaming {
                                    lines,
                                    watchdog: Box::pin(tokio::time::sleep(
                                        this.config.heartbeat_timeout,
                                    )),
                                };
                                return Poll::Ready(Some(Err(e)));
                            }
                        }
                    }
                    Poll::Pending => return Poll::Pending,
                },
                State::Draining { pending, .. } => match pending.pop_front() {
                    Some(item) => {
                        if this.kind.filter(&item) {
                            return Poll::Ready(Some(Ok(item)));
                        }
                    }
                    None => {
                        let State::Draining { lines, .. } =
                            std::mem::replace(&mut this.state, State::Done)
                        else {
                            unreachable!()
                        };
                        this.state = State::Streaming {
                            lines,
                            watchdog: Box::pin(tokio::time::sleep(this.config.heartbeat_timeout)),
                        };
                    }
                },
                State::Streaming { lines, watchdog } => {
                    match Pin::new(lines).poll_next(cx) {
                        Poll::Ready(Some(Ok(item))) => {
                            watchdog
                                .as_mut()
                                .reset(Instant::now() + this.config.heartbeat_timeout);
                            if this.kind.filter(&item) {
                                return Poll::Ready(Some(Ok(item)));
                            }
                        }
                        Poll::Ready(Some(Err(e @ Error::Decode { .. }))) => {
                            // A malformed line doesn't invalidate the
                            // connection; report it and keep streaming.
                            return Poll::Ready(Some(Err(e)));
                        }
                        Poll::Ready(Some(Err(e))) => {
                            if let Some(result) = this.connection_lost(Some(e)) {
                                return result;
                            }
                        }
                        Poll::Ready(None) => {
                            if let Some(result) = this.connection_lost(None) {
                                return result;
                            }
                        }
                        Poll::Pending => match watchdog.as_mut().poll(cx) {
                            Poll::Ready(()) => {
                                let stale = Error::Stream(format!(
                                    "no data within {:?} (heartbeats expected every 5s); connection considered stale",
                                    this.config.heartbeat_timeout
                                ));
                                if let Some(result) = this.connection_lost(Some(stale)) {
                                    return result;
                                }
                            }
                            Poll::Pending => return Poll::Pending,
                        },
                    }
                }
                State::Sleeping(sleep) => match sleep.as_mut().poll(cx) {
                    Poll::Ready(()) => {
                        this.state = State::Connecting(this.kind.connect(true));
                    }
                    Poll::Pending => return Poll::Pending,
                },
                State::Done => return Poll::Ready(None),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures_util::StreamExt;
    use std::sync::{Arc, Mutex};

    /// Scripted connection outcomes for driving the state machine.
    enum Outcome {
        /// Connection refused (non-fatal).
        Fail,
        /// Connection refused with a fatal (4xx) error.
        FailFatal,
        /// Connects; yields the chunks, then EOF.
        Chunks(Vec<&'static [u8]>),
        /// Connects; yields the chunks, then hangs forever.
        ChunksThenHang(Vec<&'static [u8]>),
        /// Connects; yields one heartbeat line every `gap` for `count`
        /// lines, then EOF.
        Spaced { count: u32, gap: Duration },
    }

    struct MockKind {
        script: std::collections::VecDeque<Outcome>,
        connects: Arc<Mutex<Vec<Instant>>>,
    }

    impl MockKind {
        fn new(script: Vec<Outcome>) -> (Self, Arc<Mutex<Vec<Instant>>>) {
            let connects = Arc::new(Mutex::new(Vec::new()));
            (
                MockKind {
                    script: script.into(),
                    connects: Arc::clone(&connects),
                },
                connects,
            )
        }
    }

    fn bytes_from(outcome: Outcome) -> Result<ByteStream, Error> {
        use futures_util::stream;
        match outcome {
            Outcome::Fail => Err(Error::Stream("connection refused".into())),
            Outcome::FailFatal => Err(Error::Api {
                status: reqwest::StatusCode::UNAUTHORIZED,
                request_id: None,
                body: crate::error::ApiErrorBody::from_text("nope".into()),
            }),
            Outcome::Chunks(chunks) => {
                Ok(stream::iter(chunks.into_iter().map(|c| Ok(Bytes::from_static(c)))).boxed())
            }
            Outcome::ChunksThenHang(chunks) => Ok(stream::iter(
                chunks.into_iter().map(|c| Ok(Bytes::from_static(c))),
            )
            .chain(stream::pending())
            .boxed()),
            Outcome::Spaced { count, gap } => Ok(stream::unfold(0u32, move |i| async move {
                if i >= count {
                    return None;
                }
                tokio::time::sleep(gap).await;
                Some((Ok(Bytes::from_static(b"{\"type\":\"HEARTBEAT\"}\n")), i + 1))
            })
            .boxed()),
        }
    }

    impl StreamKind for MockKind {
        type Item = serde_json::Value;

        fn connect(&mut self, _reconnect: bool) -> ConnectFuture {
            self.connects.lock().unwrap().push(Instant::now());
            let outcome = self.script.pop_front().expect("script exhausted");
            Box::pin(async move { bytes_from(outcome) })
        }
    }

    fn config() -> StreamConfig {
        StreamConfig::default()
    }

    fn managed(
        script: Vec<Outcome>,
        initial: Outcome,
        config: StreamConfig,
    ) -> (ManagedStream<MockKind>, Arc<Mutex<Vec<Instant>>>) {
        let (kind, connects) = MockKind::new(script);
        let initial = bytes_from(initial).unwrap();
        (ManagedStream::new(kind, config, initial), connects)
    }

    #[tokio::test(start_paused = true)]
    async fn backoff_escalates_with_cap_during_outage() {
        // Initial connection dies immediately; every reconnect fails. With
        // max 10 attempts the stream must end with an error, and the gaps
        // between attempts must escalate 1s→2s→…→300s cap (±25% jitter).
        let mut cfg = config();
        cfg.max_reconnect_attempts = Some(10);
        let script = (0..10).map(|_| Outcome::Fail).collect();
        let (stream, connects) = managed(script, Outcome::Chunks(vec![]), cfg);
        let start = Instant::now();
        let items: Vec<_> = stream.collect().await;
        assert_eq!(items.len(), 1);
        assert!(items[0].is_err(), "expected terminal error");

        let connects = connects.lock().unwrap();
        assert_eq!(connects.len(), 10);
        let mut previous = start;
        for (i, at) in connects.iter().enumerate() {
            let expected = Duration::from_secs(1 << i).min(Duration::from_secs(300));
            let delta = at.duration_since(previous);
            assert!(
                delta >= expected.mul_f64(0.74) && delta <= expected.mul_f64(1.26),
                "attempt {i}: delta {delta:?}, expected ~{expected:?}"
            );
            previous = *at;
        }
        // A multi-hour outage keeps the cadence at the 5-minute cap: the
        // last gap must be ~300s, not still growing.
        let last = connects[9].duration_since(connects[8]);
        assert!(last >= Duration::from_secs(225) && last <= Duration::from_secs(375));
    }

    #[tokio::test(start_paused = true)]
    async fn stable_connection_resets_backoff() {
        // fail, fail, then a connection healthy for >60s, then fail once
        // more: the delay after the healthy connection must be back at
        // ~1s, not continuing to escalate.
        let script = vec![
            Outcome::Fail,
            Outcome::Fail,
            Outcome::Spaced {
                count: 13,
                gap: Duration::from_secs(5),
            }, // healthy ~65s
            Outcome::Fail,
            Outcome::Chunks(vec![b"{\"ok\":1}\n"]),
        ];
        let mut cfg = config();
        cfg.max_reconnect_attempts = Some(100);
        let (stream, connects) = managed(script, Outcome::Chunks(vec![]), cfg);
        // 13 heartbeats + 1 final item; stream keeps reconnecting after
        // the last EOF, so just take what we expect.
        let items: Vec<_> = stream.take(14).collect().await;
        assert_eq!(items.iter().filter(|r| r.is_ok()).count(), 14);

        let connects = connects.lock().unwrap();
        // connect #2 (index 2, healthy) ends ~65s after it starts; connect
        // #3 (index 3) happens j(1s) later because the backoff reset.
        let healthy_end = connects[2] + Duration::from_secs(65);
        let delta = connects[3].duration_since(healthy_end);
        assert!(
            delta <= Duration::from_millis(1300),
            "backoff was not reset after stable connection: {delta:?}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn short_lived_connection_keeps_escalating() {
        // fail, then a connection that dies instantly, then fail: the
        // delay after the instant death must continue the escalation
        // (~4s), not reset to 1s.
        let script = vec![
            Outcome::Fail,
            Outcome::Chunks(vec![]), // connects, dies immediately
            Outcome::Fail,
            Outcome::Chunks(vec![b"{\"ok\":1}\n"]),
        ];
        let (stream, connects) = managed(script, Outcome::Chunks(vec![]), config());
        let items: Vec<_> = stream.take(1).collect().await;
        assert!(items[0].is_ok());

        let connects = connects.lock().unwrap();
        // delays: j(1) before #0, j(2) before #1, j(4) before #2? No —
        // successful connect #1 dies instantly (unstable), so the delay
        // before #2 continues at j(4).
        let delta = connects[2].duration_since(connects[1]);
        assert!(
            delta >= Duration::from_millis(2900),
            "backoff reset after an unstable connection: {delta:?}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn watchdog_detects_stale_connection() {
        // The initial connection sends one line then hangs. The watchdog
        // (10s) must declare it stale and reconnect.
        let script = vec![Outcome::Chunks(vec![b"{\"n\":2}\n"])];
        let (stream, connects) = managed(
            script,
            Outcome::ChunksThenHang(vec![b"{\"n\":1}\n"]),
            config(),
        );
        let start = Instant::now();
        let items: Vec<_> = stream.take(2).collect().await;
        assert_eq!(items.iter().filter(|r| r.is_ok()).count(), 2);

        let connects = connects.lock().unwrap();
        let delta = connects[0].duration_since(start);
        // ~10s watchdog + ~1s backoff (with jitter).
        assert!(
            delta >= Duration::from_millis(10_700) && delta <= Duration::from_millis(11_300),
            "unexpected stale detection timing: {delta:?}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn fatal_error_ends_stream() {
        let script = vec![Outcome::FailFatal];
        let (stream, _) = managed(script, Outcome::Chunks(vec![]), config());
        let items: Vec<_> = stream.collect().await;
        assert_eq!(items.len(), 1);
        match &items[0] {
            Err(Error::Api { status, .. }) => assert_eq!(status.as_u16(), 401),
            other => panic!("expected fatal Api error, got {other:?}"),
        }
    }

    #[tokio::test(start_paused = true)]
    async fn auto_reconnect_disabled_ends_on_eof() {
        let mut cfg = config();
        cfg.auto_reconnect = false;
        let (stream, connects) = managed(vec![], Outcome::Chunks(vec![b"{\"n\":1}\n"]), cfg);
        let items: Vec<_> = stream.collect().await;
        assert_eq!(items.len(), 1);
        assert!(items[0].is_ok());
        assert!(connects.lock().unwrap().is_empty(), "must not reconnect");
    }

    #[tokio::test(start_paused = true)]
    async fn stats_count_reconnects() {
        let script = vec![Outcome::Fail, Outcome::Chunks(vec![b"{\"n\":1}\n"])];
        let mut cfg = config();
        cfg.auto_reconnect = true;
        let (mut stream, _) = managed(script, Outcome::Chunks(vec![]), cfg);
        let first = stream.next().await.unwrap();
        assert!(first.is_ok());
        let stats = stream.stats();
        assert_eq!(stats.reconnects, 1);
        assert_eq!(stats.failed_attempts, 1);
    }
}