-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetcd_client.rs
174 lines (155 loc) · 6.67 KB
/
etcd_client.rs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::sync::Arc;
use anyhow::anyhow;
use etcd_client::{ConnectOptions, EventType, GetOptions, KeyValue, WatchOptions};
use log::{debug, error, info, warn};
use crate::config::EtcdConfig;
use crate::router::etcd::RouteArray;
use crate::router::Route;
use crate::wrapped_etcd_client::WrappedEtcdClient;
#[derive(Clone)]
pub struct EtcdClient {
pub etcd_config: EtcdConfig,
pub client: Arc<WrappedEtcdClient>,
pub prefix: String,
pub interval: u64,
}
impl EtcdClient {
pub async fn new(etcd_config: EtcdConfig) -> anyhow::Result<Self> {
let config = etcd_config.clone();
let etcd_client = Arc::new(WrappedEtcdClient::connect(
etcd_config.endpoints,
Some(ConnectOptions::new()
.with_timeout(std::time::Duration::from_millis(etcd_config.timeout))
.with_user(etcd_config.username, etcd_config.password)
),
true
).await?);
Ok(Self{
etcd_config: config,
client: etcd_client,
prefix: etcd_config.prefix,
interval: etcd_config.interval,
})
}
pub fn list_watch_routes(&self, route_name: String, routes: RouteArray, update_tx: tokio::sync::mpsc::Sender<()>) {
let watch_prefix = format!("{}/{}/", self.prefix, route_name);
let clone_self = self.clone();
tokio::spawn({
async move {
loop {
let routes = routes.clone();
let revision = clone_self.list_routes(&watch_prefix, routes.clone()).await;
if revision.is_err() {
error!("etcd router {} list error: {:?}, will retry later.", watch_prefix, revision.err());
tokio::time::sleep(std::time::Duration::from_millis(clone_self.interval)).await;
continue
}
let update_result = update_tx.send(()).await;
if update_result.is_err() {
error!("etcd router {} update_tx send error: {:?}, will retry later.", watch_prefix, update_result.err());
tokio::time::sleep(std::time::Duration::from_millis(clone_self.interval)).await;
continue
}
if let Err(err) = clone_self.watch_routes(&watch_prefix, revision.unwrap(), routes, update_tx.clone()).await {
error!("etcd router {} watch error: {:?}, will retry later.", watch_prefix, err);
}
tokio::time::sleep(std::time::Duration::from_millis(clone_self.interval)).await;
}
}
});
}
async fn list_routes(&self, route_keys: &str, routes: RouteArray) -> anyhow::Result<i64> {
let list_resp = self.client.get(
route_keys,
Some(GetOptions::new().with_prefix())
).await?;
list_resp.kvs().iter().for_each(|kv| {
let (key, route) = Self::decode_kv(kv);
if key.is_ok() && route.is_ok() {
routes.insert(key.unwrap(), route.unwrap());
}
});
if let Some(header) = list_resp.header() {
return Ok(header.revision())
}
Err(anyhow!("etcd list header none"))
}
async fn watch_routes(
&self,
route_keys: &str,
revision: i64,
routes: RouteArray,
update_tx: tokio::sync::mpsc::Sender<()>
) -> anyhow::Result<()> {
let (_, mut watch_stream) = self.client.watch(
route_keys, Some(WatchOptions::new().with_prefix().with_start_revision(revision))).await?;
while let Some(resp) = watch_stream.message().await? {
debug!("etcd router {} watch get resp: {:?}", route_keys, resp);
if resp.created() {
continue
}
if resp.canceled() {
info!("etcd router {} watch canceled: {}, will relist and watch later", route_keys, resp.cancel_reason());
break;
}
for event in resp.events() {
debug!("etcd router {} watch get event: {:?}", route_keys, event);
let event_type = event.event_type();
match event_type {
EventType::Put => {
if let Some(kv) = event.kv() {
let (key, route) = Self::decode_kv(kv);
if key.is_ok() && route.is_ok() {
routes.insert(key.unwrap(), route.unwrap());
}
}
}
EventType::Delete => {
if let Some(kv) = event.kv() {
let key = Self::decode_k(kv);
if key.is_ok() {
routes.remove(&key.unwrap());
}
}
}
}
}
update_tx.send(()).await?;
}
Ok(())
}
fn decode_k(kv: &KeyValue) -> anyhow::Result<String> {
let key = kv.key_str();
if key.is_err(){
warn!("etcd key or value is not correct, will ignore it!");
return Err(anyhow!("etcd key or value is not correct"));
}
let key = key.unwrap();
let key_split: Vec<&str> = key.split('/').collect::<Vec<_>>();
if key_split.len() != 4 { // pattern must be /prefix/router_name/route_id
return Err(anyhow!("etcd key is not correct"));
}
Ok(key_split[3].to_string())
}
fn decode_kv(kv: &KeyValue) -> (anyhow::Result<String>, anyhow::Result<Route>) {
let key = kv.key_str();
let value = kv.value_str();
if key.is_err() || value.is_err(){
warn!("etcd key or value is not correct, will ignore it!");
return (Err(anyhow!("etcd key or value is not correct")), Err(anyhow!("etcd key or value is not correct")))
}
let key = key.unwrap();
let key_split: Vec<&str> = key.split('/').collect::<Vec<_>>();
if key_split.len() != 4 { // pattern must be /prefix/router_name/route_id
return (Err(anyhow!("etcd key is not correct")), Err(anyhow!("etcd key is not correct")))
}
let key = key_split[3].to_string();
let value = value.unwrap();
let route: serde_json::error::Result<Route> = serde_json::from_str(value);
if route.is_err() {
warn!("etcd value is not correct, will ignore it: key: {}, value: {}", key, value);
return (Ok(key), Err(anyhow!("etcd value is not correct")))
}
(Ok(key), Ok(route.unwrap()))
}
}