forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ifname processor: expire old cached entries (influxdata#7838)
- Loading branch information
Showing
7 changed files
with
179 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ifname | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type TTLValType struct { | ||
time time.Time // when entry was added | ||
val valType | ||
} | ||
|
||
type timeFunc func() time.Time | ||
|
||
type TTLCache struct { | ||
validDuration time.Duration | ||
lru LRUCache | ||
now timeFunc | ||
} | ||
|
||
func NewTTLCache(valid time.Duration, capacity uint) TTLCache { | ||
return TTLCache{ | ||
lru: NewLRUCache(capacity), | ||
validDuration: valid, | ||
now: time.Now, | ||
} | ||
} | ||
|
||
func (c *TTLCache) Get(key keyType) (valType, bool, time.Duration) { | ||
v, ok := c.lru.Get(key) | ||
if !ok { | ||
return valType{}, false, 0 | ||
} | ||
age := c.now().Sub(v.time) | ||
if age < c.validDuration { | ||
return v.val, ok, age | ||
} else { | ||
c.lru.Delete(key) | ||
return valType{}, false, 0 | ||
} | ||
} | ||
|
||
func (c *TTLCache) Put(key keyType, value valType) { | ||
v := TTLValType{ | ||
val: value, | ||
time: c.now(), | ||
} | ||
c.lru.Put(key, v) | ||
} | ||
|
||
func (c *TTLCache) Delete(key keyType) { | ||
c.lru.Delete(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ifname | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTTLCacheExpire(t *testing.T) { | ||
c := NewTTLCache(1*time.Second, 100) | ||
|
||
c.now = func() time.Time { | ||
return time.Unix(0, 0) | ||
} | ||
|
||
c.Put("ones", nameMap{1: "one"}) | ||
require.Len(t, c.lru.m, 1) | ||
|
||
c.now = func() time.Time { | ||
return time.Unix(1, 0) | ||
} | ||
|
||
_, ok, _ := c.Get("ones") | ||
require.False(t, ok) | ||
require.Len(t, c.lru.m, 0) | ||
require.Equal(t, c.lru.l.Len(), 0) | ||
} | ||
|
||
func TestTTLCache(t *testing.T) { | ||
c := NewTTLCache(1*time.Second, 100) | ||
|
||
c.now = func() time.Time { | ||
return time.Unix(0, 0) | ||
} | ||
|
||
expected := nameMap{1: "one"} | ||
c.Put("ones", expected) | ||
|
||
actual, ok, _ := c.Get("ones") | ||
require.True(t, ok) | ||
require.Equal(t, expected, actual) | ||
} |