longtrader-worker 0.2.1

Local strategy host running portable strategies against pluggable backends (exchange daemon / terminal API / mock)
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
//! Backpressure-aware channel plumbing (design doc §6.5).
//!
//! [`policy_channel`] wraps a bounded mpsc channel with an overflow policy:
//!
//! - [`OverflowPolicy::Block`] forwards directly through the bounded sender, backpressuring the
//!   pump when the consumer stalls. Used for orders/balances/positions (`OVERFLOW_ORDERS`).
//! - [`OverflowPolicy::DropOldest`] buffers up to `cap` items and silently drops the oldest
//!   buffered item on overflow. Used for ticker/trades/ohlcv (`OVERFLOW_TICKER`).
//! - [`OverflowPolicy::Coalesce`] keeps only the newest item per key (e.g. per symbol/channel),
//!   collapsing stale intermediate states. Used for orderbook (`OVERFLOW_ORDERBOOK`).
//!
//! Backpressure isolation: every session owns an independent `mpsc` channel
//! (one per logical stream) so a slow consumer never blocks another session
//! or the worker↔daemon ingress. Policies above map per-channel:
//! ticker→`DropOldest`, book→`Coalesce`, orders→`Block`.

use std::{
    collections::VecDeque,
    hash::Hash,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

use tokio::sync::{Mutex, Notify, mpsc};

use crate::ports::OverflowPolicy;
// Re-export the port-level defaults so `crate::overflow::OVERFLOW_*` and
// `crate::ports::OVERFLOW_*` resolve to one owner (`crate::ports`): the policy
// values are port semantics (§6.5 table), `policy_channel` is the plumbing that
// enforces them. Either path compiles; neither drifts.
pub use crate::ports::{
    DEFAULT_PAGE_LIMIT, MAX_PAGE_LIMIT, OVERFLOW_BALANCES, OVERFLOW_OHLCV, OVERFLOW_ORDERBOOK,
    OVERFLOW_ORDERS, OVERFLOW_POSITIONS, OVERFLOW_TICKER, OVERFLOW_TRADES, page_limit_or_default,
};

struct PipeState<T, K> {
    tx: mpsc::Sender<T>,
    buf: Mutex<VecDeque<(K, T)>>,
    cap: usize,
    policy: OverflowPolicy,
    key_fn: Arc<dyn Fn(&T) -> K + Send + Sync>,
    notify: Notify,
    source_closed: AtomicBool,
}

impl<T, K> PipeState<T, K>
where
    T: Send + 'static,
    K: Eq + Hash + Send + 'static,
{
    /// Forward loop: wakes on new buffered items or source closure, drains
    /// the buffer into the bounded downstream channel.
    ///
    /// Uses `tokio::select!` to avoid the race between checking `source_closed`
    /// and waiting on `notify.notified()`. This ensures that if `close()` is
    /// called between the check and the wait, the notification is not lost.
    ///
    /// Exits when the downstream receiver is dropped: `mpsc::Sender::send`
    /// returns `Err` only in that case, and looping on a dead receiver would
    /// leak this task.
    async fn forward_loop(self: Arc<Self>) {
        loop {
            // Check for source closure first.
            if self.source_closed.load(Ordering::Acquire) {
                let drained: VecDeque<(K, T)> = std::mem::take(&mut *self.buf.lock().await);
                for (_, item) in drained {
                    if self.tx.send(item).await.is_err() {
                        return;
                    }
                }
                return;
            }
            // Try to drain buffered items without waiting.
            let items: VecDeque<(K, T)> = {
                let mut buf = self.buf.lock().await;
                if buf.is_empty() { VecDeque::new() } else { std::mem::take(&mut *buf) }
            };
            if !items.is_empty() {
                for (_, item) in items {
                    // Downstream is bounded; this backpressures draining, which is
                    // fine because the buffer already applies the drop/coalesce
                    // policy on push. A closed receiver ends the loop (see above).
                    if self.tx.send(item).await.is_err() {
                        return;
                    }
                }
                continue;
            }
            // Buffer is empty; wait for either a new item or source closure.
            // `tokio::select!` ensures we wake up immediately if `close()` is
            // called while we are waiting.
            //
            // Note: We use a simple `notify.notified()` here instead of a busy-wait
            // loop. The `close()` method sets `source_closed` and calls `notify_one()`,
            // so we will wake up immediately when the source is closed.
            self.notify.notified().await;
        }
    }
}

/// Sender side of a policy pipe. Cheap to clone.
pub struct PolicySender<T, K> {
    state: Arc<PipeState<T, K>>,
}

impl<T, K> Clone for PolicySender<T, K> {
    fn clone(&self) -> Self {
        Self { state: Arc::clone(&self.state) }
    }
}

impl<T, K> PolicySender<T, K>
where
    T: Send + 'static,
    K: Eq + Hash + Send + 'static,
{
    /// Push one item through the configured overflow policy.
    /// ponytail: Coalesce uses an O(n) key scan; cap is small (<=64 in practice).
    /// Upgrade to HashMap<index> if cap grows or profiling shows hotspot.
    ///
    /// Returns `false` when the downstream receiver is already dropped: the
    /// caller must not keep producing into a dead channel.
    pub async fn send(&self, item: T) -> bool {
        match self.state.policy {
            OverflowPolicy::Block => {
                // Direct bounded send: never drops, backpressures upstream.
                // A closed receiver is the only `Err`; report it so the pump
                // can stop instead of spinning on a dead channel.
                self.state.tx.send(item).await.is_ok()
            }
            OverflowPolicy::DropOldest | OverflowPolicy::Coalesce => {
                if self.state.tx.is_closed() {
                    return false;
                }
                let key = (self.state.key_fn)(&item);
                {
                    let mut buf = self.state.buf.lock().await;
                    debug_assert!(self.state.cap <= 1024, "coalesce cap unexpectedly large");
                    if self.state.policy == OverflowPolicy::Coalesce {
                        // Replace any buffered item with the same key.
                        if let Some(slot) = buf.iter_mut().find(|(k, _)| *k == key) {
                            slot.1 = item;
                        } else {
                            buf.push_back((key, item));
                            while buf.len() > self.state.cap {
                                buf.pop_front();
                            }
                        }
                    } else {
                        buf.push_back((key, item));
                        while buf.len() > self.state.cap {
                            buf.pop_front();
                        }
                    }
                }
                self.state.notify.notify_one();
                true
            }
        }
    }

    /// Mark the upstream source closed; buffered items are drained, then the
    /// downstream receiver closes.
    pub fn close(&self) {
        self.state.source_closed.store(true, Ordering::Release);
        self.state.notify.notify_one();
    }

    /// True once every downstream receiver has been dropped.
    pub fn is_closed(&self) -> bool {
        self.state.tx.is_closed()
    }
}

/// Create a policy-applied channel with buffer capacity `cap`. `key_of`
/// extracts the coalescing key (identity for DropOldest).
pub fn policy_channel<T, K>(
    cap: usize,
    policy: OverflowPolicy,
    key_of: impl Fn(&T) -> K + Send + Sync + 'static,
) -> (PolicySender<T, K>, mpsc::Receiver<T>)
where
    T: Send + 'static,
    K: Eq + Hash + Send + 'static,
{
    let cap = cap.max(1);
    let (tx, rx) = mpsc::channel(cap);
    let state = Arc::new(PipeState {
        tx,
        buf: Mutex::new(VecDeque::new()),
        cap,
        policy,
        key_fn: Arc::new(key_of),
        notify: Notify::new(),
        source_closed: AtomicBool::new(false),
    });
    tokio::spawn(state.clone().forward_loop());
    (PolicySender { state }, rx)
}

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

    /// Run `produce` on a separate task so bounded/blocking sends never
    /// deadlock against the consuming test task.
    async fn drive<T, K>(
        cap: usize,
        policy: OverflowPolicy,
        key_of: impl Fn(&T) -> K + Send + Sync + 'static,
        produce: impl FnOnce(PolicySender<T, K>) -> futures_util::future::BoxFuture<'static, ()>
        + Send
        + 'static,
    ) -> mpsc::Receiver<T>
    where
        T: Send + 'static,
        K: Eq + Hash + Send + 'static,
    {
        let (tx, rx) = policy_channel(cap, policy, key_of);
        tokio::spawn(async move {
            produce(tx.clone()).await;
            tx.close();
        });
        rx
    }

    #[tokio::test]
    async fn block_policy_never_drops() {
        let mut rx = drive(
            2,
            OverflowPolicy::Block,
            |v| *v,
            |tx| {
                Box::pin(async move {
                    for v in 0..8u64 {
                        tx.send(v).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![0, 1, 2, 3, 4, 5, 6, 7]);
    }

    #[tokio::test]
    async fn drop_oldest_keeps_newest_window() {
        let mut rx = drive(
            3,
            OverflowPolicy::DropOldest,
            |v| *v,
            |tx| {
                Box::pin(async move {
                    for v in 0..10u64 {
                        tx.send(v).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![7, 8, 9], "only the newest `cap` items survive");
    }

    #[tokio::test]
    async fn coalesce_keeps_newest_per_key_in_arrival_order() {
        let mut rx = drive(
            4,
            OverflowPolicy::Coalesce,
            |item: &(char, u32)| item.0,
            |tx| {
                Box::pin(async move {
                    for &(k, v) in &[('a', 1), ('b', 1), ('a', 2), ('a', 3), ('c', 9)] {
                        tx.send((k, v)).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        // Coalesce keeps each key's insertion position and only refreshes its
        // payload, so keys arrive in first-seen order.
        assert_eq!(seen, vec![('a', 3), ('b', 1), ('c', 9)]);
    }

    #[tokio::test]
    async fn close_drains_buffer_before_closing_receiver() {
        let mut rx = drive(
            4,
            OverflowPolicy::DropOldest,
            |v| *v,
            |tx| {
                Box::pin(async move {
                    tx.send(1).await;
                    tx.send(2).await;
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![1, 2]);
    }

    // -----------------------------------------------------------------------
    // Capacity clamping
    // -----------------------------------------------------------------------

    /// `mpsc::channel(0)` panics, so a zero capacity is clamped to one. The
    /// observable effect is that exactly one item survives a burst.
    #[tokio::test]
    async fn a_zero_capacity_is_clamped_to_one() {
        let mut rx = drive(
            0,
            OverflowPolicy::DropOldest,
            |v| *v,
            |tx| {
                Box::pin(async move {
                    for v in 0..6u64 {
                        tx.send(v).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![5], "cap 0 behaves as cap 1: only the newest survives");
    }

    /// Under `Block`, a clamped capacity still delivers every item — nothing is
    /// dropped, it just takes longer.
    #[tokio::test]
    async fn block_policy_with_a_clamped_capacity_still_delivers_everything() {
        let mut rx = drive(
            0,
            OverflowPolicy::Block,
            |v| *v,
            |tx| {
                Box::pin(async move {
                    for v in 0..4u64 {
                        tx.send(v).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![0, 1, 2, 3]);
    }

    // -----------------------------------------------------------------------
    // Coalesce capacity
    // -----------------------------------------------------------------------

    /// More distinct keys than `cap` must evict from the front, keeping the
    /// newest `cap` keys in arrival order.
    #[tokio::test]
    async fn coalesce_drops_the_oldest_key_when_distinct_keys_exceed_cap() {
        let mut rx = drive(
            2,
            OverflowPolicy::Coalesce,
            |item: &(char, u32)| item.0,
            |tx| {
                Box::pin(async move {
                    for &(k, v) in &[('a', 1), ('b', 2), ('c', 3), ('d', 4)] {
                        tx.send((k, v)).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![('c', 3), ('d', 4)], "the two newest keys survive");
    }

    /// A repeated key never consumes extra capacity: refreshing an existing slot
    /// replaces the payload in place, so a busy symbol cannot evict others.
    #[tokio::test]
    async fn coalesce_refreshing_a_key_does_not_consume_capacity() {
        let mut rx = drive(
            2,
            OverflowPolicy::Coalesce,
            |item: &(char, u32)| item.0,
            |tx| {
                Box::pin(async move {
                    for &(k, v) in &[('a', 1), ('b', 1), ('a', 2), ('a', 3), ('a', 4)] {
                        tx.send((k, v)).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![('a', 4), ('b', 1)], "key 'a' was refreshed, not duplicated");
    }

    /// With a single key the coalesce stream is exactly "the latest value",
    /// which is the orderbook contract.
    #[tokio::test]
    async fn coalesce_on_one_key_keeps_only_the_latest() {
        let mut rx = drive(
            8,
            OverflowPolicy::Coalesce,
            |item: &(char, u32)| item.0,
            |tx| {
                Box::pin(async move {
                    for v in 1..=20u32 {
                        tx.send(('x', v)).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![('x', 20)]);
    }

    /// Under `DropOldest` the key function is irrelevant: every item occupies a
    /// slot whether or not the key repeats.
    #[tokio::test]
    async fn drop_oldest_keeps_every_item_even_when_keys_repeat() {
        let mut rx = drive(
            3,
            OverflowPolicy::DropOldest,
            |item: &(char, u32)| item.0,
            |tx| {
                Box::pin(async move {
                    for v in 1..=10u32 {
                        tx.send(('x', v)).await;
                    }
                })
            },
        )
        .await;
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(
            seen,
            vec![('x', 8), ('x', 9), ('x', 10)],
            "keys do not dedupe under DropOldest"
        );
    }

    // -----------------------------------------------------------------------
    // Lifecycle
    // -----------------------------------------------------------------------

    /// `is_closed` tracks the downstream receiver: false while a consumer is
    /// attached, true once it drops.
    #[tokio::test]
    async fn is_closed_follows_the_downstream_receiver() {
        let (tx, rx) = policy_channel::<u64, u64>(2, OverflowPolicy::Block, |v| *v);
        assert!(!tx.is_closed(), "a live receiver is not closed");
        drop(rx);
        // The forward task observes the close asynchronously.
        for _ in 0..100 {
            if tx.is_closed() {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }
        assert!(tx.is_closed(), "dropping the receiver must close the sender");
    }

    /// Closing before anything is sent still terminates the stream.
    #[tokio::test]
    async fn closing_an_empty_pipe_terminates_the_stream() {
        let (tx, mut rx) = policy_channel::<u64, u64>(2, OverflowPolicy::DropOldest, |v| *v);
        tx.close();
        // The receiver only observes end-of-stream once the shared `PipeState`
        // is gone, i.e. after every `PolicySender` clone is dropped.
        drop(tx);
        let idle = std::time::Duration::from_secs(5);
        let closed = tokio::time::timeout(idle, rx.recv()).await;
        assert!(matches!(closed, Ok(None)), "close with no items yields an empty stream");
    }

    /// A clone shares the same pipe, so closing through one clone closes both.
    #[tokio::test]
    async fn clones_share_one_pipe() {
        let (tx, mut rx) = policy_channel::<u64, u64>(4, OverflowPolicy::DropOldest, |v| *v);
        let other = tx.clone();
        other.send(1).await;
        other.send(2).await;
        tx.close();
        // The downstream sender lives in the shared `PipeState`, so the receiver
        // only sees end-of-stream once *every* `PolicySender` is gone.
        drop(tx);
        drop(other);
        let mut seen = Vec::new();
        while let Some(v) = rx.recv().await {
            seen.push(v);
        }
        assert_eq!(seen, vec![1, 2], "items sent through either clone arrive in order");
    }

    /// Close is idempotent: calling it twice must not panic or hang.
    #[tokio::test]
    async fn closing_twice_is_harmless() {
        let (tx, mut rx) = policy_channel::<u64, u64>(2, OverflowPolicy::Block, |v| *v);
        tx.close();
        tx.close();
        drop(tx);
        let idle = std::time::Duration::from_secs(5);
        let closed = tokio::time::timeout(idle, rx.recv()).await;
        assert!(matches!(closed, Ok(None)), "a repeated close must still terminate");
    }
}