@@ -63,7 +63,7 @@ use tonic::{
63
63
Code , Request , Response , Status ,
64
64
} ;
65
65
use tower:: ServiceBuilder ;
66
- use tracing:: { error, info, instrument, Instrument , Span } ;
66
+ use tracing:: { debug , error, info, instrument, Instrument , Span } ;
67
67
use url:: Url ;
68
68
69
69
/// DfdaemonUploadServer is the grpc server of the upload.
@@ -911,17 +911,13 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
911
911
& self ,
912
912
request : Request < SyncHostRequest > ,
913
913
) -> 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
+ }
925
921
926
922
// Get request ip.
927
923
let request_ip = request. remote_addr ( ) ;
@@ -950,7 +946,7 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
950
946
951
947
// Get the interface and the interface speed of this request ip.
952
948
let mut request_interface = None ;
953
- let mut request_interface_speed = DEFAULT_INTERFACE_SPEED . as_u64 ( ) ;
949
+ let mut request_interface_speed: u64 = 0 ;
954
950
if let Some ( request_ip) = request_ip {
955
951
// Get the interface of this request ip.
956
952
let interfaces = datalink:: interfaces ( ) ;
@@ -966,14 +962,13 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
966
962
let speed_path = format ! ( "/sys/class/net/{}/speed" , interface) ;
967
963
let content = fs:: read_to_string ( speed_path) . unwrap_or_default ( ) ;
968
964
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) ) ;
970
967
info ! (
971
968
"interface {} speed is {} for request ip {}" ,
972
- & interface,
973
- ByteSize :: mb( speed / BITS_PER_BYTE ) ,
974
- request_ip,
969
+ & interface, speed, request_ip,
975
970
) ;
976
- request_interface_speed = ByteSize :: mb ( speed / BITS_PER_BYTE ) . as_u64 ( ) ;
971
+ request_interface_speed = speed. as_u64 ( ) ;
977
972
}
978
973
}
979
974
}
@@ -986,20 +981,17 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
986
981
// Initialize sysinfo network.
987
982
let mut networks = Networks :: new_with_refreshed_list ( ) ;
988
983
let mut last_refresh_time = SystemTime :: now ( ) ;
989
- // Sleep.
990
- tokio:: time:: sleep ( FIRST_HOST_INFO_REFRESH_INTERVAL ) . await ;
991
984
992
985
// Start the host info update loop.
993
986
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 ;
999
990
1000
991
// Refresh network information.
1001
992
networks. refresh ( ) ;
1002
993
let now_time = SystemTime :: now ( ) ;
994
+
1003
995
// Get interval between two refreshes.
1004
996
let interval = now_time
1005
997
. duration_since ( last_refresh_time)
@@ -1008,17 +1000,25 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
1008
1000
// Reset last_refresh_time to now_time.
1009
1001
last_refresh_time = now_time;
1010
1002
1003
+ let mut host = Host :: default ( ) ;
1004
+ let mut network = Network {
1005
+ upload_rate : request_interface_speed,
1006
+ ..Default :: default ( )
1007
+ } ;
1008
+
1011
1009
// Get interface available bandwidth.
1012
1010
if let Some ( request_interface) = request_interface. clone ( ) {
1013
1011
for ( interface, data) in & networks {
1014
1012
if * interface == request_interface {
1015
1013
if network. upload_rate
1016
- < data. transmitted ( ) * MILLISECONDS_PER_SECOND / interval
1014
+ < data. transmitted ( ) * Duration :: from_secs ( 1 ) . as_millis ( ) as u64
1015
+ / interval
1017
1016
{
1018
1017
network. upload_rate = 0 ;
1019
1018
} 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;
1022
1022
}
1023
1023
debug ! (
1024
1024
"refresh interface {} available bandwidth to {}" ,
@@ -1045,9 +1045,6 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
1045
1045
break ;
1046
1046
}
1047
1047
} ;
1048
-
1049
- // Sleep.
1050
- tokio:: time:: sleep ( DEFAULT_HOST_INFO_REFRESH_INTERVAL ) . await ;
1051
1048
}
1052
1049
}
1053
1050
. in_current_span ( ) ,
@@ -1791,6 +1788,17 @@ impl DfdaemonUploadClient {
1791
1788
Ok ( response. into_inner ( ) )
1792
1789
}
1793
1790
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
+
1794
1802
/// make_request creates a new request with timeout.
1795
1803
#[ instrument( skip_all) ]
1796
1804
fn make_request < T > ( request : T ) -> tonic:: Request < T > {
0 commit comments