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
use std::{
    collections::HashMap,
    pin::Pin,
    sync::atomic::{AtomicU64, Ordering},
    time::Duration,
};

use tokio::time::{Instant, Sleep};

pub type RequestSequence = u64;

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum RequestId {
    FetchEntries,
    FetchSubscription,
    SubscribeFeed,
    UnsubscribeFeed,
}

/// Mangae in flight requests state
pub struct InFlight {
    next_request_sequence: AtomicU64,
    in_flights: HashMap<RequestSequence, InFlightEntry>,
    throbber_timer: Pin<Box<Sleep>>,
    throbber_step: i8,
    throbber_timer_interval: Duration,
}

impl InFlight {
    pub fn new() -> Self {
        Self {
            next_request_sequence: AtomicU64::new(0),
            in_flights: HashMap::new(),
            throbber_timer: Box::pin(tokio::time::sleep(Duration::from_secs(3600 * 24))),
            throbber_step: 0,
            throbber_timer_interval: Duration::from_millis(250),
        }
    }

    #[must_use]
    pub fn with_throbber_timer_interval(self, interval: Duration) -> Self {
        Self {
            throbber_timer_interval: interval,
            ..self
        }
    }

    pub fn recent_in_flight(&self) -> Option<RequestId> {
        self.in_flights
            .iter()
            .max_by_key(|(_, entry)| entry.start)
            .map(|(_, entry)| entry.request_id)
    }

    pub async fn throbber_timer(&mut self) {
        self.throbber_timer.as_mut().await;
    }

    pub fn reset_throbber_timer(&mut self) {
        self.throbber_timer
            .as_mut()
            .reset(Instant::now() + self.throbber_timer_interval);
    }

    pub fn inc_throbber_step(&mut self) {
        self.throbber_step = self.throbber_step.wrapping_add(1);
    }

    pub fn throbber_step(&self) -> i8 {
        self.throbber_step
    }

    pub fn add(&mut self, request_id: RequestId) -> RequestSequence {
        let seq = self.next_request_sequence();
        self.in_flights.insert(
            seq,
            InFlightEntry {
                start: Instant::now(),
                request_id,
            },
        );

        self.reset_throbber_timer();

        seq
    }

    pub fn remove(&mut self, seq: RequestSequence) -> Option<RequestId> {
        let req_id = self.in_flights.remove(&seq).map(|entry| entry.request_id);

        if self.in_flights.is_empty() {
            self.throbber_timer
                .as_mut()
                .reset(Instant::now() + Duration::from_secs(3600 * 24));
        }

        req_id
    }

    fn next_request_sequence(&self) -> RequestSequence {
        self.next_request_sequence.fetch_add(1, Ordering::Relaxed)
    }
}

struct InFlightEntry {
    // request started at(approximate)
    start: Instant,
    request_id: RequestId,
}