Browse Source

fix: concurrent map access

pull/1038/head
tiancheng91 3 years ago
committed by ginuerzh
parent
commit
77262f2454
  1. 8
      selector.go

8
selector.go

@ -242,10 +242,11 @@ func (f *FastestFilter) Filter(nodes []Node) []Node {
now := time.Now().Unix() now := time.Now().Unix()
var getNodeLatency = func(node Node) int { var getNodeLatency = func(node Node) int {
f.mu.Lock()
defer f.mu.Unlock()
if f.pingResultTTL[node.ID] < now { if f.pingResultTTL[node.ID] < now {
f.mu.Lock()
f.pingResultTTL[node.ID] = now + 5 // tmp f.pingResultTTL[node.ID] = now + 5 // tmp
defer f.mu.Unlock()
// get latency // get latency
go func(node Node) { go func(node Node) {
@ -254,9 +255,10 @@ func (f *FastestFilter) Filter(nodes []Node) []Node {
ttl := 300 - int64(120*r.Float64()) ttl := 300 - int64(120*r.Float64())
f.mu.Lock() f.mu.Lock()
defer f.mu.Unlock()
f.pingResult[node.ID] = latency f.pingResult[node.ID] = latency
f.pingResultTTL[node.ID] = now + ttl f.pingResultTTL[node.ID] = now + ttl
defer f.mu.Unlock()
}(node) }(node)
} }
return f.pingResult[node.ID] return f.pingResult[node.ID]

Loading…
Cancel
Save