mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 11:22:49 +03:00
coredns plugin -- Cache /querylog API result
This commit is contained in:
parent
763dcc46e9
commit
a15c59e24e
@ -19,13 +19,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
logBufferCap = 1000 // maximum capacity of logBuffer before it's flushed to disk
|
logBufferCap = 5000 // maximum capacity of logBuffer before it's flushed to disk
|
||||||
queryLogAPI = 1000 // maximum API response for /querylog
|
queryLogCacheSize = 1000 // maximum API response for /querylog
|
||||||
|
queryLogCacheTime = time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logBufferLock sync.RWMutex
|
logBufferLock sync.RWMutex
|
||||||
logBuffer []logEntry
|
logBuffer []logEntry
|
||||||
|
|
||||||
|
queryLogCache []logEntry
|
||||||
|
queryLogLock sync.RWMutex
|
||||||
|
queryLogTime time.Time
|
||||||
)
|
)
|
||||||
|
|
||||||
type logEntry struct {
|
type logEntry struct {
|
||||||
@ -93,14 +98,26 @@ func logRequest(question *dns.Msg, answer *dns.Msg, result dnsfilter.Result, ela
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleQueryLog(w http.ResponseWriter, r *http.Request) {
|
func handleQueryLog(w http.ResponseWriter, r *http.Request) {
|
||||||
// TODO: fetch values from disk if len(logBuffer) < queryLogSize
|
now := time.Now()
|
||||||
// TODO: cache output
|
|
||||||
|
queryLogLock.RLock()
|
||||||
|
values := queryLogCache
|
||||||
|
needRefresh := now.Sub(queryLogTime) >= queryLogCacheTime
|
||||||
|
queryLogLock.RUnlock()
|
||||||
|
|
||||||
|
if needRefresh {
|
||||||
|
// need to get fresh data
|
||||||
logBufferLock.RLock()
|
logBufferLock.RLock()
|
||||||
values := logBuffer
|
values := logBuffer
|
||||||
logBufferLock.RUnlock()
|
logBufferLock.RUnlock()
|
||||||
|
|
||||||
if len(values) < queryLogAPI {
|
if len(values) < queryLogCacheSize {
|
||||||
values = appendFromLogFile(values, queryLogAPI, time.Hour*24)
|
values = appendFromLogFile(values, queryLogCacheSize, time.Hour*24)
|
||||||
|
}
|
||||||
|
queryLogLock.Lock()
|
||||||
|
queryLogCache = values
|
||||||
|
queryLogTime = now
|
||||||
|
queryLogLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = []map[string]interface{}{}
|
var data = []map[string]interface{}{}
|
||||||
|
Loading…
Reference in New Issue
Block a user