Skip to content

Commit 7e0d15e

Browse files
committed
stash code
1 parent 8db1ec7 commit 7e0d15e

40 files changed

+7133
-2685
lines changed

Cargo.toml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
[package]
2+
edition = "2021"
23
name = "polaris-rust"
34
version = "0.1.0"
4-
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
[dependencies]
8-
serde = { version = "1.0.198", features = ["derive"] }
9-
serde_json = "1.0.116"
10-
serde-duration-ext = "0.1.0"
11-
schemars = "0.8.16"
12-
serde_yaml = "0.9.34"
138
bytes = {version = "1.4.0"}
14-
prost = {version = "0.12.4" }
15-
prost-types = {version = "0.12.4" }
16-
prost-build = {version = "0.12.4" }
9+
schemars = {version = "0.8.16"}
10+
serde = {version = "1.0.198", features = ["derive"]}
11+
serde-duration-ext = {version = "0.1.0"}
12+
serde_json = {version = "1.0.116"}
13+
serde_yaml = {version = "0.9.34"}
14+
15+
uuid = {version = "1.8.0", features = [
16+
"v4", # Lets you generate random UUIDs
17+
"fast-rng", # Use a faster (but still sufficiently random) RNG
18+
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
19+
]}
20+
21+
# async
22+
tokio = {version = "1.37.0", features = ["full"]}
23+
24+
tower = {version = "0.4.13"}
25+
26+
# gRPC dep
27+
log = {version = "0.4.21"}
28+
once_cell = {version = "1.19.0"}
29+
prost = {version = "0.12.4"}
30+
prost-build = {version = "0.12.4"}
31+
prost-types = {version = "0.12.4"}
32+
tonic = {version = "0.11.0"}
33+
futures = "0.3.30"

src/core/config/config.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1414
// specific language governing permissions and limitations under the License.
1515

16-
use std::{fs, io};
16+
use std::{env, fs, io};
17+
use std::path::Path;
1718
use serde::Deserialize;
1819
use crate::core::config::config_file::ConfigFileConfig;
1920
use crate::core::config::consumer::ConsumerConfig;
2021
use crate::core::config::global::GlobalConfig;
2122
use crate::core::config::provider::ProviderConfig;
2223

