diff --git a/home/dns.go b/home/dns.go index c00ddf51..49b5d0a7 100644 --- a/home/dns.go +++ b/home/dns.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "os" + "path/filepath" "sync" "github.com/AdguardTeam/AdGuardHome/dnsfilter" @@ -34,9 +35,9 @@ func initDNSServer(baseDir string) { log.Fatalf("Cannot create DNS data dir at %s: %s", baseDir, err) } - config.stats = stats.New("./data/stats.db", int(config.DNS.StatsInterval), nil) - if config.stats == nil { - log.Fatal("config.stats == nil") + config.stats, err = stats.New(filepath.Join(baseDir, "stats.db"), int(config.DNS.StatsInterval), nil) + if err != nil { + log.Fatal("Couldn't initialize statistics module") } config.dnsServer = dnsforward.NewServer(baseDir, config.stats) diff --git a/stats/stats.go b/stats/stats.go index 2542b16d..d33ee42d 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -12,7 +12,7 @@ type unitIDCallback func() int // filename: DB file name // limit: time limit (in days) // unitID: user function to get the current unit ID. If nil, the current time hour is used. -func New(filename string, limit int, unitID unitIDCallback) Stats { +func New(filename string, limit int, unitID unitIDCallback) (Stats, error) { return createObject(filename, limit, unitID) } diff --git a/stats/stats_test.go b/stats/stats_test.go index 45b06520..d2e14189 100644 --- a/stats/stats_test.go +++ b/stats/stats_test.go @@ -26,7 +26,7 @@ func UIntArrayEquals(a []uint, b []uint) bool { } func TestStats(t *testing.T) { - s := New("./stats.db", 1, nil) + s, _ := New("./stats.db", 1, nil) e := Entry{} @@ -87,7 +87,7 @@ func TestLargeNumbers(t *testing.T) { // log.SetLevel(log.DEBUG) fn := "./stats.db" os.Remove(fn) - s := New(fn, 1, newID) + s, _ := New(fn, 1, newID) e := Entry{} n := 1000 // number of distinct clients and domains every hour diff --git a/stats/stats_unit.go b/stats/stats_unit.go index 1cc69324..faa30fdb 100644 --- a/stats/stats_unit.go +++ b/stats/stats_unit.go @@ -62,7 +62,7 @@ type unitDB struct { TimeAvg uint // usec } -func createObject(filename string, limitDays int, unitID unitIDCallback) *statsCtx { +func createObject(filename string, limitDays int, unitID unitIDCallback) (*statsCtx, error) { s := statsCtx{} s.limit = limitDays * 24 s.filename = filename @@ -72,7 +72,7 @@ func createObject(filename string, limitDays int, unitID unitIDCallback) *statsC } if !s.dbOpen() { - return nil + return nil, fmt.Errorf("open database") } id := s.unitID() @@ -116,7 +116,7 @@ func createObject(filename string, limitDays int, unitID unitIDCallback) *statsC go s.periodicFlush() log.Debug("Stats: initialized") - return &s + return &s, nil } func (s *statsCtx) dbOpen() bool {