Fix PingRecord race condition

This commit is contained in:
Takanori Hirano 2024-12-25 17:37:15 +09:00
parent 3e6e5a61f0
commit 4d9c79abaf
2 changed files with 12 additions and 6 deletions

View file

@ -21,6 +21,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/sourcegraph/conc"
@ -48,7 +49,8 @@ type VirtualTun struct {
SystemDNS bool
Conf *DeviceConfig
// PingRecord stores the last time an IP was pinged
PingRecord map[string]uint64
PingRecord map[string]uint64
PingRecordLock *sync.Mutex
}
// RoutineSpawner spawns a routine (e.g. socks5, tcp static routes) after the configuration is parsed
@ -475,7 +477,9 @@ func (d VirtualTun) pingIPs() {
}
}
d.PingRecordLock.Lock()
d.PingRecord[addr.String()] = uint64(time.Now().Unix())
d.PingRecordLock.Unlock()
defer socket.Close()
}()