|
|
|
@ -17,12 +17,12 @@ |
|
|
|
|
package les |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"sort" |
|
|
|
|
"sync" |
|
|
|
|
"sync/atomic" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock" |
|
|
|
|
"github.com/ethereum/go-ethereum/common/prque" |
|
|
|
|
"golang.org/x/exp/slices" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// servingQueue allows running tasks in a limited number of threads and puts the
|
|
|
|
@ -180,35 +180,19 @@ func (sq *servingQueue) threadController() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type ( |
|
|
|
|
// peerTasks lists the tasks received from a given peer when selecting peers to freeze
|
|
|
|
|
peerTasks struct { |
|
|
|
|
peer *clientPeer |
|
|
|
|
list []*servingTask |
|
|
|
|
sumTime uint64 |
|
|
|
|
priority float64 |
|
|
|
|
} |
|
|
|
|
// peerList is a sortable list of peerTasks
|
|
|
|
|
peerList []*peerTasks |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func (l peerList) Len() int { |
|
|
|
|
return len(l) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (l peerList) Less(i, j int) bool { |
|
|
|
|
return l[i].priority < l[j].priority |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (l peerList) Swap(i, j int) { |
|
|
|
|
l[i], l[j] = l[j], l[i] |
|
|
|
|
// peerTasks lists the tasks received from a given peer when selecting peers to freeze
|
|
|
|
|
type peerTasks struct { |
|
|
|
|
peer *clientPeer |
|
|
|
|
list []*servingTask |
|
|
|
|
sumTime uint64 |
|
|
|
|
priority float64 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// freezePeers selects the peers with the worst priority queued tasks and freezes
|
|
|
|
|
// them until burstTime goes under burstDropLimit or all peers are frozen
|
|
|
|
|
func (sq *servingQueue) freezePeers() { |
|
|
|
|
peerMap := make(map[*clientPeer]*peerTasks) |
|
|
|
|
var peerList peerList |
|
|
|
|
var peerList []*peerTasks |
|
|
|
|
if sq.best != nil { |
|
|
|
|
sq.queue.Push(sq.best, sq.best.priority) |
|
|
|
|
} |
|
|
|
@ -231,7 +215,9 @@ func (sq *servingQueue) freezePeers() { |
|
|
|
|
tasks.list = append(tasks.list, task) |
|
|
|
|
tasks.sumTime += task.expTime |
|
|
|
|
} |
|
|
|
|
sort.Sort(peerList) |
|
|
|
|
slices.SortFunc(peerList, func(a, b *peerTasks) bool { |
|
|
|
|
return a.priority < b.priority |
|
|
|
|
}) |
|
|
|
|
drop := true |
|
|
|
|
for _, tasks := range peerList { |
|
|
|
|
if drop { |
|
|
|
|