Skip to content

Commit e8384cd

Browse files
committed
重新开始rust学习
1 parent 7e0d15e commit e8384cd

File tree

15 files changed

+405
-352
lines changed

15 files changed

+405
-352
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/target
22
Cargo.lock
3-
.idea
3+
.idea
4+
.zed
5+
.vscode

crates/polaris/src/cfg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/core/config/config.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
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-
use std::{env, fs, io};
17-
use std::path::Path;
18-
use serde::Deserialize;
1916
use crate::core::config::config_file::ConfigFileConfig;
2017
use crate::core::config::consumer::ConsumerConfig;
2118
use crate::core::config::global::GlobalConfig;
2219
use crate::core::config::provider::ProviderConfig;
20+
use serde::Deserialize;
21+
use std::path::Path;
22+
use std::{env, fs, io};
2323

2424
#[derive(Deserialize, Debug)]
2525
#[serde(rename_all = "camelCase", deny_unknown_fields)]
@@ -36,25 +36,26 @@ pub fn load_default<'a>() -> Result<Configuration, io::Error> {
3636
path = Path::new("./polaris.yml");
3737
}
3838
if env::var("POLARIS_RUST_CONFIG").is_ok() {
39-
return load(env::var("POLARIS_RUST_CONFIG").unwrap())
39+
return load(env::var("POLARIS_RUST_CONFIG").unwrap());
4040
}
41-
return load(path)
41+
return load(path);
4242
}
4343

4444
pub fn load<P: AsRef<Path>>(path: P) -> Result<Configuration, io::Error> {
4545
let val = fs::read_to_string(path);
4646
if val.is_ok() {
4747
let data = val.ok().unwrap();
48-
let config: Configuration = serde_yaml::from_str(&*data).expect(&format!("failure to format yaml str {}", &data));
49-
return Ok(config)
48+
let config: Configuration =
49+
serde_yaml::from_str(&*data).expect(&format!("failure to format yaml str {}", &data));
50+
return Ok(config);
5051
}
51-
return Err(val.err().unwrap())
52+
return Err(val.err().unwrap());
5253
}
5354

