From cbb993f7ae4311b2a73ace7066a5dabf190291be Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Mon, 18 Nov 2024 14:03:42 +0300 Subject: [PATCH] all: imp code --- internal/home/dns.go | 2 +- internal/querylog/decode.go | 6 +++--- internal/querylog/http.go | 5 +++-- internal/querylog/qlog.go | 8 +++++++- internal/querylog/querylog.go | 11 ++++++----- internal/querylog/querylogfile.go | 1 + internal/querylog/search.go | 2 +- internal/querylog/search_test.go | 2 +- 8 files changed, 23 insertions(+), 14 deletions(-) diff --git a/internal/home/dns.go b/internal/home/dns.go index 601de99d..25d869e8 100644 --- a/internal/home/dns.go +++ b/internal/home/dns.go @@ -533,7 +533,7 @@ func closeDNSServer() { if Context.queryLog != nil { // TODO(s.chzhen): Pass context. - Context.queryLog.Close(context.TODO()) + Context.queryLog.Shutdown(context.TODO()) } log.Debug("all dns modules are closed") diff --git a/internal/querylog/decode.go b/internal/querylog/decode.go index 531bcf1c..e9531ed3 100644 --- a/internal/querylog/decode.go +++ b/internal/querylog/decode.go @@ -682,7 +682,7 @@ func (l *queryLog) resultDecHandler( dec *json.Decoder, ent *logEntry, ) (found bool) { - notFound := false + found = true switch name { case "ReverseHosts": l.decodeResultReverseHosts(ctx, dec, ent) @@ -693,10 +693,10 @@ func (l *queryLog) resultDecHandler( case "DNSRewriteResult": l.decodeResultDNSRewriteResult(ctx, dec, ent) default: - notFound = true + found = false } - return !notFound + return found } // decodeLogEntry decodes string str to logEntry ent. diff --git a/internal/querylog/http.go b/internal/querylog/http.go index fe5050d8..be4e92be 100644 --- a/internal/querylog/http.go +++ b/internal/querylog/http.go @@ -84,14 +84,15 @@ func (l *queryLog) handleQueryLog(w http.ResponseWriter, r *http.Request) { var entries []*logEntry var oldest time.Time + ctx := r.Context() func() { l.confMu.RLock() defer l.confMu.RUnlock() - entries, oldest = l.search(r.Context(), params) + entries, oldest = l.search(ctx, params) }() - resp := l.entriesToJSON(r.Context(), entries, oldest, l.anonymizer.Load()) + resp := l.entriesToJSON(ctx, entries, oldest, l.anonymizer.Load()) aghhttp.WriteJSONResponseOK(w, r, resp) } diff --git a/internal/querylog/qlog.go b/internal/querylog/qlog.go index 2cb4deca..ca54bd3f 100644 --- a/internal/querylog/qlog.go +++ b/internal/querylog/qlog.go @@ -82,6 +82,10 @@ func NewClientProto(s string) (cp ClientProto, err error) { } } +// type check +var _ QueryLog = (*queryLog)(nil) + +// Start implements the [QueryLog] interface for *queryLog. func (l *queryLog) Start(ctx context.Context) { if l.conf.HTTPRegister != nil { l.initWeb() @@ -90,7 +94,8 @@ func (l *queryLog) Start(ctx context.Context) { go l.periodicRotate(ctx) } -func (l *queryLog) Close(ctx context.Context) { +// Shutdown implements the [QueryLog] interface for *queryLog. +func (l *queryLog) Shutdown(ctx context.Context) { l.confMu.RLock() defer l.confMu.RUnlock() @@ -129,6 +134,7 @@ func validateIvl(ivl time.Duration) (err error) { return nil } +// WriteDiskConfig implements the [QueryLog] interface for *queryLog. func (l *queryLog) WriteDiskConfig(c *Config) { l.confMu.RLock() defer l.confMu.RUnlock() diff --git a/internal/querylog/querylog.go b/internal/querylog/querylog.go index 9c2c53cd..3968db70 100644 --- a/internal/querylog/querylog.go +++ b/internal/querylog/querylog.go @@ -17,17 +17,18 @@ import ( "github.com/miekg/dns" ) -// QueryLog - main interface +// QueryLog is the query log interface for use by other packages. type QueryLog interface { + // Start starts the query log. Start(ctx context.Context) - // Close query log object - Close(ctx context.Context) + // Shutdown stops the query log. + Shutdown(ctx context.Context) - // Add a log entry + // Add adds a log entry. Add(params *AddParams) - // WriteDiskConfig - write configuration + // WriteDiskConfig writes the query log configuration to c. WriteDiskConfig(c *Config) // ShouldLog returns true if request for the host should be logged. diff --git a/internal/querylog/querylogfile.go b/internal/querylog/querylogfile.go index 57539669..84da97cf 100644 --- a/internal/querylog/querylogfile.go +++ b/internal/querylog/querylogfile.go @@ -18,6 +18,7 @@ import ( // buffer. func (l *queryLog) flushLogBuffer(ctx context.Context) (err error) { defer func() { err = errors.Annotate(err, "flushing log buffer: %w") }() + l.fileFlushLock.Lock() defer l.fileFlushLock.Unlock() diff --git a/internal/querylog/search.go b/internal/querylog/search.go index adb9ec59..7e5dc3f6 100644 --- a/internal/querylog/search.go +++ b/internal/querylog/search.go @@ -346,7 +346,7 @@ func (l *queryLog) readNextEntry( l.logger.ErrorContext( ctx, "enriching file record at time", - "at_time", e.Time, + "at", e.Time, "client_ip", e.IP, "client_id", e.ClientID, slogutil.KeyError, err, diff --git a/internal/querylog/search_test.go b/internal/querylog/search_test.go index fb6cf5ce..304a4566 100644 --- a/internal/querylog/search_test.go +++ b/internal/querylog/search_test.go @@ -51,7 +51,7 @@ func TestQueryLog_Search_findClient(t *testing.T) { ctx := testutil.ContextWithTimeout(t, testTimeout) t.Cleanup(func() { - l.Close(ctx) + l.Shutdown(ctx) }) q := &dns.Msg{