mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
replacer: use RWMutex to protect static provider (#6184)
This commit is contained in:
parent
97a56d860a
commit
e7336cc3bf
2 changed files with 18 additions and 3 deletions
17
replacer.go
17
replacer.go
|
@ -22,13 +22,15 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewReplacer returns a new Replacer.
|
// NewReplacer returns a new Replacer.
|
||||||
func NewReplacer() *Replacer {
|
func NewReplacer() *Replacer {
|
||||||
rep := &Replacer{
|
rep := &Replacer{
|
||||||
static: make(map[string]any),
|
static: make(map[string]any),
|
||||||
|
mapMutex: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
rep.providers = []ReplacerFunc{
|
rep.providers = []ReplacerFunc{
|
||||||
globalDefaultReplacements,
|
globalDefaultReplacements,
|
||||||
|
@ -41,7 +43,8 @@ func NewReplacer() *Replacer {
|
||||||
// without the global default replacements.
|
// without the global default replacements.
|
||||||
func NewEmptyReplacer() *Replacer {
|
func NewEmptyReplacer() *Replacer {
|
||||||
rep := &Replacer{
|
rep := &Replacer{
|
||||||
static: make(map[string]any),
|
static: make(map[string]any),
|
||||||
|
mapMutex: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
rep.providers = []ReplacerFunc{
|
rep.providers = []ReplacerFunc{
|
||||||
rep.fromStatic,
|
rep.fromStatic,
|
||||||
|
@ -54,7 +57,9 @@ func NewEmptyReplacer() *Replacer {
|
||||||
// use NewReplacer to make one.
|
// use NewReplacer to make one.
|
||||||
type Replacer struct {
|
type Replacer struct {
|
||||||
providers []ReplacerFunc
|
providers []ReplacerFunc
|
||||||
static map[string]any
|
|
||||||
|
static map[string]any
|
||||||
|
mapMutex *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map adds mapFunc to the list of value providers.
|
// 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.
|
// Set sets a custom variable to a static value.
|
||||||
func (r *Replacer) Set(variable string, value any) {
|
func (r *Replacer) Set(variable string, value any) {
|
||||||
|
r.mapMutex.Lock()
|
||||||
r.static[variable] = value
|
r.static[variable] = value
|
||||||
|
r.mapMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get gets a value from the replacer. It returns
|
// 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
|
// Delete removes a variable with a static value
|
||||||
// that was created using Set.
|
// that was created using Set.
|
||||||
func (r *Replacer) Delete(variable string) {
|
func (r *Replacer) Delete(variable string) {
|
||||||
|
r.mapMutex.Lock()
|
||||||
delete(r.static, variable)
|
delete(r.static, variable)
|
||||||
|
r.mapMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// fromStatic provides values from r.static.
|
// fromStatic provides values from r.static.
|
||||||
func (r *Replacer) fromStatic(key string) (any, bool) {
|
func (r *Replacer) fromStatic(key string) (any, bool) {
|
||||||
|
r.mapMutex.RLock()
|
||||||
|
defer r.mapMutex.RUnlock()
|
||||||
val, ok := r.static[key]
|
val, ok := r.static[key]
|
||||||
return val, ok
|
return val, ok
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -238,6 +239,7 @@ func TestReplacerSet(t *testing.T) {
|
||||||
|
|
||||||
func TestReplacerReplaceKnown(t *testing.T) {
|
func TestReplacerReplaceKnown(t *testing.T) {
|
||||||
rep := Replacer{
|
rep := Replacer{
|
||||||
|
mapMutex: &sync.RWMutex{},
|
||||||
providers: []ReplacerFunc{
|
providers: []ReplacerFunc{
|
||||||
// split our possible vars to two functions (to test if both functions are called)
|
// split our possible vars to two functions (to test if both functions are called)
|
||||||
func(key string) (val any, ok bool) {
|
func(key string) (val any, ok bool) {
|
||||||
|
@ -310,6 +312,7 @@ func TestReplacerReplaceKnown(t *testing.T) {
|
||||||
|
|
||||||
func TestReplacerDelete(t *testing.T) {
|
func TestReplacerDelete(t *testing.T) {
|
||||||
rep := Replacer{
|
rep := Replacer{
|
||||||
|
mapMutex: &sync.RWMutex{},
|
||||||
static: map[string]any{
|
static: map[string]any{
|
||||||
"key1": "val1",
|
"key1": "val1",
|
||||||
"key2": "val2",
|
"key2": "val2",
|
||||||
|
@ -463,5 +466,6 @@ func testReplacer() Replacer {
|
||||||
return Replacer{
|
return Replacer{
|
||||||
providers: make([]ReplacerFunc, 0),
|
providers: make([]ReplacerFunc, 0),
|
||||||
static: make(map[string]any),
|
static: make(map[string]any),
|
||||||
|
mapMutex: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue