Skip to content

Commit

Permalink
feat: add bytesize for rate_limit (dragonflyoss#602)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi authored Jul 17, 2024
1 parent db2008f commit 7671818
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
28 changes: 20 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
]

[workspace.package]
version = "0.1.87"
version = "0.1.88"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
Expand All @@ -22,13 +22,13 @@ readme = "README.md"
edition = "2021"

[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "0.1.87" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.87" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.87" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.87" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.87" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.87" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.87" }
dragonfly-client = { path = "dragonfly-client", version = "0.1.88" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.88" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.88" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.88" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.88" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.88" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.88" }
thiserror = "1.0"
dragonfly-api = "2.0.138"
reqwest = { version = "0.12.4", features = ["stream", "native-tls", "default-tls", "rustls-tls"] }
Expand Down Expand Up @@ -84,6 +84,7 @@ toml_edit = "0.22.14"
toml = "0.8.13"
base16ct = { version = "0.2", features = ["alloc"] }
bytesize = {version = "1.2.0", features = ["serde"]}
bytesize-serde = "0.2.1"

[profile.release]
debug = true
Expand Down
2 changes: 2 additions & 0 deletions dragonfly-client-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ validator.workspace = true
humantime.workspace = true
serde_yaml.workspace = true
tokio.workspace = true
bytesize.workspace = true
bytesize-serde.workspace = true
home = "0.5.4"
local-ip-address = "0.6.1"
hostname = "^0.4"
Expand Down
35 changes: 21 additions & 14 deletions dragonfly-client-config/src/dfdaemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

use bytesize::ByteSize;
use dragonfly_client_core::error::{ErrorType, OrErr};
use dragonfly_client_core::Result;
use local_ip_address::{local_ip, local_ipv6};
Expand Down Expand Up @@ -77,11 +78,11 @@ fn default_upload_grpc_server_port() -> u16 {
4000
}

// default_upload_rate_limit is the default rate limit of the upload speed in bps(bytes per second).
// default_upload_rate_limit is the default rate limit of the upload speed in GiB/Mib/Kib per second.
#[inline]
fn default_upload_rate_limit() -> u64 {
// Default rate limit is 10Gbps.
10_000_000_000
fn default_upload_rate_limit() -> ByteSize {
// Default rate limit is 10GiB/s.
ByteSize::gib(10)
}

// default_health_server_port is the default port of the health server.
Expand All @@ -102,11 +103,11 @@ fn default_stats_server_port() -> u16 {
4004
}

// default_download_rate_limit is the default rate limit of the download speed in bps(bytes per second).
// default_download_rate_limit is the default rate limit of the download speed in GiB/Mib/Kib per second.
#[inline]
fn default_download_rate_limit() -> u64 {
// Default rate limit is 10Gbps.
10_000_000_000
fn default_download_rate_limit() -> ByteSize {
// Default rate limit is 10GiB/s.
ByteSize::gib(10)
}

// default_download_piece_timeout is the default timeout for downloading a piece from source.
Expand Down Expand Up @@ -388,9 +389,9 @@ pub struct Download {
// server is the download server configuration for dfdaemon.
pub server: DownloadServer,

// rate_limit is the rate limit of the download speed in bps(bytes per second).
#[serde(default = "default_download_rate_limit")]
pub rate_limit: u64,
// rate_limit is the rate limit of the download speed in GiB/Mib/Kib per second.
#[serde(with = "bytesize_serde", default = "default_download_rate_limit")]
pub rate_limit: ByteSize,

// piece_timeout is the timeout for downloading a piece from source.
#[serde(default = "default_download_piece_timeout", with = "humantime_serde")]
Expand Down Expand Up @@ -443,9 +444,9 @@ pub struct Upload {
// server is the upload server configuration for dfdaemon.
pub server: UploadServer,

// rate_limit is the rate limit of the upload speed in bps(bytes per second).
#[serde(default = "default_upload_rate_limit")]
pub rate_limit: u64,
// rate_limit is the rate limit of the upload speed in GiB/Mib/Kib per second.
#[serde(with = "bytesize_serde", default = "default_upload_rate_limit")]
pub rate_limit: ByteSize,
}

// Upload implements Default.
Expand Down Expand Up @@ -1044,6 +1045,12 @@ impl Config {

// convert converts the configuration.
fn convert(&mut self) {
println!(
"convert download rate limit: {:?}",
self.download.rate_limit
);
println!("convert upload rate limit: {:?}", self.upload.rate_limit);

// Convert advertise ip.
if self.host.ip.is_none() {
self.host.ip = if self.network.enable_ipv6 {
Expand Down
8 changes: 4 additions & 4 deletions dragonfly-client/src/resource/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ impl Piece {
backend_factory,
download_rate_limiter: Arc::new(
RateLimiter::builder()
.initial(config.download.rate_limit as usize)
.refill(config.download.rate_limit as usize)
.initial(config.download.rate_limit.as_u64() as usize)
.refill(config.download.rate_limit.as_u64() as usize)
.interval(Duration::from_secs(1))
.fair(false)
.build(),
),
upload_rate_limiter: Arc::new(
RateLimiter::builder()
.initial(config.upload.rate_limit as usize)
.refill(config.upload.rate_limit as usize)
.initial(config.upload.rate_limit.as_u64() as usize)
.refill(config.upload.rate_limit.as_u64() as usize)
.interval(Duration::from_secs(1))
.build(),
),
Expand Down

0 comments on commit 7671818

Please sign in to comment.