rocketmq_common/utils/
name_server_address_utils.rs1use std::env;
18
19use lazy_static::lazy_static;
20use regex::Regex;
21
22use crate::common::mix_all;
23
24const INSTANCE_PREFIX: &str = "MQ_INST_";
25const INSTANCE_REGEX: &str = "MQ_INST_\\w+_\\w+";
26const ENDPOINT_PREFIX: &str = "(\\w+://|)";
27
28lazy_static! {
29 pub static ref NAMESRV_ENDPOINT_PATTERN: Regex = Regex::new("^http://.*").unwrap();
30 pub static ref INST_ENDPOINT_PATTERN: Regex =
31 Regex::new(&format!("^{ENDPOINT_PREFIX}{INSTANCE_REGEX}")).unwrap();
32}
33pub struct NameServerAddressUtils;
34
35impl NameServerAddressUtils {
36 pub fn get_name_server_addresses() -> Option<String> {
37 env::var(mix_all::NAMESRV_ADDR_PROPERTY)
38 .or_else(|_| env::var(mix_all::NAMESRV_ADDR_ENV))
39 .ok()
40 }
41
42 pub fn validate_instance_endpoint(endpoint: &str) -> bool {
43 INST_ENDPOINT_PATTERN.is_match(endpoint)
44 }
45
46 pub fn parse_instance_id_from_endpoint(endpoint: &str) -> Option<String> {
47 if endpoint.is_empty() {
48 None
49 } else {
50 let last_slash = endpoint.rfind('/').unwrap_or(0);
51 let dot_pos = endpoint.find('.').unwrap_or(endpoint.len());
52 Some(endpoint[last_slash + 1..dot_pos].to_string())
53 }
54 }
55
56 pub fn get_name_srv_addr_from_namesrv_endpoint(name_srv_endpoint: &str) -> Option<String> {
57 if name_srv_endpoint.is_empty() {
58 None
59 } else {
60 let last_slash = name_srv_endpoint.rfind('/').unwrap_or(0);
61 Some(name_srv_endpoint[last_slash + 1..].to_string())
62 }
63 }
64}