-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from postfinance/feat/recent-neighbours-checks
feat/recent neighbours checks
- Loading branch information
Showing
10 changed files
with
153 additions
and
14 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,56 @@ | ||
package kubenurse | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// This TTLCache is a suboptimal, first-shot implementation of TTL cache, | ||
// that is, entries expire after a certain duration. | ||
// It should ideally be implemented with a Red-Black Binary tree to keep track | ||
// of the entries to expire. | ||
|
||
type TTLCache[K comparable] struct { | ||
m map[K]*CacheEntry[K] | ||
TTL time.Duration | ||
mu sync.Mutex | ||
} | ||
|
||
type CacheEntry[K comparable] struct { | ||
val K | ||
lastInsert time.Time | ||
} | ||
|
||
func (c *TTLCache[K]) Init(ttl time.Duration) { | ||
c.m = make(map[K]*CacheEntry[K]) | ||
c.mu = sync.Mutex{} | ||
c.TTL = ttl | ||
} | ||
|
||
func (c *TTLCache[K]) Insert(k K) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
if entry, ok := c.m[k]; ok { | ||
entry.lastInsert = time.Now() | ||
} else { | ||
entry := CacheEntry[K]{val: k, lastInsert: time.Now()} | ||
c.m[k] = &entry | ||
} | ||
} | ||
|
||
func (c *TTLCache[K]) ActiveEntries() int { | ||
c.cleanupExpired() | ||
return len(c.m) | ||
} | ||
|
||
func (c *TTLCache[K]) cleanupExpired() { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
for k, entry := range c.m { | ||
if time.Since(entry.lastInsert) > c.TTL { | ||
delete(c.m, k) | ||
} | ||
} | ||
} |
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,27 @@ | ||
package kubenurse | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTTLCache(t *testing.T) { | ||
|
||
c := TTLCache[string]{} | ||
c.Init(10 * time.Millisecond) | ||
|
||
c.Insert("node-a") | ||
time.Sleep(5 * time.Millisecond) | ||
|
||
require.Equal(t, 1, c.ActiveEntries(), "after 5ms and with a 10ms TTL, there should have been 1 entry in the cache") | ||
|
||
c.Insert("node-a") // refresh ttl | ||
time.Sleep(5 * time.Millisecond) | ||
require.Equal(t, 1, c.ActiveEntries(), "after 5ms and with a 10ms TTL, there should have been 1 entry in the cache") | ||
|
||
time.Sleep(5 * time.Millisecond) | ||
require.Equal(t, 0, c.ActiveEntries(), "after 10ms and with a 10ms TTL, there should be no entry in the cache") | ||
|
||
} |
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