23-
#[derive(Deserialize)]
24+
#[derive(Deserialize, Debug)]
2425
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2526
pub struct Configuration {
2627
pub global: GlobalConfig,
@@ -29,12 +30,23 @@ pub struct Configuration {
2930
pub config: ConfigFileConfig,
3031
}
3132

32-
pub fn load(path: String) -> Result<*const Configuration, io::Error> {
33+
pub fn load_default<'a>() -> Result<Configuration, io::Error> {
34+
let mut path = Path::new("./polaris.yaml");
35+
if !path.exists() {
36+
path = Path::new("./polaris.yml");
37+
}
38+
if env::var("POLARIS_RUST_CONFIG").is_ok() {
39+
return load(env::var("POLARIS_RUST_CONFIG").unwrap())
40+
}
41+
return load(path)
42+
}
43+
44+
pub fn load<P: AsRef<Path>>(path: P) -> Result<Configuration, io::Error> {
3345
let val = fs::read_to_string(path);
3446
if val.is_ok() {
3547
let data = val.ok().unwrap();
3648
let config: Configuration = serde_yaml::from_str(&*data).expect(&format!("failure to format yaml str {}", &data));
37-
return Ok(&config)
49+
return Ok(config)
3850
}
3951
return Err(val.err().unwrap())
4052
}

src/core/config/config_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use serde::Deserialize;
1717

18-
#[derive(Deserialize)]
18+
#[derive(Deserialize, Debug)]
1919
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2020
pub struct ConfigFileConfig {
2121
pub properties_value_cache_size: u32,

src/core/config/consumer.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,39 @@
1313
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1414
// specific language governing permissions and limitations under the License.
1515

16-
use std::time::Duration;
1716
use schemars::Map;
1817
use serde::Deserialize;
1918

20-
#[derive(Deserialize)]
19+
#[derive(Deserialize, Debug)]
2120
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2221
pub struct ConsumerConfig {
2322
pub service_router: ServiceRouterConfig,
2423
pub circuit_breaker: CircuitBreakerConfig,
2524
pub load_balancer: LoadBalancerConfig,
2625
}
2726

28-
#[derive(Deserialize)]
27+
#[derive(Deserialize, Debug)]
2928
#[serde(rename_all = "camelCase", deny_unknown_fields)]
3029
pub struct ServiceRouterConfig {
3130
pub before_chain: Vec<ServiceRouterPluginConfig>,
3231
pub core_chain: Vec<ServiceRouterPluginConfig>,
3332
pub after_chain: Vec<ServiceRouterPluginConfig>,
3433
}
3534

36-
#[derive(Deserialize)]
35+
#[derive(Deserialize, Debug)]
3736
#[serde(rename_all = "camelCase", deny_unknown_fields)]
3837
pub struct ServiceRouterPluginConfig {
3938
pub name: String,
4039
pub options: Option<Map<String, String>>
4140
}
4241

43-
#[derive(Deserialize)]
42+
#[derive(Deserialize, Debug)]
4443
#[serde(rename_all = "camelCase", deny_unknown_fields)]
4544
pub struct LoadBalancerConfig {
4645
pub plugins: Option<Vec<String>>
4746
}
4847

49-
#[derive(Deserialize)]
48+
#[derive(Deserialize, Debug)]
5049
#[serde(rename_all = "camelCase", deny_unknown_fields)]
5150
pub struct CircuitBreakerConfig {
5251
pub enable: bool,

src/core/config/global.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
// Tencent is pleased to support the open source community by making Polaris available.
2-
//
2+
//
33
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4-
//
4+
//
55
// Licensed under the BSD 3-Clause License (the "License");
66
// you may not use this file except in compliance with the License.
77
// You may obtain a copy of the License at
8-
//
8+
//
99
// https://opensource.org/licenses/BSD-3-Clause
10-
//
10+
//
1111
// Unless required by applicable law or agreed to in writing, software distributed
1212
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
1313
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1414
// specific language governing permissions and limitations under the License.
1515

16-
17-
use std::collections::{BTreeMap, HashMap};
16+
use serde::Deserialize;
17+
use std::collections::HashMap;
1818
use std::time::Duration;
19-
use schemars::Map;
20-
use serde::{Serialize, Deserialize};
2119

22-
#[derive(Deserialize)]
20+
#[derive(Deserialize, Debug)]
2321
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2422
pub struct GlobalConfig {
2523
pub system: SystemConfig,
2624
pub api: APIConfig,
27-
pub server_connectors: Map<String, ServerConnectorConfig>,
25+
pub server_connectors: HashMap<String, ServerConnectorConfig>,
2826
pub stat_reporter: StatReporterConfig,
2927
pub location: LocationConfig,
30-
pub local_cache: LocalCacheConfig
28+
pub local_cache: LocalCacheConfig,
29+
}
30+
31+
impl GlobalConfig {
32+
pub fn update_server_connector_address(&mut self, name: &str, addresses: Vec<String>) {
33+
let mut binding = self.server_connectors.get_mut(name);
34+
let mut connector = binding.unwrap();
35+
connector.update_addresses(addresses);
36+
}
3137
}
3238

33-
#[derive(Deserialize)]
39+
#[derive(Deserialize, Debug)]
3440
#[serde(rename_all = "camelCase", deny_unknown_fields)]
3541
pub struct SystemConfig {
3642
pub discover_cluster: Option<ClusterConfig>,
3743
pub config_cluster: Option<ClusterConfig>,
3844
pub health_check_cluster: Option<ClusterConfig>,
39-
pub variables: Option<Map<String, String>>,
45+
pub variables: Option<HashMap<String, String>>,
4046
}
4147

42-
#[derive(Deserialize)]
48+
#[derive(Deserialize, Debug)]
4349
#[serde(rename_all = "camelCase", deny_unknown_fields)]
4450
pub struct APIConfig {
4551
#[serde(with = "serde_duration_ext")]
@@ -53,7 +59,10 @@ pub struct APIConfig {
5359
pub report_interval: Duration,
5460
}
5561

56-
#[derive(Deserialize)]
62+
pub static DISCOVER_SERVER_CONNECTOR: &str = "discover";
63+
pub static CONFIG_SERVER_CONNECTOR: &str = "config";
64+
65+
#[derive(Deserialize, Debug)]
5766
#[serde(rename_all = "camelCase", deny_unknown_fields)]
5867
pub struct ServerConnectorConfig {
5968
pub addresses: Vec<String>,
@@ -68,56 +77,68 @@ pub struct ServerConnectorConfig {
6877
pub connection_idle_timeout: Duration,
6978
#[serde(with = "serde_duration_ext")]
7079
pub reconnect_interval: Duration,
71-
pub metadata: Option<Map<String, String>>,
80+
pub metadata: Option<HashMap<String, String>>,
7281
pub ssl: Option<SSL>,
7382
pub token: Option<String>,
7483
}
7584

76-
#[derive(Deserialize)]
85+
impl ServerConnectorConfig {
86+
87+
pub fn get_protocol(&self) -> String {
88+
return self.protocol.clone()
89+
}
90+
91+
pub fn update_addresses(&mut self, addresses: Vec<String>) {
92+
self.addresses.clear();
93+
self.addresses.extend(addresses);
94+
}
95+
}
96+
97+
#[derive(Deserialize, Debug)]
7798
#[serde(rename_all = "camelCase", deny_unknown_fields)]
7899
pub struct SSL {
79100
pub trusted_ca_file: String,
80101
pub cert_file: String,
81102
pub key_file: String,
82103
}
83104

84-
#[derive(Deserialize)]
105+
#[derive(Deserialize, Debug)]
85106
#[serde(rename_all = "camelCase", deny_unknown_fields)]
86107
pub struct StatReporterConfig {
87108
pub enable: bool,
88109
pub chain: Option<Vec<StatReporterPluginConfig>>,
89110
}
90111

91-
#[derive(Deserialize)]
112+
#[derive(Deserialize, Debug)]
92113
#[serde(rename_all = "camelCase", deny_unknown_fields)]
93114
pub struct StatReporterPluginConfig {
94115
pub name: String,
95-
pub options: Option<Map<String, String>>
116+
pub options: Option<HashMap<String, String>>,
96117
}
97118

98-
#[derive(Deserialize)]
119+
#[derive(Deserialize, Debug)]
99120
#[serde(rename_all = "camelCase", deny_unknown_fields)]
100121
pub struct LocationConfig {
101-
pub providers: Option<Vec<LocationProviderConfig>>
122+
pub providers: Option<Vec<LocationProviderConfig>>,
102123
}
103124

104125
fn default_location_providers() -> Vec<LocationProviderConfig> {
105126
let mut providers = Vec::new();
106-
providers.push(LocationProviderConfig{
127+
providers.push(LocationProviderConfig {
107128
name: "local".to_string(),
108-
options: BTreeMap::new(),
129+
options: HashMap::new(),
109130
});
110131
return providers;
111132
}
112133

113-
#[derive(Deserialize)]
134+
#[derive(Deserialize, Debug)]
114135
#[serde(rename_all = "camelCase", deny_unknown_fields)]
115136
pub struct LocationProviderConfig {
116137
pub name: String,
117-
pub options: Map<String, String>,
138+
pub options: HashMap<String, String>,
118139
}
119140

120-
#[derive(Deserialize)]
141+
#[derive(Deserialize, Debug)]
121142
#[serde(rename_all = "camelCase", deny_unknown_fields)]
122143
pub struct ClusterConfig {
123144
pub namespace: Option<String>,
@@ -128,7 +149,7 @@ pub struct ClusterConfig {
128149
pub lb_policy: String,
129150
}
130151

131-
#[derive(Deserialize)]
152+
#[derive(Deserialize, Debug)]
132153
#[serde(rename_all = "camelCase", deny_unknown_fields)]
133154
pub struct LocalCacheConfig {
134155
#[serde(default = "default_local_cache_name")]

src/core/config/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
1313
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1414
// specific language governing permissions and limitations under the License.
15-
mod config;
16-
mod consumer;
17-
mod global;
18-
mod provider;
19-
mod config_file;
15+
pub mod config;
16+
pub mod consumer;
17+
pub mod global;
18+
pub mod provider;
19+
pub mod config_file;

src/core/config/provider.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use std::time::Duration;
1818
use serde::Deserialize;
1919

20-
#[derive(Deserialize)]
20+
#[derive(Deserialize, Debug)]
2121
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2222
pub struct ProviderConfig {
2323
pub rate_limit: RateLimitConfig,
@@ -31,7 +31,7 @@ fn default_min_register_interval() -> Duration {
3131
Duration::from_secs(30)
3232
}
3333

34-
#[derive(Deserialize)]
34+
#[derive(Deserialize, Debug)]
3535
#[serde(rename_all = "camelCase", deny_unknown_fields)]
3636
pub struct RateLimitConfig {
3737
pub enable: bool,
@@ -47,7 +47,7 @@ pub struct RateLimitConfig {
4747
pub report_metrics: bool
4848
}
4949

50-
#[derive(Deserialize)]
50+
#[derive(Deserialize, Debug)]
5151
#[serde(rename_all = "camelCase", deny_unknown_fields)]
5252
pub struct LosslessConfig {
5353
pub enable: bool,

0 commit comments

Comments
 (0)