debot_db/
counter.rs

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
// counter.rs

pub enum CounterType {
    Position,
    Price,
    Pnl,
}

pub struct CounterData {
    max: Option<u32>,
    counter: std::sync::Mutex<u32>,
}

pub struct Counter {
    position: CounterData,
    price: CounterData,
    pnl: CounterData,
}

impl Counter {
    pub fn new(
        max_position_counter: Option<u32>,
        max_price_counter: Option<u32>,
        max_pnl_counter: Option<u32>,
        position_counter: u32,
        price_counter: u32,
        pnl_counter: u32,
    ) -> Self {
        Self {
            position: CounterData {
                max: max_position_counter,
                counter: std::sync::Mutex::new(position_counter),
            },
            price: CounterData {
                max: max_price_counter,
                counter: std::sync::Mutex::new(price_counter),
            },
            pnl: CounterData {
                max: max_pnl_counter,
                counter: std::sync::Mutex::new(pnl_counter),
            },
        }
    }

    pub fn increment(&self, counter_type: CounterType) -> u32 {
        let counter_data = match counter_type {
            CounterType::Position => &self.position,
            CounterType::Price => &self.price,
            CounterType::Pnl => &self.pnl,
        };

        let mut counter = counter_data.counter.lock().unwrap();
        *counter += 1;
        let mut id = *counter;
        if let Some(max_counter) = counter_data.max {
            id = *counter % max_counter;
            if id == 0 {
                id = 1;
            }
            *counter = id;
        }

        drop(counter);
        id
    }
}