Skip to content

Commit

Permalink
Refactor enable, status, and disable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybear327 committed May 3, 2024
1 parent 99477da commit 1c33e68
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
10 changes: 5 additions & 5 deletions runtime/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
15 changes: 1 addition & 14 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
}

0 comments on commit 1c33e68

Please sign in to comment.