Merge: + QueryLog: new setting "dns.querylog_memsize"

Close #958

* commit '7e45c2fc2428679ac7454c5244b0130901449324':
  + new setting "dns.querylog_memsize"
This commit is contained in:
Simon Zolin 2019-12-02 15:57:17 +03:00
commit 48c70aefaa
6 changed files with 15 additions and 8 deletions

View File

@ -106,6 +106,7 @@ type dnsConfig struct {
QueryLogEnabled bool `yaml:"querylog_enabled"` // if true, query log is enabled
QueryLogInterval uint32 `yaml:"querylog_interval"` // time interval for query log (in days)
QueryLogMemSize uint32 `yaml:"querylog_memsize"` // number of entries kept in memory before they are flushed to disk
dnsforward.FilteringConfig `yaml:",inline"`
@ -164,8 +165,6 @@ var config = configuration{
BindHost: "0.0.0.0",
Port: 53,
StatsInterval: 1,
QueryLogEnabled: true,
QueryLogInterval: 1,
FilteringConfig: dnsforward.FilteringConfig{
ProtectionEnabled: true, // whether or not use any of dnsfilter features
BlockingMode: "nxdomain", // mode how to answer filtered requests
@ -202,6 +201,10 @@ func initConfig() {
config.WebSessionTTLHours = 30 * 24
config.DNS.QueryLogEnabled = true
config.DNS.QueryLogInterval = 90
config.DNS.QueryLogMemSize = 1000
config.DNS.CacheSize = 4 * 1024 * 1024
config.DNS.DnsfilterConf.SafeBrowsingCacheSize = 1 * 1024 * 1024
config.DNS.DnsfilterConf.SafeSearchCacheSize = 1 * 1024 * 1024
@ -310,6 +313,7 @@ func (c *configuration) write() error {
config.queryLog.WriteDiskConfig(&dc)
config.DNS.QueryLogEnabled = dc.Enabled
config.DNS.QueryLogInterval = dc.Interval
config.DNS.QueryLogMemSize = dc.MemSize
}
if config.dnsFilter != nil {

View File

@ -50,6 +50,7 @@ func initDNSServer() {
Enabled: config.DNS.QueryLogEnabled,
BaseDir: baseDir,
Interval: config.DNS.QueryLogInterval,
MemSize: config.DNS.QueryLogMemSize,
ConfigModified: onConfigModified,
HTTPRegister: httpRegister,
}

View File

@ -16,7 +16,6 @@ import (
)
const (
logBufferCap = 5000 // maximum capacity of buffer before it's flushed to disk
queryLogFileName = "querylog.json" // .gz added during compression
getDataLimit = 500 // GetData(): maximum log entries to return
@ -147,7 +146,7 @@ func (l *queryLog) Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Res
l.buffer = append(l.buffer, &entry)
needFlush := false
if !l.flushPending {
needFlush = len(l.buffer) >= logBufferCap
needFlush = len(l.buffer) >= int(l.conf.MemSize)
if needFlush {
l.flushPending = true
}

View File

@ -13,6 +13,7 @@ import (
type DiskConfig struct {
Enabled bool
Interval uint32
MemSize uint32
}
// QueryLog - main interface
@ -32,6 +33,7 @@ type Config struct {
Enabled bool
BaseDir string // directory where log file is stored
Interval uint32 // interval to rotate logs (in days)
MemSize uint32 // number of entries kept in memory before they are flushed to disk
// Called when the configuration is changed by HTTP request
ConfigModified func()

View File

@ -27,7 +27,7 @@ func (l *queryLog) flushLogBuffer(fullFlush bool) error {
// flush remainder to file
l.bufferLock.Lock()
needFlush := len(l.buffer) >= logBufferCap
needFlush := len(l.buffer) >= int(l.conf.MemSize)
if !needFlush && !fullFlush {
l.bufferLock.Unlock()
return nil

View File

@ -23,6 +23,7 @@ func TestQueryLog(t *testing.T) {
conf := Config{
Enabled: true,
Interval: 1,
MemSize: 100,
}
conf.BaseDir = prepareTestDir()
defer func() { _ = os.RemoveAll(conf.BaseDir) }()