fix: concurrent map access
This commit is contained in:
parent
a9e7fee369
commit
164601cfd2
@ -242,11 +242,12 @@ 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 {
|
||||||
if f.pingResultTTL[node.ID] < now {
|
|
||||||
f.mu.Lock()
|
f.mu.Lock()
|
||||||
f.pingResultTTL[node.ID] = now + 5 // tmp
|
|
||||||
defer f.mu.Unlock()
|
defer f.mu.Unlock()
|
||||||
|
|
||||||
|
if f.pingResultTTL[node.ID] < now {
|
||||||
|
f.pingResultTTL[node.ID] = now + 5 // tmp
|
||||||
|
|
||||||
// get latency
|
// get latency
|
||||||
go func(node Node) {
|
go func(node Node) {
|
||||||
latency := f.doTcpPing(node.Addr)
|
latency := f.doTcpPing(node.Addr)
|
||||||
@ -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…
Reference in New Issue
Block a user