diff options
author | Mohammed Al Sahaf <[email protected]> | 2024-03-21 21:15:18 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2024-03-21 18:15:18 +0000 |
commit | e7336cc3bfcf60eb775c60509d521e92a5a3b6fe (patch) | |
tree | 13bab6446abffe65401ef476a63fa6681226416e /replacer.go | |
parent | 97a56d860a2254f4d9939746a33698b365d546d6 (diff) | |
download | caddy-e7336cc3bfcf60eb775c60509d521e92a5a3b6fe.tar.gz caddy-e7336cc3bfcf60eb775c60509d521e92a5a3b6fe.zip |
replacer: use RWMutex to protect static provider (#6184)
Diffstat (limited to 'replacer.go')
-rw-r--r-- | replacer.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/replacer.go b/replacer.go index 046be867a..2ad5b8bcb 100644 --- a/replacer.go +++ b/replacer.go @@ -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, @@ -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, @@ -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. @@ -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 @@ -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 } |