From f3daf5e041fbacd32a79696fdf4bb65d3e15e487 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Tue, 26 Mar 2024 18:49:50 +0800 Subject: [PATCH] all: make regexp.Regexp variables global when not using configuration methods As the doc of regexp.Regex is saying: "A Regexp is safe for concurrent use by multiple goroutines, except for configuration methods, such as Regexp.Longest." Signed-off-by: Jes Cok --- client/pkg/testutil/leak.go | 5 +++-- etcdctl/ctlv3/command/util.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/client/pkg/testutil/leak.go b/client/pkg/testutil/leak.go index 8c08fbd5123..9e7a738efa0 100644 --- a/client/pkg/testutil/leak.go +++ b/client/pkg/testutil/leak.go @@ -34,6 +34,8 @@ running(leaking) after all tests. ... } */ +var normalizedRegexp = regexp.MustCompile(`\(0[0-9a-fx, ]*\)`) + func CheckLeakedGoroutine() bool { gs := interestingGoroutines() if len(gs) == 0 { @@ -41,10 +43,9 @@ func CheckLeakedGoroutine() bool { } stackCount := make(map[string]int) - re := regexp.MustCompile(`\(0[0-9a-fx, ]*\)`) for _, g := range gs { // strip out pointer arguments in first function of stack dump - normalized := string(re.ReplaceAll([]byte(g), []byte("(...)"))) + normalized := string(normalizedRegexp.ReplaceAll([]byte(g), []byte("(...)"))) stackCount[normalized]++ } diff --git a/etcdctl/ctlv3/command/util.go b/etcdctl/ctlv3/command/util.go index 03ba24ccf94..05fa19bc49e 100644 --- a/etcdctl/ctlv3/command/util.go +++ b/etcdctl/ctlv3/command/util.go @@ -56,9 +56,10 @@ func addHexPrefix(s string) string { return string(ns) } +var argsRegexp = regexp.MustCompile(`"(?:[^"\\]|\\.)*"|'[^']*'|[^'"\s]\S*[^'"\s]?`) + func Argify(s string) []string { - r := regexp.MustCompile(`"(?:[^"\\]|\\.)*"|'[^']*'|[^'"\s]\S*[^'"\s]?`) - args := r.FindAllString(s, -1) + args := argsRegexp.FindAllString(s, -1) for i := range args { if len(args[i]) == 0 { continue