Skip to content

Commit 663a69b

Browse files
committed
add sync_host to DfdaemonUploadClient and optimize DfdaemonUpload.sync_host
Signed-off-by: baowj <[email protected]>
1 parent 9130e6b commit 663a69b

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

dragonfly-client/src/grpc/dfdaemon_upload.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use tonic::{
6363
Code, Request, Response, Status,
6464
};
6565
use tower::ServiceBuilder;
66-
use tracing::{error, info, instrument, Instrument, Span};
66+
use tracing::{debug, error, info, instrument, Instrument, Span};
6767
use url::Url;
6868

6969
/// DfdaemonUploadServer is the grpc server of the upload.
@@ -911,17 +911,13 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
911911
&self,
912912
request: Request<SyncHostRequest>,
913913
) -> Result<Response<Self::SyncHostStream>, Status> {
914-
/// DEFAULT_INTERFACE_SPEED is the default speed for interfaces.
915-
const DEFAULT_INTERFACE_SPEED: ByteSize = ByteSize::mb(10000 / 8);
916-
/// FIRST_HOST_INFO_REFRESH_INTERVAL is the interval for the first refresh of the host info
917-
/// when start up.
918-
const FIRST_HOST_INFO_REFRESH_INTERVAL: Duration = Duration::from_millis(100);
919-
/// DEFAULT_HOST_INFO_REFRESH_INTERVAL is the default interval for refreshing the host info.
920-
const DEFAULT_HOST_INFO_REFRESH_INTERVAL: Duration = Duration::from_secs(1);
921-
/// MILLISECONDS_PER_SECOND is the number of milliseconds contained per second.
922-
const MILLISECONDS_PER_SECOND: u64 = 1_000;
923-
/// BITS_PER_BYTE is the number of bits contained per byte.
924-
const BITS_PER_BYTE: u64 = 8;
914+
// DEFAULT_HOST_INFO_REFRESH_INTERVAL is the default interval for refreshing the host info.
915+
const DEFAULT_HOST_INFO_REFRESH_INTERVAL: Duration = Duration::from_millis(500);
916+
917+
/// bits_to_bytes converts network speed from bits/sec to bytes/sec.
918+
fn bits_to_bytes(bits_per_sec: u64) -> u64 {
919+
bits_per_sec / 8
920+
}
925921

926922
// Get request ip.
927923
let request_ip = request.remote_addr();
@@ -950,7 +946,7 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
950946

951947
// Get the interface and the interface speed of this request ip.
952948
let mut request_interface = None;
953-
let mut request_interface_speed = DEFAULT_INTERFACE_SPEED.as_u64();
949+
let mut request_interface_speed: u64 = 0;
954950
if let Some(request_ip) = request_ip {
955951
// Get the interface of this request ip.
956952
let interfaces = datalink::interfaces();
@@ -966,14 +962,13 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
966962
let speed_path = format!("/sys/class/net/{}/speed", interface);
967963
let content = fs::read_to_string(speed_path).unwrap_or_default();
968964
if let Ok(speed) = content.trim().parse::<u64>() {
969-
// Convert byte/Sec to bit/Sec.
965+
// Convert bits/sec to bytes/sec.
966+
let speed = ByteSize::mb(bits_to_bytes(speed));
970967
info!(
971968
"interface {} speed is {} for request ip {}",
972-
&interface,
973-
ByteSize::mb(speed / BITS_PER_BYTE),
974-
request_ip,
969+
&interface, speed, request_ip,
975970
);
976-
request_interface_speed = ByteSize::mb(speed / BITS_PER_BYTE).as_u64();
971+
request_interface_speed = speed.as_u64();
977972
}
978973
}
979974
}
@@ -986,20 +981,17 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
986981
// Initialize sysinfo network.
987982
let mut networks = Networks::new_with_refreshed_list();
988983
let mut last_refresh_time = SystemTime::now();
989-
// Sleep.
990-
tokio::time::sleep(FIRST_HOST_INFO_REFRESH_INTERVAL).await;
991984

992985
// Start the host info update loop.
993986
loop {
994-
let mut host = Host::default();
995-
let mut network = Network {
996-
upload_rate: request_interface_speed,
997-
..Default::default()
998-
};
987+
// Sleep to calculate the network traffic difference over
988+
// the DEFAULT_HOST_INFO_REFRESH_INTERVAL.
989+
tokio::time::sleep(DEFAULT_HOST_INFO_REFRESH_INTERVAL).await;
999990

1000991
// Refresh network information.
1001992
networks.refresh();
1002993
let now_time = SystemTime::now();
994+
1003995
// Get interval between two refreshes.
1004996
let interval = now_time
1005997
.duration_since(last_refresh_time)
@@ -1008,17 +1000,25 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
10081000
// Reset last_refresh_time to now_time.
10091001
last_refresh_time = now_time;
10101002

1003+
let mut host = Host::default();
1004+
let mut network = Network {
1005+
upload_rate: request_interface_speed,
1006+
..Default::default()
1007+
};
1008+
10111009
// Get interface available bandwidth.
10121010
if let Some(request_interface) = request_interface.clone() {
10131011
for (interface, data) in &networks {
10141012
if *interface == request_interface {
10151013
if network.upload_rate
1016-
< data.transmitted() * MILLISECONDS_PER_SECOND / interval
1014+
< data.transmitted() * Duration::from_secs(1).as_millis() as u64
1015+
/ interval
10171016
{
10181017
network.upload_rate = 0;
10191018
} else {
1020-
network.upload_rate -=
1021-
data.transmitted() * MILLISECONDS_PER_SECOND / interval;
1019+
network.upload_rate -= data.transmitted()
1020+
* Duration::from_secs(1).as_millis() as u64
1021+
/ interval;
10221022
}
10231023
debug!(
10241024
"refresh interface {} available bandwidth to {}",
@@ -1045,9 +1045,6 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
10451045
break;
10461046
}
10471047
};
1048-
1049-
// Sleep.
1050-
tokio::time::sleep(DEFAULT_HOST_INFO_REFRESH_INTERVAL).await;
10511048
}
10521049
}
10531050
.in_current_span(),
@@ -1791,6 +1788,17 @@ impl DfdaemonUploadClient {
17911788
Ok(response.into_inner())
17921789
}
17931790

1791+
/// sync_host provides the host info for parent.
1792+
#[instrument(skip_all)]
1793+
pub async fn sync_host(
1794+
&self,
1795+
request: SyncHostRequest,
1796+
) -> ClientResult<tonic::Response<tonic::codec::Streaming<Host>>> {
1797+
let request = Self::make_request(request);
1798+
let response = self.client.clone().sync_host(request).await?;
1799+
Ok(response)
1800+
}
1801+
17941802
/// make_request creates a new request with timeout.
17951803
#[instrument(skip_all)]
17961804
fn make_request<T>(request: T) -> tonic::Request<T> {

0 commit comments

Comments
 (0)