From f5ce27368810d762c7098ca7f822498dd07735be Mon Sep 17 00:00:00 2001 From: charSLee013 Date: Sat, 11 Feb 2023 12:40:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E4=B8=8B=E8=BD=BD=E6=B5=8B=E9=80=9F?= =?UTF-8?q?=E6=95=B0=E5=80=BC=E8=BF=87=E5=A4=A7=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#290)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix the speed measurement value is too large * fix break when err == io.EOF --- task/download.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/task/download.go b/task/download.go index a6456bae..65ddfd86 100644 --- a/task/download.go +++ b/task/download.go @@ -167,10 +167,15 @@ func downloadHandler(ip *net.IPAddr) float64 { } bufferRead, err := response.Body.Read(buffer) if err != nil { - if err != io.EOF { // 文件下载完了,或因网络等问题导致链接中断,则退出循环(终止测速) + // 获取上个时间片 + last_time_slice := timeStart.Add(timeSlice * time.Duration(timeCounter-1)) + // 下载数据量 / (用当前时间 - 上个时间片/ 时间片) + e.Add(float64(contentRead-lastContentRead) / (float64(currentTime.Sub(last_time_slice)) / float64(timeSlice))) + + if err == io.EOF { // 文件下载完了,或因网络等问题导致链接中断,则退出循环(终止测速) break } - e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice))) + } contentRead += int64(bufferRead) }