summaryrefslogtreecommitdiff
path: root/exes/rest/src/ratelimit_client/remote_hashring.rs
blob: b9f780036fc8aba08db65ae0f03c0b464e53142c (plain)
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
use core::fmt::Debug;
use proto::nova::ratelimit::ratelimiter::ratelimiter_client::RatelimiterClient;
use std::hash::Hash;
use std::ops::Deref;
use std::ops::DerefMut;
use tonic::transport::Channel;

#[derive(Debug, Clone)]
pub struct VNode {
    address: String,

    client: RatelimiterClient<Channel>,
}

impl Deref for VNode {
    type Target = RatelimiterClient<Channel>;

    fn deref(&self) -> &Self::Target {
        &self.client
    }
}

impl DerefMut for VNode {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.client
    }
}

impl Hash for VNode {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.address.hash(state);
    }
}

impl VNode {
    pub async fn new(address: String) -> Result<Self, tonic::transport::Error> {
        let client = RatelimiterClient::connect(format!("http://{}:8080", address.clone())).await?;

        Ok(VNode { client, address })
    }
}

unsafe impl Send for VNode {}

#[repr(transparent)]
#[derive(Default)]
pub struct HashRingWrapper(hashring::HashRing<VNode>);

impl Deref for HashRingWrapper {
    type Target = hashring::HashRing<VNode>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for HashRingWrapper {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Debug for HashRingWrapper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("HashRing").finish()
    }
}