Skip to content

Commit

Permalink
Fix ticker leaks.
Browse files Browse the repository at this point in the history
Now that we allow adding and removing probes dynamically, we should properly clean up ticker channels in probes.

PiperOrigin-RevId: 267886277
  • Loading branch information
manugarg committed Sep 9, 2019
1 parent 8b8c1f6 commit cf7e2f0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion probes/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ func (p *Probe) Start(ctx context.Context, dataChan chan *metrics.EventMetrics)

go probeutils.StatsKeeper(ctx, "dns", p.name, p.opts.StatsExportInterval, targetsFunc, resultsChan, dataChan, p.opts.LogMetrics, p.l)

for range time.Tick(p.opts.Interval) {
ticker := time.NewTicker(p.opts.Interval)
defer ticker.Stop()

for range ticker.C {
// Don't run another probe if context is canceled already.
select {
case <-ctx.Done():
Expand Down
6 changes: 5 additions & 1 deletion probes/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,11 @@ func (p *Probe) runProbe(ctx context.Context) {
// Start starts and runs the probe indefinitely.
func (p *Probe) Start(ctx context.Context, dataChan chan *metrics.EventMetrics) {
p.dataChan = dataChan
for range time.Tick(p.opts.Interval) {

ticker := time.NewTicker(p.opts.Interval)
defer ticker.Stop()

for range ticker.C {
// Don't run another probe if context is canceled already.
select {
case <-ctx.Done():
Expand Down
5 changes: 4 additions & 1 deletion probes/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ func (p *Probe) runProbe(ctx context.Context) {

// Start starts and runs the probe indefinitely.
func (p *Probe) Start(ctx context.Context, dataChan chan *metrics.EventMetrics) {
for ts := range time.Tick(p.opts.Interval) {
ticker := time.NewTicker(p.opts.Interval)
defer ticker.Stop()

for ts := range ticker.C {
// Don't run another probe if context is canceled already.
select {
case <-ctx.Done():
Expand Down
6 changes: 5 additions & 1 deletion probes/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,11 @@ func (p *Probe) Start(ctx context.Context, dataChan chan *metrics.EventMetrics)
p.l.Critical("Probe has not been properly initialized yet.")
}
defer p.conn.close()
for ts := range time.Tick(p.opts.Interval) {

ticker := time.NewTicker(p.opts.Interval)
defer ticker.Stop()

for ts := range ticker.C {
// Don't run another probe if context is canceled already.
select {
case <-ctx.Done():
Expand Down
5 changes: 3 additions & 2 deletions probes/probeutils/probeutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type ProbeResult interface {
// updated on a regular basis.
func StatsKeeper(ctx context.Context, ptype, name string, exportInterval time.Duration, targetsFunc func() []string, resultsChan <-chan ProbeResult, dataChan chan<- *metrics.EventMetrics, logMetrics func(*metrics.EventMetrics), l *logger.Logger) {
targetMetrics := make(map[string]*metrics.EventMetrics)
doExport := time.Tick(exportInterval)
exportTicker := time.NewTicker(exportInterval)
defer exportTicker.Stop()

for {
select {
Expand All @@ -76,7 +77,7 @@ func StatsKeeper(ctx context.Context, ptype, name string, exportInterval time.Du
if err != nil {
l.Errorf("Error adding metrics from the probe result for the target: %s. Err: %v", t, err)
}
case ts := <-doExport:
case ts := <-exportTicker.C:
for _, t := range targetsFunc() {
em := targetMetrics[t]
if em != nil {
Expand Down

0 comments on commit cf7e2f0

Please sign in to comment.