diff options
| author | Shulhan <ms@kilabit.info> | 2019-04-12 21:05:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-04-12 21:19:19 +0700 |
| commit | e460113b3219dc3a56d03cfb9d0f091890cd9c16 (patch) | |
| tree | 848cf13a0a73503bbc3022b66879523733450e06 | |
| parent | 21dc9bcd808a426f9a37e759d50ec63d7ac907f7 (diff) | |
| download | pakakeh.go-e460113b3219dc3a56d03cfb9d0f091890cd9c16.tar.xz | |
dns: add worker for pruning old caches by threshold
The worker prune process will run based on prune delay and it will remove
any cached answer that has not been accessed less than prune threshold
value.
| -rw-r--r-- | lib/dns/caches.go | 28 | ||||
| -rw-r--r-- | lib/dns/server.go | 5 |
2 files changed, 30 insertions, 3 deletions
diff --git a/lib/dns/caches.go b/lib/dns/caches.go index 14af58d6..27971fd2 100644 --- a/lib/dns/caches.go +++ b/lib/dns/caches.go @@ -6,8 +6,11 @@ package dns import ( "container/list" + "fmt" "sync" "time" + + "github.com/shuLhan/share/lib/debug" ) // @@ -38,7 +41,7 @@ type caches struct { // newCaches create new in memory caches with specific prune delay and // threshold. // The prune delay MUST be greater than 1 minute or it will set to 1 hour. -// The prune threshold MUST be greater than -1 minute or it will be set to 1 +// The prune threshold MUST be greater than -1 minute or it will be set to -1 // hour. // func newCaches(pruneDelay, pruneThreshold time.Duration) (ca *caches) { @@ -56,6 +59,8 @@ func newCaches(pruneDelay, pruneThreshold time.Duration) (ca *caches) { pruneThreshold: pruneThreshold, } + go ca.startWorker() + return } @@ -117,6 +122,10 @@ func (c *caches) prune() { break } + if debug.Value >= 1 { + fmt.Printf("dns: - 0:%s\n", an.msg.Question) + } + next := e.Next() _ = c.lru.Remove(e) c.remove(an) @@ -173,3 +182,20 @@ func (c *caches) upsert(nu *answer) (inserted bool) { return inserted } + +// +// startWorker start the worker pruning process. +// +// The worker prune process will run based on prune delay and it will remove +// any cached answer that has not been accessed less than prune threshold +// value. +// +func (c *caches) startWorker() { + ticker := time.NewTicker(c.pruneDelay) + + for t := range ticker.C { + fmt.Printf("dns: pruning at %v\n", t) + + c.prune() + } +} diff --git a/lib/dns/server.go b/lib/dns/server.go index d5650e6a..c24cd1e6 100644 --- a/lib/dns/server.go +++ b/lib/dns/server.go @@ -53,7 +53,8 @@ import ( // > : the answer is sent to client // ! : no answer found on cache and the query is not recursive // ^ : request is forwarded to parent name server -// - : answer exist on cache but its expired +// ~ : answer exist on cache but its expired +// - : answer is pruned from caches // + : new answer is added to caches // # : the expired answer is renewed and updated on caches // @@ -553,7 +554,7 @@ func (srv *Server) processRequest() { } else { if an.msg.IsExpired() && srv.hasForwarders { if debug.Value >= 1 { - fmt.Printf("dns: - %d:%s\n", + fmt.Printf("dns: ~ %d:%s\n", req.message.Header.ID, req.message.Question) } |
