Skip to content

Commit 82b5dd1

Browse files
LjhAUMEMmaoxikun
authored andcommitted
feat(api): update timestamp for existing IPs in AddIP instead of skipping (XTLS#4989)
Co-authored-by: null <null> (cherry picked from commit 9359844)
1 parent 3e4af3d commit 82b5dd1

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

app/stats/online_map.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ func (c *OnlineMap) AddIP(ip string) {
4040
if ip == "127.0.0.1" {
4141
return
4242
}
43+
4344
c.access.Lock()
44-
if _, ok := c.ipList[ip]; !ok {
45-
c.ipList[ip] = time.Now()
46-
}
45+
c.ipList[ip] = time.Now()
4746
c.access.Unlock()
47+
4848
if time.Since(c.lastCleanup) > c.cleanupPeriod {
49-
c.RemoveExpiredIPs(c.ipList)
49+
c.RemoveExpiredIPs()
5050
c.lastCleanup = time.Now()
5151
}
5252
}
@@ -62,23 +62,22 @@ func (c *OnlineMap) GetKeys() []string {
6262
return keys
6363
}
6464

65-
func (c *OnlineMap) RemoveExpiredIPs(list map[string]time.Time) map[string]time.Time {
65+
func (c *OnlineMap) RemoveExpiredIPs() {
6666
c.access.Lock()
6767
defer c.access.Unlock()
6868

6969
now := time.Now()
70-
for k, t := range list {
70+
for k, t := range c.ipList {
7171
diff := now.Sub(t)
7272
if diff.Seconds() > 20 {
73-
delete(list, k)
73+
delete(c.ipList, k)
7474
}
7575
}
76-
return list
7776
}
7877

7978
func (c *OnlineMap) IpTimeMap() map[string]time.Time {
8079
if time.Since(c.lastCleanup) > c.cleanupPeriod {
81-
c.RemoveExpiredIPs(c.ipList)
80+
c.RemoveExpiredIPs()
8281
c.lastCleanup = time.Now()
8382
}
8483

0 commit comments

Comments
 (0)