Skip to content

Commit

Permalink
fix: concurrent map access
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancheng91 authored and ginuerzh committed Jun 13, 2024
1 parent 94f812b commit 77262f2
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,11 @@ func (f *FastestFilter) Filter(nodes []Node) []Node {
now := time.Now().Unix()

var getNodeLatency = func(node Node) int {
f.mu.Lock()
defer f.mu.Unlock()

if f.pingResultTTL[node.ID] < now {
f.mu.Lock()
f.pingResultTTL[node.ID] = now + 5 // tmp
defer f.mu.Unlock()

// get latency
go func(node Node) {
Expand All @@ -254,9 +255,10 @@ func (f *FastestFilter) Filter(nodes []Node) []Node {
ttl := 300 - int64(120*r.Float64())

f.mu.Lock()
defer f.mu.Unlock()

f.pingResult[node.ID] = latency
f.pingResultTTL[node.ID] = now + ttl
defer f.mu.Unlock()
}(node)
}
return f.pingResult[node.ID]
Expand Down

0 comments on commit 77262f2

Please sign in to comment.