5455
#[cfg(test)]
5556
mod tests {
56-
use std::env;
5757
use crate::core::config::config::load;
58+
use std::env;
5859

5960
#[test]
6061
fn test_load() {

src/core/engine.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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
@@ -19,14 +19,15 @@ use crate::core::model::error::PolarisError;
1919
use crate::core::model::naming::InstanceRequest;
2020
use crate::core::plugin::connector::Connector;
2121
use crate::core::plugin::plugins::Extensions;
22-
use crate::discovery::req::{InstanceDeregisterRequest, InstanceRegisterRequest, InstanceRegisterResponse};
22+
use crate::discovery::req::{
23+
InstanceDeregisterRequest, InstanceRegisterRequest, InstanceRegisterResponse,
24+
};
2325

2426
pub struct Engine {
25-
extensions: Extensions
27+
extensions: Extensions,
2628
}
2729

2830
impl Engine {
29-
3031
pub fn new() -> Self {
3132
Self {
3233
extensions: Extensions::default(),
@@ -41,40 +42,44 @@ impl Engine {
4142
return Ok(true);
4243
}
4344

44-
pub(crate) fn sync_register_instance(&mut self, req: InstanceRegisterRequest) -> Result<InstanceRegisterResponse, PolarisError> {
45-
let mut connector = self.extensions.get_active_connector(DISCOVER_SERVER_CONNECTOR);
46-
let rsp = connector.register_instance(InstanceRequest{
45+
pub(crate) fn sync_register_instance(
46+
&mut self,
47+
req: InstanceRegisterRequest,
48+
) -> Result<InstanceRegisterResponse, PolarisError> {
49+
let connector = self
50+
.extensions
51+
.get_active_connector(DISCOVER_SERVER_CONNECTOR);
52+
let rsp = connector.register_instance(InstanceRequest {
4753
flow_id: req.flow_id.clone(),
4854
ttl: req.ttl.clone(),
4955
instance: req.convert_instance(),
5056
});
5157

5258
return match rsp {
53-
Ok(ins_rsp) => {
54-
Ok(InstanceRegisterResponse{ instance_id: ins_rsp.instance.ip.clone(), exist: ins_rsp.exist.clone() })
55-
}
56-
Err(err) => {
57-
Err(err)
58-
}
59-
}
59+
Ok(ins_rsp) => Ok(InstanceRegisterResponse {
60+
instance_id: ins_rsp.instance.ip.clone(),
61+
exist: ins_rsp.exist.clone(),
62+
}),
63+
Err(err) => Err(err),
64+
};
6065
}
6166

62-
pub(crate) fn sync_deregister_instance(&mut self, req: InstanceDeregisterRequest) -> Result<(), PolarisError> {
63-
let mut connector = self.extensions.get_active_connector(DISCOVER_SERVER_CONNECTOR);
64-
let rsp = connector.deregister_instance(InstanceRequest{
67+
pub(crate) fn sync_deregister_instance(
68+
&mut self,
69+
req: InstanceDeregisterRequest,
70+
) -> Result<(), PolarisError> {
71+
let mut connector = self
72+
.extensions
73+
.get_active_connector(DISCOVER_SERVER_CONNECTOR);
74+
let rsp = connector.deregister_instance(InstanceRequest {
6575
flow_id: req.flow_id.clone(),
6676
ttl: 0,
6777
instance: req.convert_instance(),
6878
});
6979

7080
return match rsp {
71-
Ok(ins_rsp) => {
72-
Ok(())
73-
}
74-
Err(err) => {
75-
Err(err)
76-
}
77-
}
81+
Ok(ins_rsp) => Ok(()),
82+
Err(err) => Err(err),
83+
};
7884
}
79-
8085
}

src/core/model/cluster.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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
@@ -20,13 +20,13 @@ use std::time::Duration;
2020
pub static BUILDIN_SERVER_NAMESPACE: &str = "Polaris";
2121
pub static BUILDIN_SERVER_SERVICE: &str = "polaris.builtin";
2222

23-
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
23+
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Hash)]
2424
pub enum ClusterType {
2525
#[default]
2626
BuildIn,
2727
Discover,
2828
Config,
29-
HealthCheck
29+
HealthCheck,
3030
}
3131

3232
impl Display for ClusterType {

src/core/plugin/connector.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
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-
use std::collections::HashMap;
17-
use std::sync::Arc;
1816
use crate::core::model::error::{ErrorCode, PolarisError};
1917
use crate::core::model::naming::{InstanceRequest, InstanceResponse};
2018
use crate::core::plugin::plugins::{Extensions, Plugin};
19+
use std::collections::HashMap;
20+
use std::sync::Arc;
2121

2222
pub trait Connector: Plugin {
23-
2423
// register_resource_handler 注册资源处理器
2524
fn register_resource_handler(&self) -> Result<bool, PolarisError>;
2625

2726
// deregister_resource_handler
2827
fn deregister_resource_handler(&self) -> Result<bool, PolarisError>;
2928

30-
fn register_instance(&mut self, req: InstanceRequest) -> Result<InstanceResponse, PolarisError>;
29+
fn register_instance(&self, req: InstanceRequest) -> Result<InstanceResponse, PolarisError>;
3130

32-
fn deregister_instance(&mut self, req: InstanceRequest) -> Result<bool, PolarisError>;
31+
fn deregister_instance(&self, req: InstanceRequest) -> Result<bool, PolarisError>;
3332

3433
fn heartbeat_instance(&self, req: InstanceRequest) -> Result<bool, PolarisError>;
3534

@@ -48,18 +47,16 @@ pub trait Connector: Plugin {
4847
fn upsert_publish_config_file(&self) -> Result<bool, PolarisError>;
4948
}
5049

51-
pub struct NoopConnector {
52-
53-
}
50+
pub struct NoopConnector {}
5451

5552
impl Default for NoopConnector {
5653
fn default() -> Self {
57-
Self{}
54+
Self {}
5855
}
5956
}
6057

6158
impl Plugin for NoopConnector {
62-
fn init(&self, options: HashMap<String, String>, extensions: Arc<Extensions>) {
59+
fn init(&mut self, options: HashMap<String, String>, extensions: Arc<Extensions>) {
6360
todo!()
6461
}
6562

@@ -81,11 +78,11 @@ impl Connector for NoopConnector {
8178
todo!()
8279
}
8380

84-
fn register_instance(&mut self, req: InstanceRequest) -> Result<InstanceResponse, PolarisError> {
81+
fn register_instance(&self, req: InstanceRequest) -> Result<InstanceResponse, PolarisError> {
8582
todo!()
8683
}
8784

88-
fn deregister_instance(&mut self, req: InstanceRequest) -> Result<bool, PolarisError> {
85+
fn deregister_instance(&self, req: InstanceRequest) -> Result<bool, PolarisError> {
8986
todo!()
9087
}
9188

0 commit comments

Comments
 (0)