feat: smooth speed calculation (#99)

This commit is contained in:
Li Wei authored and GitHub committed 2023-03-09 10:35:10 +08:00
1 parent e77441f0de
commit 701ea59035
2 files changed
+17 -4

No files matched your search

+13
View File
@@ -29,6 +29,7 @@ type Task struct {
fetcher fetcher.Fetcher
timer *util.Timer
lock *sync.Mutex
speedArr []int64
}
func NewTask() *Task {
@@ -54,6 +55,18 @@ func (t *Task) clone() *Task {
}
}
func (t *Task) calcSpeed(downloaded int64, usedTime float64) int64 {
t.speedArr = append(t.speedArr, downloaded)
if len(t.speedArr) > 6 {
t.speedArr = t.speedArr[1:]
}
var total int64
for _, v := range t.speedArr {
total += v
}
return int64(float64(total) / float64(len(t.speedArr)) / usedTime)
}
type DownloaderConfig struct {
Controller *controller.Controller
FetchBuilders []fetcher.FetcherBuilder