http, cache: Expose cache stats

This commit is contained in:
Martin Polden 2020-09-11 20:52:35 +02:00
parent df49848167
commit ceaff84709
3 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package http
import (
"container/list"
"fmt"
"hash/fnv"
"net"
"sync"
@ -14,6 +15,11 @@ type Cache struct {
values *list.List
}
type CacheStats struct {
Capacity int
Size int
}
func NewCache(capacity int) *Cache {
if capacity < 0 {
capacity = 0
@ -63,3 +69,12 @@ func (c *Cache) Get(ip net.IP) (Response, bool) {
}
return r.Value.(Response), true
}
func (c *Cache) Stats() CacheStats {
c.mu.RLock()
defer c.mu.RUnlock()
return CacheStats{
Size: len(c.entries),
Capacity: c.capacity,
}
}

View File

@ -271,6 +271,24 @@ func (s *Server) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
return nil
}
func (s *Server) cacheHandler(w http.ResponseWriter, r *http.Request) *appError {
cacheStats := s.cache.Stats()
var data = struct {
Size int `json:"size"`
Capacity int `json:"capacity"`
}{
cacheStats.Size,
cacheStats.Capacity,
}
b, err := json.Marshal(data)
if err != nil {
return internalServerError(err).AsJSON()
}
w.Header().Set("Content-Type", jsonMediaType)
w.Write(b)
return nil
}
func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appError {
response, err := s.newResponse(r)
if err != nil {
@ -391,6 +409,7 @@ func (s *Server) Handler() http.Handler {
// Profiling
if s.profile {
r.Route("GET", "/debug/cache/", s.cacheHandler)
r.Route("GET", "/debug/pprof/cmdline", wrapHandlerFunc(pprof.Cmdline))
r.Route("GET", "/debug/pprof/profile", wrapHandlerFunc(pprof.Profile))
r.Route("GET", "/debug/pprof/symbol", wrapHandlerFunc(pprof.Symbol))

View File

@ -160,6 +160,21 @@ func TestJSONHandlers(t *testing.T) {
}
}
func TestCacheHandler(t *testing.T) {
log.SetOutput(ioutil.Discard)
srv := testServer()
srv.profile = true
s := httptest.NewServer(srv.Handler())
got, _, err := httpGet(s.URL+"/debug/cache/", jsonMediaType, "")
if err != nil {
t.Fatal(err)
}
want := `{"size":0,"capacity":100}`
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestIPFromRequest(t *testing.T) {
var tests = []struct {
remoteAddr string