From e51e11e7a8d1588365d4a133ddb78e6fdf756765 Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Fri, 19 Apr 2024 11:20:21 +0200 Subject: [PATCH] Reduce failpointsMu locking scope Signed-off-by: Chun-Hung Tseng --- runtime/runtime.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/runtime/runtime.go b/runtime/runtime.go index 2356980..71a3408 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -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 } @@ -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 } @@ -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 } @@ -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) }