Sfoglia il codice sorgente

API: Fix user online map remain 1 after connection dropped (#4982)

* c.value

* remove value

* c.access.RLock()

* remove local var "list"

---------

Co-authored-by: null <null>
LjhAUMEM 5 mesi fa
parent
commit
8222f43eea
1 ha cambiato i file con 8 aggiunte e 12 eliminazioni
  1. 8 12
      app/stats/online_map.go

+ 8 - 12
app/stats/online_map.go

@@ -7,7 +7,6 @@ import (
 
 // OnlineMap is an implementation of stats.OnlineMap.
 type OnlineMap struct {
-	value         int
 	ipList        map[string]time.Time
 	access        sync.RWMutex
 	lastCleanup   time.Time
@@ -25,7 +24,10 @@ func NewOnlineMap() *OnlineMap {
 
 // Count implements stats.OnlineMap.
 func (c *OnlineMap) Count() int {
-	return c.value
+	c.access.RLock()
+	defer c.access.RUnlock()
+
+	return len(c.ipList)
 }
 
 // List implements stats.OnlineMap.
@@ -35,23 +37,18 @@ func (c *OnlineMap) List() []string {
 
 // AddIP implements stats.OnlineMap.
 func (c *OnlineMap) AddIP(ip string) {
-	list := c.ipList
-
 	if ip == "127.0.0.1" {
 		return
 	}
 	c.access.Lock()
-	if _, ok := list[ip]; !ok {
-		list[ip] = time.Now()
+	if _, ok := c.ipList[ip]; !ok {
+		c.ipList[ip] = time.Now()
 	}
 	c.access.Unlock()
 	if time.Since(c.lastCleanup) > c.cleanupPeriod {
-		list = c.RemoveExpiredIPs(list)
+		c.RemoveExpiredIPs(c.ipList)
 		c.lastCleanup = time.Now()
 	}
-
-	c.value = len(list)
-	c.ipList = list
 }
 
 func (c *OnlineMap) GetKeys() []string {
@@ -80,9 +77,8 @@ func (c *OnlineMap) RemoveExpiredIPs(list map[string]time.Time) map[string]time.
 }
 
 func (c *OnlineMap) IpTimeMap() map[string]time.Time {
-	list := c.ipList
 	if time.Since(c.lastCleanup) > c.cleanupPeriod {
-		list = c.RemoveExpiredIPs(list)
+		c.RemoveExpiredIPs(c.ipList)
 		c.lastCleanup = time.Now()
 	}