-
Notifications
You must be signed in to change notification settings - Fork 23
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
⚡ discover assets in parallel #4973
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
42f0627
:zap: workerpool package to submit parallel requests
afiune 5ae3d75
:zap: fetch org repositories in parallel
afiune 93ca08d
⚙️ add a collector to the workerpool
afiune 4765ace
:rotating_light: fix race conditions
afiune 8c98904
:zap: discover assets in parallel
afiune b3dc6d8
🧪 decrease workerpool wait ticker to 10ms
afiune 17f7beb
🐛 make `DiscoveredAssets.AddError()` thread safe
afiune 842e1e7
:thread: add mutex when running `provider.connect()`
afiune e785c76
Merge branch 'main' of github.com:mondoohq/cnquery into afiune/parall…
afiune e3a58fd
⚙️ reduce the workerpool Task function
afiune 4ffb815
⚙️ return `pool.Result` as a combined struct
afiune 842f60e
🏎️ fix more data race conditions
afiune 8badda7
🤖 Run race detector on CI
afiune ceb2585
⚙️ split plugin connect func and assignation
afiune File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,6 +198,18 @@ | |
"shell", "ssh", "[email protected]", | ||
], | ||
}, | ||
{ | ||
"name": "scan github org", | ||
"type": "go", | ||
"request": "launch", | ||
"program": "${workspaceRoot}/apps/cnquery/cnquery.go", | ||
"args": [ | ||
"scan", | ||
"github", | ||
"org", "hit-training", | ||
"--log-level", "trace" | ||
] | ||
}, | ||
{ | ||
"name": "Configure Built-in Providers", | ||
"type": "go", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package workerpool | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type collector[R any] struct { | ||
resultsCh <-chan R | ||
results []R | ||
read sync.Mutex | ||
|
||
errorsCh <-chan error | ||
errors []error | ||
|
||
requestsRead int64 | ||
} | ||
|
||
func (c *collector[R]) start() { | ||
go func() { | ||
for { | ||
select { | ||
case result := <-c.resultsCh: | ||
c.read.Lock() | ||
c.results = append(c.results, result) | ||
c.read.Unlock() | ||
|
||
case err := <-c.errorsCh: | ||
c.read.Lock() | ||
c.errors = append(c.errors, err) | ||
c.read.Unlock() | ||
} | ||
|
||
atomic.AddInt64(&c.requestsRead, 1) | ||
} | ||
}() | ||
} | ||
func (c *collector[R]) GetResults() []R { | ||
c.read.Lock() | ||
defer c.read.Unlock() | ||
return c.results | ||
} | ||
|
||
func (c *collector[R]) GetErrors() []error { | ||
c.read.Lock() | ||
defer c.read.Unlock() | ||
return c.errors | ||
} | ||
|
||
func (c *collector[R]) RequestsRead() int64 { | ||
return atomic.LoadInt64(&c.requestsRead) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package workerpool | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
type Task[R any] func() (result R, err error) | ||
|
||
// Pool is a generic pool of workers. | ||
type Pool[R any] struct { | ||
queueCh chan Task[R] | ||
resultsCh chan R | ||
errorsCh chan error | ||
|
||
requestsSent int64 | ||
once sync.Once | ||
|
||
workers []*worker[R] | ||
workerCount int | ||
|
||
collector[R] | ||
} | ||
|
||
// New initializes a new Pool with the provided number of workers. The pool is generic and can | ||
// accept any type of Task that returns the signature `func() (R, error)`. | ||
// | ||
// For example, a Pool[int] will accept Tasks similar to: | ||
// | ||
// task := func() (int, error) { | ||
// return 42, nil | ||
// } | ||
func New[R any](count int) *Pool[R] { | ||
resultsCh := make(chan R) | ||
errorsCh := make(chan error) | ||
return &Pool[R]{ | ||
queueCh: make(chan Task[R]), | ||
resultsCh: resultsCh, | ||
errorsCh: errorsCh, | ||
workerCount: count, | ||
collector: collector[R]{resultsCh: resultsCh, errorsCh: errorsCh}, | ||
} | ||
} | ||
|
||
// Start the pool workers and collector. Make sure call `Close()` to clear the pool. | ||
// | ||
// pool := workerpool.New[int](10) | ||
// pool.Start() | ||
// defer pool.Close() | ||
func (p *Pool[R]) Start() { | ||
p.once.Do(func() { | ||
for i := 0; i < p.workerCount; i++ { | ||
w := worker[R]{id: i, queueCh: p.queueCh, resultsCh: p.resultsCh, errorsCh: p.errorsCh} | ||
w.start() | ||
p.workers = append(p.workers, &w) | ||
} | ||
|
||
p.collector.start() | ||
}) | ||
} | ||
|
||
// Submit sends a task to the workers | ||
func (p *Pool[R]) Submit(t Task[R]) { | ||
p.queueCh <- t | ||
atomic.AddInt64(&p.requestsSent, 1) | ||
} | ||
|
||
// GetErrors returns any error from a processed task | ||
func (p *Pool[R]) GetErrors() error { | ||
return errors.Join(p.collector.GetErrors()...) | ||
} | ||
|
||
// GetResults returns the tasks results. | ||
// | ||
// It is recommended to call `Wait()` before reading the results. | ||
func (p *Pool[R]) GetResults() []R { | ||
return p.collector.GetResults() | ||
} | ||
|
||
// Close waits for workers and collector to process all the requests, and then closes | ||
// the task queue channel. After closing the pool, calling `Submit()` will panic. | ||
func (p *Pool[R]) Close() { | ||
p.Wait() | ||
close(p.queueCh) | ||
} | ||
|
||
// Wait waits until all tasks have been processed. | ||
func (p *Pool[R]) Wait() { | ||
ticker := time.NewTicker(10 * time.Millisecond) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixed the benchmark failure. |
||
for { | ||
if !p.Processing() { | ||
return | ||
} | ||
<-ticker.C | ||
} | ||
} | ||
|
||
// PendingRequests returns the number of pending requests. | ||
func (p *Pool[R]) PendingRequests() int64 { | ||
return atomic.LoadInt64(&p.requestsSent) - p.collector.RequestsRead() | ||
} | ||
|
||
// Processing return true if tasks are being processed. | ||
func (p *Pool[R]) Processing() bool { | ||
return p.PendingRequests() != 0 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you mean to keep this in here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I think it is useful for folks testing, specially now that we have https://github.com/hit-training.
I can remove it.