mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-01-27 20:14:11 +00:00
Try fix OnlineMap SIGSEGV and simplify code
This commit is contained in:
parent
e80ca67fee
commit
3ed502abbb
@ -35,46 +35,37 @@ func (c *OnlineMap) List() []string {
|
|||||||
|
|
||||||
// AddIP implements stats.OnlineMap.
|
// AddIP implements stats.OnlineMap.
|
||||||
func (c *OnlineMap) AddIP(ip string) {
|
func (c *OnlineMap) AddIP(ip string) {
|
||||||
list := c.ipList
|
|
||||||
|
|
||||||
if ip == "127.0.0.1" {
|
if ip == "127.0.0.1" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, ok := list[ip]; !ok {
|
|
||||||
c.access.Lock()
|
c.access.Lock()
|
||||||
list[ip] = time.Now()
|
defer c.access.Unlock()
|
||||||
c.access.Unlock()
|
|
||||||
|
if _, ok := c.ipList[ip]; !ok {
|
||||||
|
c.ipList[ip] = time.Now()
|
||||||
}
|
}
|
||||||
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
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.lastCleanup = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.value = len(list)
|
c.value = len(c.ipList)
|
||||||
c.ipList = list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OnlineMap) GetKeys() []string {
|
func (c *OnlineMap) GetKeys() []string {
|
||||||
c.access.RLock()
|
c.access.Lock()
|
||||||
defer c.access.RUnlock()
|
defer c.access.Unlock()
|
||||||
|
|
||||||
keys := []string{}
|
keys := []string{}
|
||||||
for k := range c.ipList {
|
for k := range c.ipList {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
return keys
|
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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user