Skip to content

Commit

Permalink
fixed race conditions when manipulating rules
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-iniguez-goya committed Jun 20, 2020
1 parent 43f2a56 commit 6d24c54
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions daemon/rule/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func NewLoader(liveReload bool) (*Loader, error) {

// NumRules returns he number of loaded rules.
func (l *Loader) NumRules() int {
l.Lock()
defer l.Unlock()
l.RLock()
defer l.RUnlock()
return len(l.rules)
}

Expand Down Expand Up @@ -216,6 +216,9 @@ func (l *Loader) Add(rule *Rule, saveToDisk bool) error {
func (l *Loader) Replace(rule *Rule, saveToDisk bool) error {
l.replaceUserRule(rule)
if saveToDisk {
l.Lock()
defer l.Unlock()

fileName := filepath.Join(l.path, fmt.Sprintf("%s.json", rule.Name))
return l.Save(rule, fileName)
}
Expand All @@ -241,8 +244,8 @@ func (l *Loader) Save(rule *Rule, path string) error {
// If the duration is Always (i.e: saved on disk), it'll attempt to delete
// it from disk.
func (l *Loader) Delete(ruleName string) error {
l.RLock()
defer l.RUnlock()
l.Lock()
defer l.Unlock()

rule := l.rules[ruleName]
delete(l.rules, ruleName)
Expand Down

0 comments on commit 6d24c54

Please sign in to comment.