Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POA-2904 Use sync.Once #84

Merged
merged 1 commit into from
Feb 19, 2025
Merged
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
16 changes: 6 additions & 10 deletions data_masks/redactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"regexp"
"strings"
"sync/atomic"
"sync"
"time"

pb "github.com/akitasoftware/akita-ir/go/api_spec"
Expand Down Expand Up @@ -36,9 +36,8 @@ type Redactor struct {
// userConfig should exit.
exitChannel chan struct{}

// Whether this redactor is still running. False if and only if exitChannel is
// closed.
running *atomic.Bool
// Ensures that exitChannel is closed at most once.
closeExitChannelOnce *sync.Once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for a * here, but it's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The * avoids copying the Once instance when a Redactor is copied.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I suppose Redactor instances shouldn't be copied regardless!

}

// Creates a redactor for the given service ID. Uses the given learn client to
Expand Down Expand Up @@ -107,22 +106,19 @@ func NewRedactor(
}
}()

running := &atomic.Bool{}
running.Store(true)

return &Redactor{
SensitiveDataKeys: sets.NewSet(config.SensitiveKeys...),
SensitiveDataValuePatterns: sensitiveDataRegex,
userConfig: activeUserConfig,
exitChannel: exitChannel,
running: running,
closeExitChannelOnce: &sync.Once{},
}, nil
}

func (o *Redactor) StopPeriodicUpdates() {
if o.running.Swap(false) {
o.closeExitChannelOnce.Do(func() {
close(o.exitChannel)
}
})
}

func (o *Redactor) RedactSensitiveData(m *pb.Method) {
Expand Down