vformat supports multi-core processing (#757)

* Feat: vformat supports multi-core processing (#996)

Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
This commit is contained in:
yuhan6665 2021-10-12 11:29:22 -04:00 committed by GitHub
parent a97d45c93a
commit 3554886ce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 23 deletions

View File

@ -87,8 +87,7 @@ func (s *Service) Subscribe(name string) *Subscriber {
done: done.New(), done: done.New(),
} }
s.Lock() s.Lock()
subs := append(s.subs[name], sub) s.subs[name] = append(s.subs[name], sub)
s.subs[name] = subs
s.Unlock() s.Unlock()
common.Must(s.ctask.Start()) common.Must(s.ctask.Start())
return sub return sub

View File

@ -1,4 +1,4 @@
package core 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 ./.. //go:generate go run ../infra/vformat/main.go -pwd ./..

View File

@ -52,7 +52,7 @@ func GetRuntimeEnv(key string) (string, error) {
for _, envItem := range envStrings { for _, envItem := range envStrings {
envItem = strings.TrimSuffix(envItem, "\r") envItem = strings.TrimSuffix(envItem, "\r")
envKeyValue := strings.Split(envItem, "=") 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]) runtimeEnv = strings.TrimSpace(envKeyValue[1])
} }
} }
@ -79,31 +79,31 @@ func GetGOBIN() string {
return GOBIN return GOBIN
} }
func Run(binary string, args []string) (string, error) { func Run(binary string, args []string) ([]byte, error) {
cmd := exec.Command(binary, args...) cmd := exec.Command(binary, args...)
cmd.Env = append(cmd.Env, os.Environ()...) cmd.Env = append(cmd.Env, os.Environ()...)
output, cmdErr := cmd.CombinedOutput() output, cmdErr := cmd.CombinedOutput()
if cmdErr != nil { if cmdErr != nil {
return "", cmdErr return nil, cmdErr
} }
if len(output) > 0 { return output, nil
return string(output), nil
}
return "", nil
} }
func RunMany(binary string, args, files []string) { func RunMany(binary string, args, files []string) {
fmt.Println("Processing...") fmt.Println("Processing...")
maxTasks := make(chan struct{}, runtime.NumCPU())
for _, file := range files { for _, file := range files {
args2 := append(args, file) maxTasks <- struct{}{}
output, err := Run(binary, args2) go func(file string) {
if err != nil { output, err := Run(binary, append(args, file))
fmt.Println(err) if err != nil {
continue fmt.Println(err)
} } else if len(output) > 0 {
if len(output) > 0 { fmt.Println(string(output))
fmt.Println(output) }
} <-maxTasks
}(file)
} }
} }
@ -135,7 +135,7 @@ func main() {
suffix = ".exe" suffix = ".exe"
} }
gofmt := "gofmt" + suffix gofmt := "gofmt" + suffix
goimports := "goimports" + suffix goimports := "gci" + suffix
if gofmtPath, err := exec.LookPath(gofmt); err != nil { if gofmtPath, err := exec.LookPath(gofmt); err != nil {
fmt.Println("Can not find", gofmt, "in system path or current working directory.") fmt.Println("Can not find", gofmt, "in system path or current working directory.")
@ -151,7 +151,7 @@ func main() {
goimports = goimportsPath goimports = goimportsPath
} }
rawFilesSlice := make([]string, 0) rawFilesSlice := make([]string, 0, 1000)
walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error { walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -166,7 +166,8 @@ func main() {
filename := filepath.Base(path) filename := filepath.Base(path)
if strings.HasSuffix(filename, ".go") && if strings.HasSuffix(filename, ".go") &&
!strings.HasSuffix(filename, ".pb.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) rawFilesSlice = append(rawFilesSlice, path)
} }

View File

@ -4,7 +4,8 @@ import (
"bufio" "bufio"
"net/http" "net/http"
_ "unsafe" // required to use //go:linkname // required to use go:linkname
_ "unsafe"
) )
//go:linkname readRequest net/http.readRequest //go:linkname readRequest net/http.readRequest