-
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 all 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
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 |
---|---|---|
|
@@ -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
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 |
---|---|---|
|
@@ -7,25 +7,40 @@ import ( | |
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// Represent the tasks that can be sent to the pool. | ||
type Task[R any] func() (result R, err error) | ||
|
||
// The result generated from a task. | ||
type Result[R any] struct { | ||
Value R | ||
Error error | ||
} | ||
|
||
// Pool is a generic pool of workers. | ||
type Pool[R any] struct { | ||
queueCh chan Task[R] | ||
resultsCh chan R | ||
errorsCh chan error | ||
// The queue where tasks are submitted. | ||
queueCh chan Task[R] | ||
|
||
// Where workers send the results after a task is executed, | ||
// the collector then reads them and aggregate them. | ||
resultsCh chan Result[R] | ||
|
||
// The total number of requests sent. | ||
requestsSent int64 | ||
once sync.Once | ||
|
||
workers []*worker[R] | ||
// Number of workers to spawn. | ||
workerCount int | ||
|
||
// The list of workers that are listening to the queue. | ||
workers []*worker[R] | ||
|
||
// A single collector to aggregate results. | ||
collector[R] | ||
|
||
// used to protect starting the pool multiple times | ||
once sync.Once | ||
} | ||
|
||
// New initializes a new Pool with the provided number of workers. The pool is generic and can | ||
|
@@ -37,14 +52,12 @@ type Pool[R any] struct { | |
// return 42, nil | ||
// } | ||
func New[R any](count int) *Pool[R] { | ||
resultsCh := make(chan R) | ||
errorsCh := make(chan error) | ||
resultsCh := make(chan Result[R]) | ||
return &Pool[R]{ | ||
queueCh: make(chan Task[R]), | ||
resultsCh: resultsCh, | ||
errorsCh: errorsCh, | ||
workerCount: count, | ||
collector: collector[R]{resultsCh: resultsCh, errorsCh: errorsCh}, | ||
collector: collector[R]{resultsCh: resultsCh}, | ||
} | ||
} | ||
|
||
|
@@ -56,7 +69,7 @@ func New[R any](count int) *Pool[R] { | |
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 := worker[R]{id: i, queueCh: p.queueCh, resultsCh: p.resultsCh} | ||
w.start() | ||
p.workers = append(p.workers, &w) | ||
} | ||
|
@@ -67,22 +80,33 @@ func (p *Pool[R]) 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()...) | ||
if t != nil { | ||
p.queueCh <- t | ||
atomic.AddInt64(&p.requestsSent, 1) | ||
} | ||
} | ||
|
||
// GetResults returns the tasks results. | ||
// | ||
// It is recommended to call `Wait()` before reading the results. | ||
func (p *Pool[R]) GetResults() []R { | ||
func (p *Pool[R]) GetResults() []Result[R] { | ||
return p.collector.GetResults() | ||
} | ||
|
||
// GetValues returns only the values of the pool results | ||
// | ||
// It is recommended to call `Wait()` before reading the results. | ||
func (p *Pool[R]) GetValues() []R { | ||
return p.collector.GetValues() | ||
} | ||
|
||
// GetErrors returns only the errors of the pool results | ||
// | ||
// It is recommended to call `Wait()` before reading the results. | ||
func (p *Pool[R]) GettErrors() []error { | ||
return p.collector.GetErrors() | ||
} | ||
|
||
// 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() { | ||
|
@@ -92,7 +116,7 @@ func (p *Pool[R]) Close() { | |
|
||
// Wait waits until all tasks have been processed. | ||
func (p *Pool[R]) Wait() { | ||
ticker := time.NewTicker(100 * time.Millisecond) | ||
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 | ||
|
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.