Skip to content

Commit

Permalink
Less bespoke signal handling
Browse files Browse the repository at this point in the history
Use signal.NotifyContext() instead of creating our own go routine
checking for signals and closing a custom channel.
  • Loading branch information
eest committed Sep 13, 2024
1 parent aeeed0e commit 9d2d1fe
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ timerLoop:
}
}
}
case <-edm.stop:
case <-edm.ctx.Done():
break timerLoop
}
}
Expand Down Expand Up @@ -546,6 +546,7 @@ func Run() {
logger.Error("unable to init edm", "error", err)
os.Exit(1)
}
defer edm.stop()

viperNotifyCh := make(chan fsnotify.Event)

Expand Down Expand Up @@ -644,16 +645,6 @@ func Run() {
os.Exit(1)
}

// Exit gracefully on SIGINT or SIGTERM
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
<-sigs

// We received a signal, tell runMinimiser() to stop
close(edm.stop)
}()

metricsServer := &http.Server{
Addr: "127.0.0.1:2112",
ReadTimeout: 10 * time.Second,
Expand Down Expand Up @@ -797,9 +788,10 @@ type dnstapMinimiser struct {
newQnameDiscarded prometheus.Counter
seenQnameLRUEvicted prometheus.Counter
newQnameChannelLen prometheus.Gauge
stop chan struct{} // close this channel to gracefully stop runMinimiser()
debug bool // if we should print debug messages during operation
mutex sync.RWMutex // Mutex for protecting updates to fields at runtime
ctx context.Context
stop context.CancelFunc // call this to gracefully stop runMinimiser()
debug bool // if we should print debug messages during operation
mutex sync.RWMutex // Mutex for protecting updates to fields at runtime
sessionWriterCh chan *prevSessions
histogramWriterCh chan *wellKnownDomainsData
newQnamePublisherCh chan *protocols.EventsMqttMessageNewQnameJson
Expand Down Expand Up @@ -833,6 +825,9 @@ func newDnstapMinimiser(logger *slog.Logger, cryptopanKey string, cryptopanSalt
return nil, fmt.Errorf("newDnstapMinimiser: %w", err)
}

// Exit gracefully on SIGINT or SIGTERM
edm.ctx, edm.stop = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)

// Use separate prometheus registry for each edm instance, otherwise
// trying to run tests where each test do their own call to
// newDnstapMinimiser() will panic:
Expand Down Expand Up @@ -882,7 +877,6 @@ func newDnstapMinimiser(logger *slog.Logger, cryptopanKey string, cryptopanSalt
})

edm.promReg = promReg
edm.stop = make(chan struct{})
// Size 32 matches unexported "const outputChannelSize = 32" in
// https://github.com/dnstap/golang-dnstap/blob/master/dnstap.go
edm.inputChannel = make(chan []byte, 32)
Expand Down Expand Up @@ -1152,7 +1146,7 @@ func (edm *dnstapMinimiser) qnameSeen(msg *dns.Msg, seenQnameLRU *lru.Cache[stri

// runMinimiser reads frames from the inputChannel, doing any modifications and
// then passes them on to a dnstap.Output. To gracefully stop
// runMinimiser() you need to close the edm.stop channel.
// runMinimiser() you can call edm.stop().
func (edm *dnstapMinimiser) runMinimiser(minimiserID int, wg *sync.WaitGroup, dawgFile string, seenQnameLRU *lru.Cache[string, struct{}], pdb *pebble.DB, newQnameBuffer int, disableSessionFiles bool, debugDnstapFile *os.File, disableHistogramSender bool, labelLimit int, wkdTracker *wellKnownDomainsTracker) {
defer wg.Done()

Expand Down Expand Up @@ -1246,7 +1240,7 @@ minimiserLoop:
session := edm.newSession(dt, msg, isQuery, labelLimit, timestamp)
edm.sessionCollectorCh <- session
}
case <-edm.stop:
case <-edm.ctx.Done():
break minimiserLoop
}
}
Expand Down Expand Up @@ -1486,7 +1480,7 @@ timerLoop:
}
}
}
case <-edm.stop:
case <-edm.ctx.Done():
break timerLoop
}
}
Expand Down

0 comments on commit 9d2d1fe

Please sign in to comment.