Skip to content

Commit f5ce273

Browse files
authored
修复 某些情况下下载测速数值过大的问题 (#290)
* fix the speed measurement value is too large * fix break when err == io.EOF
1 parent ed1d512 commit f5ce273

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

task/download.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,15 @@ func downloadHandler(ip *net.IPAddr) float64 {
167167
}
168168
bufferRead, err := response.Body.Read(buffer)
169169
if err != nil {
170-
if err != io.EOF { // 文件下载完了,或因网络等问题导致链接中断,则退出循环(终止测速)
170+
// 获取上个时间片
171+
last_time_slice := timeStart.Add(timeSlice * time.Duration(timeCounter-1))
172+
// 下载数据量 / (用当前时间 - 上个时间片/ 时间片)
173+
e.Add(float64(contentRead-lastContentRead) / (float64(currentTime.Sub(last_time_slice)) / float64(timeSlice)))
174+
175+
if err == io.EOF { // 文件下载完了,或因网络等问题导致链接中断,则退出循环(终止测速)
171176
break
172177
}
173-
e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
178+
174179
}
175180
contentRead += int64(bufferRead)
176181
}

0 commit comments

Comments
 (0)