all: imp code

This commit is contained in:
Stanislav Chzhen 2024-11-18 14:03:42 +03:00
parent 8d88d79930
commit cbb993f7ae
8 changed files with 23 additions and 14 deletions

View File

@ -533,7 +533,7 @@ func closeDNSServer() {
if Context.queryLog != nil { if Context.queryLog != nil {
// TODO(s.chzhen): Pass context. // TODO(s.chzhen): Pass context.
Context.queryLog.Close(context.TODO()) Context.queryLog.Shutdown(context.TODO())
} }
log.Debug("all dns modules are closed") log.Debug("all dns modules are closed")

View File

@ -682,7 +682,7 @@ func (l *queryLog) resultDecHandler(
dec *json.Decoder, dec *json.Decoder,
ent *logEntry, ent *logEntry,
) (found bool) { ) (found bool) {
notFound := false found = true
switch name { switch name {
case "ReverseHosts": case "ReverseHosts":
l.decodeResultReverseHosts(ctx, dec, ent) l.decodeResultReverseHosts(ctx, dec, ent)
@ -693,10 +693,10 @@ func (l *queryLog) resultDecHandler(
case "DNSRewriteResult": case "DNSRewriteResult":
l.decodeResultDNSRewriteResult(ctx, dec, ent) l.decodeResultDNSRewriteResult(ctx, dec, ent)
default: default:
notFound = true found = false
} }
return !notFound return found
} }
// decodeLogEntry decodes string str to logEntry ent. // decodeLogEntry decodes string str to logEntry ent.

View File

@ -84,14 +84,15 @@ func (l *queryLog) handleQueryLog(w http.ResponseWriter, r *http.Request) {
var entries []*logEntry var entries []*logEntry
var oldest time.Time var oldest time.Time
ctx := r.Context()
func() { func() {
l.confMu.RLock() l.confMu.RLock()
defer l.confMu.RUnlock() 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) aghhttp.WriteJSONResponseOK(w, r, resp)
} }

View File

@ -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) { func (l *queryLog) Start(ctx context.Context) {
if l.conf.HTTPRegister != nil { if l.conf.HTTPRegister != nil {
l.initWeb() l.initWeb()
@ -90,7 +94,8 @@ func (l *queryLog) Start(ctx context.Context) {
go l.periodicRotate(ctx) 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() l.confMu.RLock()
defer l.confMu.RUnlock() defer l.confMu.RUnlock()
@ -129,6 +134,7 @@ func validateIvl(ivl time.Duration) (err error) {
return nil return nil
} }
// WriteDiskConfig implements the [QueryLog] interface for *queryLog.
func (l *queryLog) WriteDiskConfig(c *Config) { func (l *queryLog) WriteDiskConfig(c *Config) {
l.confMu.RLock() l.confMu.RLock()
defer l.confMu.RUnlock() defer l.confMu.RUnlock()

View File

@ -17,17 +17,18 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// QueryLog - main interface // QueryLog is the query log interface for use by other packages.
type QueryLog interface { type QueryLog interface {
// Start starts the query log.
Start(ctx context.Context) Start(ctx context.Context)
// Close query log object // Shutdown stops the query log.
Close(ctx context.Context) Shutdown(ctx context.Context)
// Add a log entry // Add adds a log entry.
Add(params *AddParams) Add(params *AddParams)
// WriteDiskConfig - write configuration // WriteDiskConfig writes the query log configuration to c.
WriteDiskConfig(c *Config) WriteDiskConfig(c *Config)
// ShouldLog returns true if request for the host should be logged. // ShouldLog returns true if request for the host should be logged.

View File

@ -18,6 +18,7 @@ import (
// buffer. // buffer.
func (l *queryLog) flushLogBuffer(ctx context.Context) (err error) { func (l *queryLog) flushLogBuffer(ctx context.Context) (err error) {
defer func() { err = errors.Annotate(err, "flushing log buffer: %w") }() defer func() { err = errors.Annotate(err, "flushing log buffer: %w") }()
l.fileFlushLock.Lock() l.fileFlushLock.Lock()
defer l.fileFlushLock.Unlock() defer l.fileFlushLock.Unlock()

View File

@ -346,7 +346,7 @@ func (l *queryLog) readNextEntry(
l.logger.ErrorContext( l.logger.ErrorContext(
ctx, ctx,
"enriching file record at time", "enriching file record at time",
"at_time", e.Time, "at", e.Time,
"client_ip", e.IP, "client_ip", e.IP,
"client_id", e.ClientID, "client_id", e.ClientID,
slogutil.KeyError, err, slogutil.KeyError, err,

View File

@ -51,7 +51,7 @@ func TestQueryLog_Search_findClient(t *testing.T) {
ctx := testutil.ContextWithTimeout(t, testTimeout) ctx := testutil.ContextWithTimeout(t, testTimeout)
t.Cleanup(func() { t.Cleanup(func() {
l.Close(ctx) l.Shutdown(ctx)
}) })
q := &dns.Msg{ q := &dns.Msg{