From 3554886ce1d6d5e69dd6cdb0d5d6c020ab678dcb Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Tue, 12 Oct 2021 11:29:22 -0400 Subject: [PATCH] vformat supports multi-core processing (#757) * Feat: vformat supports multi-core processing (#996) Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> --- common/signal/pubsub/pubsub.go | 3 +- core/format.go | 2 +- infra/vformat/main.go | 39 ++++++++++--------- .../headers/http/linkedreadRequest.go | 3 +- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/common/signal/pubsub/pubsub.go b/common/signal/pubsub/pubsub.go index 8612bb61..cc9b87eb 100644 --- a/common/signal/pubsub/pubsub.go +++ b/common/signal/pubsub/pubsub.go @@ -87,8 +87,7 @@ func (s *Service) Subscribe(name string) *Subscriber { done: done.New(), } s.Lock() - subs := append(s.subs[name], sub) - s.subs[name] = subs + s.subs[name] = append(s.subs[name], sub) s.Unlock() common.Must(s.ctask.Start()) return sub diff --git a/core/format.go b/core/format.go index 99dbff47..de6b7951 100644 --- a/core/format.go +++ b/core/format.go @@ -1,4 +1,4 @@ package core -//go:generate go install -v golang.org/x/tools/cmd/goimports@latest +//go:generate go install -v github.com/daixiang0/gci@latest //go:generate go run ../infra/vformat/main.go -pwd ./.. diff --git a/infra/vformat/main.go b/infra/vformat/main.go index 2ea7af76..4ea72ba6 100644 --- a/infra/vformat/main.go +++ b/infra/vformat/main.go @@ -52,7 +52,7 @@ func GetRuntimeEnv(key string) (string, error) { for _, envItem := range envStrings { envItem = strings.TrimSuffix(envItem, "\r") envKeyValue := strings.Split(envItem, "=") - if strings.EqualFold(strings.TrimSpace(envKeyValue[0]), key) { + if len(envKeyValue) == 2 && strings.TrimSpace(envKeyValue[0]) == key { runtimeEnv = strings.TrimSpace(envKeyValue[1]) } } @@ -79,31 +79,31 @@ func GetGOBIN() string { return GOBIN } -func Run(binary string, args []string) (string, error) { +func Run(binary string, args []string) ([]byte, error) { cmd := exec.Command(binary, args...) cmd.Env = append(cmd.Env, os.Environ()...) output, cmdErr := cmd.CombinedOutput() if cmdErr != nil { - return "", cmdErr + return nil, cmdErr } - if len(output) > 0 { - return string(output), nil - } - return "", nil + return output, nil } func RunMany(binary string, args, files []string) { fmt.Println("Processing...") + + maxTasks := make(chan struct{}, runtime.NumCPU()) for _, file := range files { - args2 := append(args, file) - output, err := Run(binary, args2) - if err != nil { - fmt.Println(err) - continue - } - if len(output) > 0 { - fmt.Println(output) - } + maxTasks <- struct{}{} + go func(file string) { + output, err := Run(binary, append(args, file)) + if err != nil { + fmt.Println(err) + } else if len(output) > 0 { + fmt.Println(string(output)) + } + <-maxTasks + }(file) } } @@ -135,7 +135,7 @@ func main() { suffix = ".exe" } gofmt := "gofmt" + suffix - goimports := "goimports" + suffix + goimports := "gci" + suffix if gofmtPath, err := exec.LookPath(gofmt); err != nil { fmt.Println("Can not find", gofmt, "in system path or current working directory.") @@ -151,7 +151,7 @@ func main() { goimports = goimportsPath } - rawFilesSlice := make([]string, 0) + rawFilesSlice := make([]string, 0, 1000) walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error { if err != nil { fmt.Println(err) @@ -166,7 +166,8 @@ func main() { filename := filepath.Base(path) if strings.HasSuffix(filename, ".go") && !strings.HasSuffix(filename, ".pb.go") && - !strings.Contains(dir, filepath.Join("testing", "mocks")) { + !strings.Contains(dir, filepath.Join("testing", "mocks")) && + !strings.Contains(path, filepath.Join("main", "distro", "all", "all.go")) { rawFilesSlice = append(rawFilesSlice, path) } diff --git a/transport/internet/headers/http/linkedreadRequest.go b/transport/internet/headers/http/linkedreadRequest.go index e39f8373..45773312 100644 --- a/transport/internet/headers/http/linkedreadRequest.go +++ b/transport/internet/headers/http/linkedreadRequest.go @@ -4,7 +4,8 @@ import ( "bufio" "net/http" - _ "unsafe" // required to use //go:linkname + // required to use go:linkname + _ "unsafe" ) //go:linkname readRequest net/http.readRequest