Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,23 @@ func configUpdater(viperNotifyCh chan fsnotify.Event, edm *dnstapMinimiser) {
})
t.Stop()

for e = range viperNotifyCh {
// If an event has been recevied this means we now want to
// enable the timer so the function will be called "soon", but
// if more events occur we will reset it again. This allows us
// to wait until events on the file settles down before
// actually calling the update function.
t.Reset(100 * time.Millisecond)
for {
select {
case event, ok := <-viperNotifyCh:
if !ok {
return
}
e = event
// If an event has been recevied this means we now want to
// enable the timer so the function will be called "soon", but
// if more events occur we will reset it again. This allows us
// to wait until events on the file settles down before
// actually calling the update function.
t.Reset(100 * time.Millisecond)
case <-edm.ctx.Done():
t.Stop()
return
}
}
}

Expand Down Expand Up @@ -1158,12 +1168,15 @@ func Run(logger *slog.Logger, loggerLevel *slog.LevelVar) {
os.Exit(1)
}

viperNotifyCh := make(chan fsnotify.Event)
viperNotifyCh := make(chan fsnotify.Event, 1)

go configUpdater(viperNotifyCh, edm)

viper.OnConfigChange(func(e fsnotify.Event) {
viperNotifyCh <- e
select {
case viperNotifyCh <- e:
default:
}
})

pdbDir := filepath.Join(startConf.DataDir, "pebble")
Expand Down Expand Up @@ -1415,6 +1428,8 @@ func Run(logger *slog.Logger, loggerLevel *slog.LevelVar) {
edm.log.Info("Run: waiting for other workers to exit")
wg.Wait()

close(viperNotifyCh)

// Wait for graceful disconnection from MQTT bus
if !startConf.DisableMQTT {
edm.log.Info("Run: waiting on MQTT disconnection")
Expand Down
34 changes: 34 additions & 0 deletions pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"os"
"slices"
"strings"
"sync"
"testing"
"time"

dnstap "github.com/dnstap/golang-dnstap"
"github.com/fsnotify/fsnotify"
"github.com/miekg/dns"
"github.com/parquet-go/parquet-go"
"github.com/parquet-go/parquet-go/format"
Expand Down Expand Up @@ -2145,3 +2147,35 @@ func TestWriteHistogramParquetExplicitThreshold(t *testing.T) {
}
}
}
func TestConfigUpdaterExitsOnContextCancel(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
edm, err := newDnstapMinimiser(logger, defaultTC)
if err != nil {
t.Fatalf("newDnstapMinimiser: %s", err)
}
t.Cleanup(edm.stop)

viperNotifyCh := make(chan fsnotify.Event, 1)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
configUpdater(viperNotifyCh, edm)
}()

time.Sleep(50 * time.Millisecond)

edm.stop()

done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("configUpdater did not exit after context cancel")
}
}