Skip to content

Commit

Permalink
Added the possibility of retrieving query cache with a timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Dondero committed Oct 30, 2018
1 parent e105bf9 commit b7d1290
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
9 changes: 8 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ func StartAPIServer(config *Config,
})

router.GET("/questioncache", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"length": questionCache.Length(), "items": questionCache.Backend})
highWater, err := strconv.ParseInt(c.DefaultQuery("highWater", "-1"), 10, 64)
if err != nil {
highWater = -1
}
c.IndentedJSON(http.StatusOK, gin.H{
"length": questionCache.Length(),
"items": questionCache.GetOlder(highWater),
})
})

router.GET("/questioncache/length", func(c *gin.Context) {
Expand Down
12 changes: 12 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,15 @@ func (c *MemoryQuestionCache) Length() int {
defer c.mu.RUnlock()
return len(c.Backend)
}

// GetOlder eturns a slice of the entries older than `time`
func (c *MemoryQuestionCache) GetOlder(time int64) []QuestionCacheEntry {
c.mu.RLock()
defer c.mu.RUnlock()
for i, e := range c.Backend {
if e.Date > time {
return c.Backend[i:]
}
}
return []QuestionCacheEntry{}
}
25 changes: 25 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,28 @@ func TestExpirationRace(t *testing.T) {
go cache.Set(testDomain, m, true)
}
}

func addToCache(cache *MemoryQuestionCache, time int64) {
cache.Add(QuestionCacheEntry{
Date: time,
Remote: fmt.Sprintf("%d", time),
Blocked: true,
Query: Question{},
})
}

func TestQuestionCacheGetFromTimestamp(t *testing.T) {
memCache := makeQuestionCache(100)
for i := 0; i < 100; i++ {
addToCache(memCache, int64(i))
}

entries := memCache.GetOlder(50)
assert.Len(t, entries, 49)
entries = memCache.GetOlder(0)
assert.Len(t, entries, 99)
entries = memCache.GetOlder(-1)
assert.Len(t, entries, 100)
entries = memCache.GetOlder(200)
assert.Len(t, entries, 0)
}
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ apidebug = false
bind = "0.0.0.0:53"
# address to bind to for the API server
api = "127.0.0.1:18080"
api = "127.0.0.1:8080"
# ipv4 address to forward blocked queries to
nullroute = "0.0.0.0"
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func main() {
// BlockCache contains all blocked domains
blockCache := &MemoryBlockCache{Backend: make(map[string]bool)}
// QuestionCache contains all queries to the dns server
questionCache := &MemoryQuestionCache{Backend: make([]QuestionCacheEntry, 0), Maxcount: 1000}
questionCache.Maxcount = config.QuestionCacheCap
questionCache := makeQuestionCache(config.QuestionCacheCap)

reloadChan := make(chan bool)

Expand Down
8 changes: 0 additions & 8 deletions test_utils.go

This file was deleted.

12 changes: 12 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func makeCache() MemoryCache {
return MemoryCache{
Backend: make(map[string]*Mesg, 0),
Maxcount: 0,
}
}

func makeQuestionCache(maxCount int) *MemoryQuestionCache {
return &MemoryQuestionCache{Backend: make([]QuestionCacheEntry, 0), Maxcount: maxCount}
}

0 comments on commit b7d1290

Please sign in to comment.