+ new setting "dns.querylog_memsize"

* set 1000 entries by default (not 5000)
This commit is contained in:
Simon Zolin 2019-11-08 12:31:50 +03:00
parent 31ffae7717
commit 7e45c2fc24
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 QueryLogEnabled bool `yaml:"querylog_enabled"` // if true, query log is enabled
QueryLogInterval uint32 `yaml:"querylog_interval"` // time interval for query log (in days) 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"` dnsforward.FilteringConfig `yaml:",inline"`
@ -161,11 +162,9 @@ var config = configuration{
BindPort: 3000, BindPort: 3000,
BindHost: "0.0.0.0", BindHost: "0.0.0.0",
DNS: dnsConfig{ DNS: dnsConfig{
BindHost: "0.0.0.0", BindHost: "0.0.0.0",
Port: 53, Port: 53,
StatsInterval: 1, StatsInterval: 1,
QueryLogEnabled: true,
QueryLogInterval: 1,
FilteringConfig: dnsforward.FilteringConfig{ FilteringConfig: dnsforward.FilteringConfig{
ProtectionEnabled: true, // whether or not use any of dnsfilter features ProtectionEnabled: true, // whether or not use any of dnsfilter features
BlockingMode: "nxdomain", // mode how to answer filtered requests BlockingMode: "nxdomain", // mode how to answer filtered requests
@ -202,6 +201,10 @@ func initConfig() {
config.WebSessionTTLHours = 30 * 24 config.WebSessionTTLHours = 30 * 24
config.DNS.QueryLogEnabled = true
config.DNS.QueryLogInterval = 90
config.DNS.QueryLogMemSize = 1000
config.DNS.CacheSize = 4 * 1024 * 1024 config.DNS.CacheSize = 4 * 1024 * 1024
config.DNS.DnsfilterConf.SafeBrowsingCacheSize = 1 * 1024 * 1024 config.DNS.DnsfilterConf.SafeBrowsingCacheSize = 1 * 1024 * 1024
config.DNS.DnsfilterConf.SafeSearchCacheSize = 1 * 1024 * 1024 config.DNS.DnsfilterConf.SafeSearchCacheSize = 1 * 1024 * 1024
@ -310,6 +313,7 @@ func (c *configuration) write() error {
config.queryLog.WriteDiskConfig(&dc) config.queryLog.WriteDiskConfig(&dc)
config.DNS.QueryLogEnabled = dc.Enabled config.DNS.QueryLogEnabled = dc.Enabled
config.DNS.QueryLogInterval = dc.Interval config.DNS.QueryLogInterval = dc.Interval
config.DNS.QueryLogMemSize = dc.MemSize
} }
if config.dnsFilter != nil { if config.dnsFilter != nil {

View File

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

View File

@ -16,7 +16,6 @@ import (
) )
const ( const (
logBufferCap = 5000 // maximum capacity of buffer before it's flushed to disk
queryLogFileName = "querylog.json" // .gz added during compression queryLogFileName = "querylog.json" // .gz added during compression
getDataLimit = 500 // GetData(): maximum log entries to return 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) l.buffer = append(l.buffer, &entry)
needFlush := false needFlush := false
if !l.flushPending { if !l.flushPending {
needFlush = len(l.buffer) >= logBufferCap needFlush = len(l.buffer) >= int(l.conf.MemSize)
if needFlush { if needFlush {
l.flushPending = true l.flushPending = true
} }

View File

@ -13,6 +13,7 @@ import (
type DiskConfig struct { type DiskConfig struct {
Enabled bool Enabled bool
Interval uint32 Interval uint32
MemSize uint32
} }
// QueryLog - main interface // QueryLog - main interface
@ -32,6 +33,7 @@ type Config struct {
Enabled bool Enabled bool
BaseDir string // directory where log file is stored BaseDir string // directory where log file is stored
Interval uint32 // interval to rotate logs (in days) 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 // Called when the configuration is changed by HTTP request
ConfigModified func() ConfigModified func()

View File

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

View File

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