Skip to content

Commit

Permalink
fix: validate rate limiter (#1392)
Browse files Browse the repository at this point in the history
* fix: validate rate limiter

Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Jun 16, 2022
1 parent 2b42308 commit 8a8eb25
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
DefaultPerPeerDownloadLimit = 20 * unit.MB
DefaultTotalDownloadLimit = 100 * unit.MB
DefaultUploadLimit = 100 * unit.MB
DefaultMinRate = 64 * unit.KB
DefaultMinRate = 20 * unit.MB
)

/* others */
Expand Down
4 changes: 4 additions & 0 deletions client/config/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (cfg *ClientOption) Validate() error {
return errors.Wrapf(dferrors.ErrInvalidHeader, "output: %v", err)
}

if int64(cfg.RateLimit.Limit) < DefaultMinRate.ToNumber() {
return errors.Wrapf(dferrors.ErrInvalidArgument, "rate limit must be greater than %s", DefaultMinRate.String())
}

return nil
}

Expand Down
6 changes: 5 additions & 1 deletion client/config/dfget_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package config

import "d7y.io/dragonfly/v2/client/clientutil"
import (
"golang.org/x/time/rate"

"d7y.io/dragonfly/v2/client/clientutil"
)

var dfgetConfig = ClientOption{
URL: "",
Expand Down
9 changes: 9 additions & 0 deletions client/config/peerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ func (p *DaemonOption) Validate() error {
if len(p.Scheduler.NetAddrs) == 0 {
return errors.New("empty schedulers and config server is not specified")
}

if int64(p.Download.TotalRateLimit.Limit) < DefaultMinRate.ToNumber() {
return errors.Errorf("rate limit must be greater than %s", DefaultMinRate.String())
}

if int64(p.Upload.RateLimit.Limit) < DefaultMinRate.ToNumber() {
return errors.Errorf("rate limit must be greater than %s", DefaultMinRate.String())
}

switch p.Download.DefaultPattern {
case PatternP2P, PatternSeedPeer, PatternSource:
default:
Expand Down

0 comments on commit 8a8eb25

Please sign in to comment.