Skip to content

Commit

Permalink
Reduce failpointsMu locking scope
Browse files Browse the repository at this point in the history
Signed-off-by: Chun-Hung Tseng <[email protected]>
  • Loading branch information
henrybear327 authored and Chun-Hung Tseng committed May 3, 2024
1 parent fb4eae7 commit e51e11e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ func parseFailpoints(fps string) (map[string]string, error) {

// Enable sets a failpoint to a given failpoint description.
func Enable(name, inTerms string) error {
failpointsMu.Lock()
defer failpointsMu.Unlock()
return enable(name, inTerms)
}

// enable enables a failpoint
func enable(name, inTerms string) error {
failpointsMu.RLock()
fp := failpoints[name]
failpointsMu.RUnlock()
if fp == nil {
return ErrNoExist
}
Expand All @@ -97,13 +97,13 @@ func enable(name, inTerms string) error {

// Disable stops a failpoint from firing.
func Disable(name string) error {
failpointsMu.Lock()
defer failpointsMu.Unlock()
return disable(name)
}

func disable(name string) error {
failpointsMu.RLock()
fp := failpoints[name]
failpointsMu.RUnlock()
if fp == nil {
return ErrNoExist
}
Expand All @@ -121,14 +121,13 @@ func disable(name string) error {

// Status gives the current setting and execution count for the failpoint
func Status(failpath string) (string, int, error) {
failpointsMu.Lock()
defer failpointsMu.Unlock()

return status(failpath)
}

func status(failpath string) (string, int, error) {
failpointsMu.RLock()
fp := failpoints[failpath]
failpointsMu.RUnlock()
if fp == nil {
return "", 0, ErrNoExist
}
Expand Down Expand Up @@ -160,13 +159,14 @@ func list() []string {

func register(name string) *Failpoint {
failpointsMu.Lock()
defer failpointsMu.Unlock()
if _, ok := failpoints[name]; ok {
failpointsMu.Unlock()
panic(fmt.Sprintf("failpoint name %s is already registered.", name))
}

fp := &Failpoint{}
failpoints[name] = fp
failpointsMu.Unlock()
if t, ok := envTerms[name]; ok {
enable(name, t)
}
Expand Down

0 comments on commit e51e11e

Please sign in to comment.