Skip to content

Commit

Permalink
Fix groutine leaks in cmd/anonymizer/app/writer (#5216)
Browse files Browse the repository at this point in the history
  • Loading branch information
akagami-harsh committed Feb 22, 2024
1 parent 6f7d9a1 commit ef4791e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
23 changes: 21 additions & 2 deletions cmd/anonymizer/app/anonymizer/anonymizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package anonymizer

import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
Expand Down Expand Up @@ -57,6 +58,8 @@ type Anonymizer struct {
lock sync.Mutex
mapping mapping
options Options
cancel context.CancelFunc
wg sync.WaitGroup
}

// Options represents the various options with which the anonymizer can be configured.
Expand All @@ -70,6 +73,7 @@ type Options struct {
// New creates new Anonymizer. The mappingFile stores the mapping from original to
// obfuscated strings, in case later investigations require looking at the original traces.
func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
ctx, cancel := context.WithCancel(context.Background())
a := &Anonymizer{
mappingFile: mappingFile,
logger: logger,
Expand All @@ -78,6 +82,7 @@ func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
Operations: make(map[string]string),
},
options: options,
cancel: cancel,
}
if _, err := os.Stat(filepath.Clean(mappingFile)); err == nil {
dat, err := os.ReadFile(filepath.Clean(mappingFile))
Expand All @@ -88,14 +93,28 @@ func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
logger.Fatal("Cannot unmarshal previous mapping", zap.Error(err))
}
}
a.wg.Add(1)
go func() {
for range time.NewTicker(10 * time.Second).C {
a.SaveMapping()
defer a.wg.Done()
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
a.SaveMapping()
case <-ctx.Done():
return
}
}
}()
return a
}

func (a *Anonymizer) Stop() {
a.cancel()
a.wg.Wait()
}

// SaveMapping writes the mapping from original to obfuscated strings to a file.
// It is called by the anonymizer itself periodically, and should be called at
// the end of the extraction run.
Expand Down
2 changes: 2 additions & 0 deletions cmd/anonymizer/app/anonymizer/anonymizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestNew(t *testing.T) {

file, err := os.CreateTemp(tempDir, "mapping.json")
require.NoError(t, err)
defer file.Close()

_, err = file.Write([]byte(`
{
Expand All @@ -97,6 +98,7 @@ func TestNew(t *testing.T) {
require.NoError(t, err)

anonymizer := New(file.Name(), Options{}, nopLogger)
defer anonymizer.Stop()
assert.NotNil(t, anonymizer)
}

Expand Down
14 changes: 14 additions & 0 deletions cmd/anonymizer/app/writer/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package writer

import (
"testing"

"github.com/jaegertracing/jaeger/pkg/testutils"
)

func TestMain(m *testing.M) {
testutils.VerifyGoLeaks(m)
}
1 change: 1 addition & 0 deletions cmd/anonymizer/app/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,6 @@ func (w *Writer) Close() {
w.capturedFile.Close()
w.anonymizedFile.WriteString("\n]\n")
w.anonymizedFile.Close()
w.anonymizer.Stop()
w.anonymizer.SaveMapping()
}
3 changes: 2 additions & 1 deletion cmd/anonymizer/app/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func TestNew(t *testing.T) {
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
}
_, err := New(config, nopLogger)
writer, err := New(config, nopLogger)
require.NoError(t, err)
defer writer.Close()
})

t.Run("CapturedFile does not exist", func(t *testing.T) {
Expand Down

0 comments on commit ef4791e

Please sign in to comment.