mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-10 07:27:22 +01:00
http, cache: Track evictions
This commit is contained in:
parent
b7ed233452
commit
832f3655e8
@ -9,15 +9,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
capacity int
|
capacity int
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
entries map[uint64]*list.Element
|
entries map[uint64]*list.Element
|
||||||
values *list.List
|
values *list.List
|
||||||
|
evictions uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheStats struct {
|
type CacheStats struct {
|
||||||
Capacity int
|
Capacity int
|
||||||
Size int
|
Size int
|
||||||
|
Evictions uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCache(capacity int) *Cache {
|
func NewCache(capacity int) *Cache {
|
||||||
@ -55,6 +57,7 @@ func (c *Cache) Set(ip net.IP, resp Response) {
|
|||||||
el = next
|
el = next
|
||||||
evicted++
|
evicted++
|
||||||
}
|
}
|
||||||
|
c.evictions += uint64(evicted)
|
||||||
}
|
}
|
||||||
current, ok := c.entries[k]
|
current, ok := c.entries[k]
|
||||||
if ok {
|
if ok {
|
||||||
@ -81,6 +84,7 @@ func (c *Cache) Resize(capacity int) error {
|
|||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
c.capacity = capacity
|
c.capacity = capacity
|
||||||
|
c.evictions = 0
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +92,8 @@ func (c *Cache) Stats() CacheStats {
|
|||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
return CacheStats{
|
return CacheStats{
|
||||||
Size: len(c.entries),
|
Size: len(c.entries),
|
||||||
Capacity: c.capacity,
|
Capacity: c.capacity,
|
||||||
|
Evictions: c.evictions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,13 @@ import (
|
|||||||
func TestCacheCapacity(t *testing.T) {
|
func TestCacheCapacity(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
addCount, capacity, size int
|
addCount, capacity, size int
|
||||||
|
evictions uint64
|
||||||
}{
|
}{
|
||||||
{1, 0, 0},
|
{1, 0, 0, 0},
|
||||||
{1, 2, 1},
|
{1, 2, 1, 0},
|
||||||
{2, 2, 2},
|
{2, 2, 2, 0},
|
||||||
{3, 2, 2},
|
{3, 2, 2, 1},
|
||||||
|
{10, 5, 5, 5},
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
c := NewCache(tt.capacity)
|
c := NewCache(tt.capacity)
|
||||||
@ -27,6 +29,9 @@ func TestCacheCapacity(t *testing.T) {
|
|||||||
if got := len(c.entries); got != tt.size {
|
if got := len(c.entries); got != tt.size {
|
||||||
t.Errorf("#%d: len(entries) = %d, want %d", i, got, tt.size)
|
t.Errorf("#%d: len(entries) = %d, want %d", i, got, tt.size)
|
||||||
}
|
}
|
||||||
|
if got := c.evictions; got != tt.evictions {
|
||||||
|
t.Errorf("#%d: evictions = %d, want %d", i, got, tt.evictions)
|
||||||
|
}
|
||||||
if tt.capacity > 0 && tt.addCount > tt.capacity && tt.capacity == tt.size {
|
if tt.capacity > 0 && tt.addCount > tt.capacity && tt.capacity == tt.size {
|
||||||
lastAdded := responses[tt.addCount-1]
|
lastAdded := responses[tt.addCount-1]
|
||||||
if _, ok := c.Get(lastAdded.IP); !ok {
|
if _, ok := c.Get(lastAdded.IP); !ok {
|
||||||
@ -57,7 +62,7 @@ func TestCacheDuplicate(t *testing.T) {
|
|||||||
|
|
||||||
func TestCacheResize(t *testing.T) {
|
func TestCacheResize(t *testing.T) {
|
||||||
c := NewCache(10)
|
c := NewCache(10)
|
||||||
for i := 1; i <= 10; i++ {
|
for i := 1; i <= 20; i++ {
|
||||||
ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i))
|
ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i))
|
||||||
r := Response{IP: ip}
|
r := Response{IP: ip}
|
||||||
c.Set(ip, r)
|
c.Set(ip, r)
|
||||||
@ -65,9 +70,15 @@ func TestCacheResize(t *testing.T) {
|
|||||||
if got, want := len(c.entries), 10; got != want {
|
if got, want := len(c.entries), 10; got != want {
|
||||||
t.Errorf("want %d entries, got %d", want, got)
|
t.Errorf("want %d entries, got %d", want, got)
|
||||||
}
|
}
|
||||||
|
if got, want := c.evictions, uint64(10); got != want {
|
||||||
|
t.Errorf("want %d evictions, got %d", want, got)
|
||||||
|
}
|
||||||
if err := c.Resize(5); err != nil {
|
if err := c.Resize(5); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if got, want := c.evictions, uint64(0); got != want {
|
||||||
|
t.Errorf("want %d evictions, got %d", want, got)
|
||||||
|
}
|
||||||
r := Response{IP: net.ParseIP("192.0.2.42")}
|
r := Response{IP: net.ParseIP("192.0.2.42")}
|
||||||
c.Set(r.IP, r)
|
c.Set(r.IP, r)
|
||||||
if got, want := len(c.entries), 5; got != want {
|
if got, want := len(c.entries), 5; got != want {
|
||||||
|
@ -299,11 +299,13 @@ func (s *Server) cacheResizeHandler(w http.ResponseWriter, r *http.Request) *app
|
|||||||
func (s *Server) cacheHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (s *Server) cacheHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
cacheStats := s.cache.Stats()
|
cacheStats := s.cache.Stats()
|
||||||
var data = struct {
|
var data = struct {
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
Capacity int `json:"capacity"`
|
Capacity int `json:"capacity"`
|
||||||
|
Evictions uint64 `json:"evictions"`
|
||||||
}{
|
}{
|
||||||
cacheStats.Size,
|
cacheStats.Size,
|
||||||
cacheStats.Capacity,
|
cacheStats.Capacity,
|
||||||
|
cacheStats.Evictions,
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(data)
|
b, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -187,7 +187,7 @@ func TestCacheHandler(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
want := `{"size":0,"capacity":100}`
|
want := `{"size":0,"capacity":100,"evictions":0}`
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("got %q, want %q", got, want)
|
t.Errorf("got %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user