Skip to content

Commit

Permalink
add mutex to fix race condition with regexes map
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Apr 20, 2016
1 parent 977fd77 commit 0a8f5c6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hound.go
Expand Up @@ -6,9 +6,11 @@ import (
"io/ioutil"
"path/filepath"
"regexp"
"sync"
)

var (
mutex = &sync.Mutex{}
regexes = make(map[string]*regexp.Regexp)
)

Expand Down Expand Up @@ -93,6 +95,12 @@ func (h *Hound) parse(config []byte) error {
// will compile the pattern and store it in the cache. Returns a Regexp
// and an error.
func (h *Hound) regexp(pattern string) (*regexp.Regexp, error) {

// Ensure that we don't encounter a race condition where multiple goroutines
// are attempting to read/write to the regexes map.
defer func() { mutex.Unlock() }()
mutex.Lock()

if regexes[pattern] != nil {
return regexes[pattern], nil
}
Expand Down

0 comments on commit 0a8f5c6

Please sign in to comment.