优化监控算法

This commit is contained in:
apple
2018-03-03 10:00:36 +08:00
parent d4719c72e8
commit f8ba5ab4a4

View File

@@ -11,8 +11,7 @@ var (
)
// blockMonitor 延迟监控各线程状态,
// 管理空闲 (已完成下载任务) 的线程,
// 清除长时间无响应, 和下载速度为 0 的线程
// 重设长时间无响应, 和下载速度为 0 的线程
func (der *Downloader) blockMonitor() <-chan struct{} {
c := make(chan struct{})
go func() {
@@ -32,8 +31,9 @@ func (der *Downloader) blockMonitor() <-chan struct{} {
der.recordBreakPoint()
}
// 下载进度超过 70%, 开启监控
if float64(der.status.Downloaded)/float64(der.status.TotalSize) > 0.7 {
// 速度减慢, 开启监控
if der.status.Speeds < der.status.MaxSpeeds/10 {
der.status.MaxSpeeds = 0
for k := range der.status.BlockList {
go func(k int) {
// 过滤已完成下载任务的线程
@@ -41,27 +41,21 @@ func (der *Downloader) blockMonitor() <-chan struct{} {
return
}
// 清除长时间无响应, 和下载速度为 0 的线程
// 重设长时间无响应, 和下载速度为 0 的线程
go func(k int) {
// 设 old 速度监测点, 2 秒后检查速度有无变化
old := der.status.BlockList[k].Begin
time.Sleep(2 * time.Second)
// 过滤 速度有变化, 或 2 秒内完成了下载任务 的线程, 不过滤正在等待写入磁盘的线程
if der.status.BlockList[k].waitingToWrite || old != der.status.BlockList[k].Begin || der.status.BlockList[k].isDone() {
return
if der.status.Speeds != 0 {
// 设 old 速度监测点, 2 秒后检查速度有无变化
old := der.status.BlockList[k].Begin
time.Sleep(2 * time.Second)
// 过滤 速度有变化, 或 2 秒内完成了下载任务 的线程, 不过滤正在等待写入磁盘的线程
if der.status.BlockList[k].waitingToWrite || old != der.status.BlockList[k].Begin || der.status.BlockList[k].isDone() {
return
}
}
// 筛选出 长时间无响应, 和下载速度为 0 的线程
// 然后尝试清除该线程, 选出其他 空闲的线程, 重新添加下载任务
mu.Lock() // 加锁, 防止出现重复添加线程的状况 (实验阶段)
// 筛选空闲的线程
_, ok := der.status.BlockList.avaliableThread()
if !ok { // 没有空的
mu.Unlock() // 解锁
return
}
// 重设连接
if r := der.status.BlockList[k].resp; r != nil {
r.Body.Close()
@@ -69,38 +63,7 @@ func (der *Downloader) blockMonitor() <-chan struct{} {
mu.Unlock() // 解锁
}(k)
/*
// 动态分配新线程
go func(k int) {
mu.Lock()
// 筛选空闲的线程
index, ok := der.status.BlockList.avaliableThread()
if !ok { // 没有空的
mu.Unlock() // 解锁
return
}
middle := (der.status.BlockList[k].Begin + der.status.BlockList[k].End) / 2
if der.status.BlockList[k].End-middle <= 102400 { // 如果线程剩余的下载量太少, 不分配空闲线程
mu.Unlock()
return
}
// 折半
der.status.BlockList[index].Begin = middle + 1
der.status.BlockList[index].End = der.status.BlockList[k].End
der.status.BlockList[index].IsFinal = der.status.BlockList[k].IsFinal
der.status.BlockList[k].End = middle
// End 已变, 取消 Final
der.status.BlockList[k].IsFinal = false
mu.Unlock()
der.addExecBlock(index)
}(k)
*/
}(k)
}
}