Browse Source

API: Fix data race in online ipList (#4513)

Fixes https://github.com/XTLS/Xray-core/issues/4512
风扇滑翔翼 7 months ago
parent
commit
b70d0abebe
1 changed files with 2 additions and 2 deletions
  1. 2 2
      app/stats/online_map.go

+ 2 - 2
app/stats/online_map.go

@@ -40,11 +40,11 @@ func (c *OnlineMap) AddIP(ip string) {
 	if ip == "127.0.0.1" {
 	if ip == "127.0.0.1" {
 		return
 		return
 	}
 	}
+	c.access.Lock()
 	if _, ok := list[ip]; !ok {
 	if _, ok := list[ip]; !ok {
-		c.access.Lock()
 		list[ip] = time.Now()
 		list[ip] = time.Now()
-		c.access.Unlock()
 	}
 	}
+	c.access.Unlock()
 	if time.Since(c.lastCleanup) > c.cleanupPeriod {
 	if time.Since(c.lastCleanup) > c.cleanupPeriod {
 		list = c.RemoveExpiredIPs(list)
 		list = c.RemoveExpiredIPs(list)
 		c.lastCleanup = time.Now()
 		c.lastCleanup = time.Now()