From 1c33e686693476150c20f331ee22a1e01a7e2272 Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Fri, 3 May 2024 23:36:13 +0800 Subject: [PATCH] Refactor enable, status, and disable functions --- runtime/http.go | 10 +++++----- runtime/runtime.go | 15 +-------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/runtime/http.go b/runtime/http.go index 3c27681..f006869 100644 --- a/runtime/http.go +++ b/runtime/http.go @@ -72,7 +72,7 @@ func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } for k, v := range fpMap { - if err := enable(k, v); err != nil { + if err := Enable(k, v); err != nil { http.Error(w, fmt.Sprintf("fail to set failpoint: %v", err), http.StatusBadRequest) return } @@ -86,13 +86,13 @@ func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { sort.Strings(fps) lines := make([]string, len(fps)) for i := range lines { - s, _, _ := status(fps[i]) + s, _, _ := Status(fps[i]) lines[i] = fps[i] + "=" + s } w.Write([]byte(strings.Join(lines, "\n") + "\n")) } else if strings.HasSuffix(key, "/count") { fp := key[:len(key)-len("/count")] - _, count, err := status(fp) + _, count, err := Status(fp) if err != nil { if errors.Is(err, ErrNoExist) { http.Error(w, "failed to GET: "+err.Error(), http.StatusNotFound) @@ -103,7 +103,7 @@ func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } w.Write([]byte(strconv.Itoa(count))) } else { - status, _, err := status(key) + status, _, err := Status(key) if err != nil { http.Error(w, "failed to GET: "+err.Error(), http.StatusNotFound) } @@ -112,7 +112,7 @@ func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // deactivates a failpoint case r.Method == "DELETE": - if err := disable(key); err != nil { + if err := Disable(key); err != nil { http.Error(w, "failed to delete failpoint "+err.Error(), http.StatusBadRequest) return } diff --git a/runtime/runtime.go b/runtime/runtime.go index 5aa7588..1e57932 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -72,11 +72,6 @@ func parseFailpoints(fps string) (map[string]string, error) { // Enable sets a failpoint to a given failpoint description. func Enable(name, inTerms string) error { - return enable(name, inTerms) -} - -// enable enables a failpoint -func enable(name, inTerms string) error { failpointsMu.RLock() fp := failpoints[name] failpointsMu.RUnlock() @@ -100,10 +95,6 @@ func enable(name, inTerms string) error { // Disable stops a failpoint from firing. func Disable(name string) error { - return disable(name) -} - -func disable(name string) error { failpointsMu.RLock() fp := failpoints[name] failpointsMu.RUnlock() @@ -124,10 +115,6 @@ func disable(name string) error { // Status gives the current setting and execution count for the failpoint func Status(failpath string) (string, int, error) { - return status(failpath) -} - -func status(failpath string) (string, int, error) { failpointsMu.RLock() fp := failpoints[failpath] failpointsMu.RUnlock() @@ -171,7 +158,7 @@ func register(name string) *Failpoint { failpoints[name] = fp failpointsMu.Unlock() if t, ok := envTerms[name]; ok { - enable(name, t) + Enable(name, t) } return fp }