mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-15 00:30:24 +08:00
优化nftables集合元素过期时间判断
This commit is contained in:
@@ -40,7 +40,10 @@ func (this *Expiration) Remove(key []byte) {
|
|||||||
|
|
||||||
func (this *Expiration) Contains(key []byte) bool {
|
func (this *Expiration) Contains(key []byte) bool {
|
||||||
this.locker.RLock()
|
this.locker.RLock()
|
||||||
_, ok := this.m[string(key)]
|
expires, ok := this.m[string(key)]
|
||||||
|
if ok && expires.Year() > 2000 && time.Now().After(expires) {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
this.locker.RUnlock()
|
this.locker.RUnlock()
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
@@ -55,7 +58,7 @@ func (this *Expiration) gc() {
|
|||||||
|
|
||||||
var now = time.Now().Add(-10 * time.Second) // gc elements expired before 10 seconds ago
|
var now = time.Now().Add(-10 * time.Second) // gc elements expired before 10 seconds ago
|
||||||
for key, expires := range this.m {
|
for key, expires := range this.m {
|
||||||
if expires.Year() >= 2000 && now.After(expires) {
|
if expires.Year() > 2000 && now.After(expires) {
|
||||||
delete(this.m, key)
|
delete(this.m, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ func TestExpiration_Add(t *testing.T) {
|
|||||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now())
|
expiration.Add([]byte{'a', 'b', 'c'}, time.Now())
|
||||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(1*time.Second))
|
||||||
|
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||||
|
}
|
||||||
|
{
|
||||||
|
expiration.Add([]byte{'a', 'b', 'c'}, time.Time{})
|
||||||
|
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||||
|
}
|
||||||
{
|
{
|
||||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(-1*time.Second))
|
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(-1*time.Second))
|
||||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||||
|
|||||||
Reference in New Issue
Block a user