Skip to content

Commit

Permalink
Try fix OnlineMap SIGSEGV and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhan6665 committed Dec 30, 2024
1 parent e80ca67 commit 3ed502a
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions app/stats/online_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,37 @@ 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
}
if _, ok := list[ip]; !ok {
c.access.Lock()
list[ip] = time.Now()
c.access.Unlock()

c.access.Lock()
defer c.access.Unlock()

if _, ok := c.ipList[ip]; !ok {
c.ipList[ip] = time.Now()
}
if time.Since(c.lastCleanup) > c.cleanupPeriod {
list = c.RemoveExpiredIPs(list)
now := time.Now()
for k, t := range c.ipList {
diff := now.Sub(t)
if diff.Seconds() > 20 {
delete(c.ipList, k) // safe to do delete in range
}
}
c.lastCleanup = time.Now()
}

c.value = len(list)
c.ipList = list
c.value = len(c.ipList)
}

func (c *OnlineMap) GetKeys() []string {
c.access.RLock()
defer c.access.RUnlock()
c.access.Lock()
defer c.access.Unlock()

keys := []string{}
for k := range c.ipList {
keys = append(keys, k)
}
return keys
}

func (c *OnlineMap) RemoveExpiredIPs(list map[string]time.Time) map[string]time.Time {
c.access.Lock()
defer c.access.Unlock()

now := time.Now()
for k, t := range list {
diff := now.Sub(t)
if diff.Seconds() > 20 {
delete(list, k)
}
}
return list
}
}

0 comments on commit 3ed502a

Please sign in to comment.