Skip to content

Commit 50bbe9d

Browse files
committed
Fix UDP access conflict #6
1 parent 08906f3 commit 50bbe9d

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

src/exporter.rs

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use hyper::service::service_fn_ok;
55
use hyper::{Body, Response, Server, Uri};
66
use lazy_static::lazy_static;
77
use prometheus::{Encoder, Gauge, GaugeVec, Opts, Registry, TextEncoder};
8+
use std::sync::{Arc, Mutex};
89
use url::form_urlencoded;
910

1011
// ---------------------------------------------------------------------------------------------------------------------
@@ -66,11 +67,15 @@ impl Exporter {
6667
println!("Server started: {:?}", addr);
6768
}
6869

70+
let mutex = Arc::new(Mutex::new(()));
71+
6972
let service = move || {
73+
let mutex = Arc::clone(&mutex);
7074
service_fn_ok(move |req| {
75+
let mutex = Arc::clone(&mutex);
7176
let uri = req.uri();
7277
if uri.path() == "/probe" {
73-
Exporter::probe(uri, verbose)
78+
Exporter::probe(uri, verbose, mutex)
7479
} else {
7580
Response::new(Body::from(LANDING_PAGE))
7681
}
@@ -87,7 +92,7 @@ impl Exporter {
8792
}
8893

8994
#[cfg_attr(tarpaulin, skip)]
90-
fn probe(uri: &Uri, verbose: bool) -> Response<Body> {
95+
fn probe(uri: &Uri, verbose: bool, mutex: Arc<Mutex<()>>) -> Response<Body> {
9196
let registry = Registry::new();
9297

9398
let build_info = GaugeVec::new(
@@ -132,46 +137,51 @@ impl Exporter {
132137
if verbose {
133138
println!("Access to switch: {} though {}", host, if_name);
134139
}
135-
let sw = ProSafeSwitch::new(&host, &if_name);
136-
match sw.port_stat() {
137-
Ok(stats) => {
138-
for s in stats.stats {
139-
receive_bytes
140-
.with_label_values(&[&format!("{}", s.port_no)])
141-
.set(s.recv_bytes as f64);
142-
transmit_bytes
143-
.with_label_values(&[&format!("{}", s.port_no)])
144-
.set(s.send_bytes as f64);
145-
error_packets
146-
.with_label_values(&[&format!("{}", s.port_no)])
147-
.set(s.error_pkts as f64);
148-
}
149140

150-
up.set(1.0);
151-
}
152-
Err(x) => {
153-
up.set(0.0);
154-
eprintln!("Fail to access: {}", x);
155-
}
156-
}
157-
match sw.speed_stat() {
158-
Ok(stats) => {
159-
for s in stats.stats {
160-
let speed = match s.link {
161-
Link::None => 0,
162-
Link::Speed10Mbps => 10,
163-
Link::Speed100Mbps => 100,
164-
Link::Speed1Gbps => 1000,
165-
Link::Speed10Gbps => 10000,
166-
Link::Unknown => 0,
167-
};
168-
link_speed
169-
.with_label_values(&[&format!("{}", s.port_no)])
170-
.set(f64::from(speed));
141+
{
142+
let _guard = mutex.lock();
143+
144+
let sw = ProSafeSwitch::new(&host, &if_name);
145+
match sw.port_stat() {
146+
Ok(stats) => {
147+
for s in stats.stats {
148+
receive_bytes
149+
.with_label_values(&[&format!("{}", s.port_no)])
150+
.set(s.recv_bytes as f64);
151+
transmit_bytes
152+
.with_label_values(&[&format!("{}", s.port_no)])
153+
.set(s.send_bytes as f64);
154+
error_packets
155+
.with_label_values(&[&format!("{}", s.port_no)])
156+
.set(s.error_pkts as f64);
157+
}
158+
159+
up.set(1.0);
160+
}
161+
Err(x) => {
162+
up.set(0.0);
163+
eprintln!("Fail to access: {}", x);
171164
}
172165
}
173-
Err(x) => {
174-
eprintln!("Fail to access: {}", x);
166+
match sw.speed_stat() {
167+
Ok(stats) => {
168+
for s in stats.stats {
169+
let speed = match s.link {
170+
Link::None => 0,
171+
Link::Speed10Mbps => 10,
172+
Link::Speed100Mbps => 100,
173+
Link::Speed1Gbps => 1000,
174+
Link::Speed10Gbps => 10000,
175+
Link::Unknown => 0,
176+
};
177+
link_speed
178+
.with_label_values(&[&format!("{}", s.port_no)])
179+
.set(f64::from(speed));
180+
}
181+
}
182+
Err(x) => {
183+
eprintln!("Fail to access: {}", x);
184+
}
175185
}
176186
}
177187
}

0 commit comments

Comments
 (0)