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

replacer: use RWMutex to protect static provider #6184

Merged
merged 2 commits into from
Mar 21, 2024
Merged
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
17 changes: 14 additions & 3 deletions replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"
)

// NewReplacer returns a new Replacer.
func NewReplacer() *Replacer {
rep := &Replacer{
static: make(map[string]any),
static: make(map[string]any),
mapMutex: &sync.RWMutex{},
}
rep.providers = []ReplacerFunc{
globalDefaultReplacements,
Expand All @@ -41,7 +43,8 @@ func NewReplacer() *Replacer {
// without the global default replacements.
func NewEmptyReplacer() *Replacer {
rep := &Replacer{
static: make(map[string]any),
static: make(map[string]any),
mapMutex: &sync.RWMutex{},
}
rep.providers = []ReplacerFunc{
rep.fromStatic,
Expand All @@ -54,7 +57,9 @@ func NewEmptyReplacer() *Replacer {
// use NewReplacer to make one.
type Replacer struct {
providers []ReplacerFunc
static map[string]any

static map[string]any
mapMutex *sync.RWMutex
}

// Map adds mapFunc to the list of value providers.
Expand All @@ -65,7 +70,9 @@ func (r *Replacer) Map(mapFunc ReplacerFunc) {

// Set sets a custom variable to a static value.
func (r *Replacer) Set(variable string, value any) {
r.mapMutex.Lock()
r.static[variable] = value
r.mapMutex.Unlock()
}

// Get gets a value from the replacer. It returns
Expand All @@ -89,11 +96,15 @@ func (r *Replacer) GetString(variable string) (string, bool) {
// Delete removes a variable with a static value
// that was created using Set.
func (r *Replacer) Delete(variable string) {
r.mapMutex.Lock()
delete(r.static, variable)
r.mapMutex.Unlock()
}

// fromStatic provides values from r.static.
func (r *Replacer) fromStatic(key string) (any, bool) {
r.mapMutex.RLock()
defer r.mapMutex.RUnlock()
val, ok := r.static[key]
return val, ok
}
Expand Down
4 changes: 4 additions & 0 deletions replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"path/filepath"
"runtime"
"sync"
"testing"
)

Expand Down Expand Up @@ -238,6 +239,7 @@ func TestReplacerSet(t *testing.T) {

func TestReplacerReplaceKnown(t *testing.T) {
rep := Replacer{
mapMutex: &sync.RWMutex{},
providers: []ReplacerFunc{
// split our possible vars to two functions (to test if both functions are called)
func(key string) (val any, ok bool) {
Expand Down Expand Up @@ -310,6 +312,7 @@ func TestReplacerReplaceKnown(t *testing.T) {

func TestReplacerDelete(t *testing.T) {
rep := Replacer{
mapMutex: &sync.RWMutex{},
static: map[string]any{
"key1": "val1",
"key2": "val2",
Expand Down Expand Up @@ -463,5 +466,6 @@ func testReplacer() Replacer {
return Replacer{
providers: make([]ReplacerFunc, 0),
static: make(map[string]any),
mapMutex: &sync.RWMutex{},
}
}
Loading