diff --git a/app/stats/online_map.go b/app/stats/online_map.go index 7cc6ac96..6f7a6701 100644 --- a/app/stats/online_map.go +++ b/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() }