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
use get_if_addrs::get_if_addrs;

use crate::com::ClusterConfig;
use std::cell::RefCell;
use std::env;
use std::net::IpAddr;

thread_local!(static TLS_META: RefCell<Option<Meta>> = RefCell::new(None));

#[derive(Debug, Clone)]
pub struct Meta {
    cluster: String,
    port: String,
    ip: String,
}

pub fn get_if_addr() -> String {
    if let Ok(if_adrs) = get_if_addrs() {
        for iface in if_adrs {
            if iface.is_loopback() {
                continue;
            }
            let addr = iface.ip();
            if !addr.is_unspecified() && addr.is_ipv4() && !addr.is_loopback() {
                return addr.to_string();
            }
        }
    }
    // get from env
    if let Ok(host_ip) = env::var("HOST") {
        if let Ok(addr) = host_ip.parse::<IpAddr>() {
            if !addr.is_unspecified() && addr.is_ipv4() && !addr.is_loopback() {
                return addr.to_string();
            }
        }
    }
    "127.0.0.1".to_string()
}

pub fn load_meta(cc: ClusterConfig, ip: Option<String>) -> Meta {
    let port = cc
        .listen_addr
        .split(':')
        .nth(1)
        .expect("listen_addr must contains port")
        .to_string();

    let ip = ip.unwrap_or_else(get_if_addr);

    Meta {
        cluster: cc.name,
        port,
        ip,
    }
}

pub fn meta_init(meta: Meta) {
    TLS_META.with(|gkd| {
        let mut handler = gkd.borrow_mut();
        handler.replace(meta);
    });
}

pub fn get_ip() -> String {
    TLS_META.with(|gkd| {
        gkd.borrow()
            .as_ref()
            .map(|x| x.ip.clone())
            .expect("get_ip must be called after init")
    })
}

pub fn get_port() -> String {
    TLS_META.with(|gkd| {
        gkd.borrow()
            .as_ref()
            .map(|x| x.port.clone())
            .expect("get_ip must be called after init")
    })
}

pub fn get_cluster() -> String {
    TLS_META.with(|gkd| {
        gkd.borrow()
            .as_ref()
            .map(|x| x.cluster.clone())
            .expect("get_ip must be called after init")
    })
}