Skip to content

Commit

Permalink
Simplify locking
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel authored and julz committed Aug 28, 2023
1 parent 5bc719a commit b8654f9
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,16 @@ type Config struct {
Rules []*Rule
DisallowUnaliased bool
DisallowExtraAliases bool
muRules sync.RWMutex
muRules sync.Mutex
}

func (c *Config) CompileRegexp() error {
c.muRules.RLock()
rules := c.Rules
c.muRules.RUnlock()
if rules != nil {
return nil
}
c.muRules.Lock()
defer c.muRules.Unlock()
// Check if another process got here first.
if c.Rules != nil {
return nil
}
rules = make([]*Rule, 0, len(c.RequiredAlias))
rules := make([]*Rule, 0, len(c.RequiredAlias))
for path, alias := range c.RequiredAlias {
reg, err := regexp.Compile(fmt.Sprintf("^%s$", path))
if err != nil {
Expand All @@ -40,15 +33,15 @@ func (c *Config) CompileRegexp() error {
Alias: alias,
})
}

c.Rules = rules
return nil
}

func (c *Config) findRule(path string) *Rule {
c.muRules.RLock()
defer c.muRules.RUnlock()
for _, rule := range c.Rules {
c.muRules.Lock()
rules := c.Rules
c.muRules.Unlock()
for _, rule := range rules {
if rule.Regexp.MatchString(path) {
return rule
}
Expand Down

0 comments on commit b8654f9

Please # to comment.