Skip to content

Commit

Permalink
fix concurrent cache writes
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Jan 16, 2022
1 parent f3e9beb commit 4980d72
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion dynaml/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"strconv"
"strings"
"sync"

"github.com/mandelsoft/spiff/legacy/candiedyaml"

Expand Down Expand Up @@ -122,6 +123,7 @@ func getArg(key interface{}, value interface{}, wopt WriteOpts, allowyaml bool)
}

var cache = make(map[string][]byte)
var lock sync.Mutex

type Bytes interface {
Bytes() []byte
Expand All @@ -137,6 +139,8 @@ func cachedExecute(cached bool, content *string, args []string) ([]byte, error)
}
hash := fmt.Sprintf("%x", h.Sum(nil))
if cached {
lock.Lock()
defer lock.Unlock()
result := cache[hash]
if result != nil {
debug.Debug("exec: reusing cache %s for %v\n", hash, args)
Expand All @@ -154,7 +158,9 @@ func cachedExecute(cached bool, content *string, args []string) ([]byte, error)
fmt.Fprintf(os.Stderr, "exec: calling %v\n", args)
fmt.Fprintf(os.Stderr, " error: %v\n", stderr)
}
cache[hash] = result
if cached {
cache[hash] = result
}
return result, err
}

Expand Down

0 comments on commit 4980d72

Please sign in to comment.