Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More idiomatic Go concurrency #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions goprocess/gp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package goprocess

import (
"os"
"sync"

goversion "rsc.io/goversion/version"

Expand Down Expand Up @@ -39,51 +38,36 @@ func FindAll() []P {
type isGoFunc func(ps.Process) (path, version string, agent, ok bool, err error)

func findAll(pss []ps.Process, isGo isGoFunc, concurrencyLimit int) []P {
input := make(chan ps.Process, len(pss))
output := make(chan P, len(pss))

for _, ps := range pss {
input <- ps
}
close(input)

var wg sync.WaitGroup
wg.Add(concurrencyLimit) // used to wait for workers to be finished

// Run concurrencyLimit of workers until there
// is no more processes to be checked in the input channel.
for i := 0; i < concurrencyLimit; i++ {
output := make(chan []P, 1)
output <- nil
// Using buffered channel as a semaphore to limit throughput.
// See https://golang.org/doc/effective_go.html#channels
type token struct{}
sem := make(chan token, concurrencyLimit)
for _, pr := range pss {
sem <- token{}
pr := pr
go func() {
defer wg.Done()

for pr := range input {
path, version, agent, ok, err := isGo(pr)
if err != nil {
// TODO(jbd): Return a list of errors.
continue
}
if !ok {
continue
}
output <- P{
defer func() { <-sem }()
if path, version, agent, ok, err := isGo(pr); err != nil {
// TODO(jbd): Return a list of errors.
} else if ok {
output <- append(<-output, P{
PID: pr.Pid(),
PPID: pr.PPid(),
Exec: pr.Executable(),
Path: path,
BuildVersion: version,
Agent: agent,
}
})
}
}()
}
wg.Wait() // wait until all workers are finished
close(output) // no more results to be waited for

var results []P
for p := range output {
results = append(results, p)
// Acquire all semaphore slots to wait for work to complete.
for n := cap(sem); n > 0; n-- {
sem <- token{}
}
return results
return <-output
}

// Find finds info about the process identified with the given PID.
Expand Down