Skip to content

Commit

Permalink
Add --workers flag
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jan 28, 2022
1 parent 8ea66c9 commit f2481e9
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dockers:
- internal
- go.mod
- go.sum
- Makefile
- image_templates:
["ghcr.io/cloudflare/{{ .ProjectName }}:{{ .Version }}-arm64"]
goarch: arm64
Expand All @@ -78,6 +79,7 @@ dockers:
- internal
- go.mod
- go.sum
- Makefile
docker_manifests:
- name_template: ghcr.io/cloudflare/{{ .ProjectName }}:{{ .Version }}
image_templates:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.7.3

### Added

- `--workers` flag to control the number of worker threads for running checks.

## v0.7.2

### Changed
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ test:
-timeout=5m \
./...

.PHONY: debug-testscript
debug-testscript:
for I in ./cmd/pint/tests/*.txt ; do T=`basename "$${I}" | cut -d. -f1`; echo ">>> $${T}" ; go test -count=1 -timeout=30s -v -run=TestScript/$${T} ./cmd/pint ; done

.PHONY: cover
cover: test
go tool cover -func=$(COVER_PROFILE)
Expand Down
7 changes: 6 additions & 1 deletion cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func actionCI(c *cli.Context) (err error) {
return fmt.Errorf("failed to set log level: %w", err)
}

workers := c.Int(workersFlag)
if workers < 1 {
return fmt.Errorf("--%s flag must be > 0", workersFlag)
}

cfg, err := config.Load(c.Path(configFlag), c.IsSet(configFlag))
if err != nil {
return fmt.Errorf("failed to load config file %q: %w", c.Path(configFlag), err)
Expand Down Expand Up @@ -71,7 +76,7 @@ func actionCI(c *cli.Context) (err error) {

gitBlame := discovery.NewGitBlameLineFinder(git.RunGit, toScan.Commits())
ctx := context.WithValue(context.Background(), config.CommandKey, config.CICommand)
summary := scanFiles(ctx, cfg, toScan, gitBlame)
summary := scanFiles(ctx, workers, cfg, toScan, gitBlame)

reps := []reporter.Reporter{
reporter.NewConsoleReporter(os.Stderr),
Expand Down
7 changes: 6 additions & 1 deletion cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func actionLint(c *cli.Context) (err error) {
return fmt.Errorf("failed to set log level: %w", err)
}

workers := c.Int(workersFlag)
if workers < 1 {
return fmt.Errorf("--%s flag must be > 0", workersFlag)
}

paths := c.Args().Slice()
if len(paths) == 0 {
return fmt.Errorf("at least one file or directory required")
Expand All @@ -51,7 +56,7 @@ func actionLint(c *cli.Context) (err error) {
}

ctx := context.WithValue(context.Background(), config.CommandKey, config.LintCommand)
summary := scanFiles(ctx, cfg, toScan, &discovery.NoopLineFinder{})
summary := scanFiles(ctx, workers, cfg, toScan, &discovery.NoopLineFinder{})

r := reporter.NewConsoleReporter(os.Stderr)
err = r.Submit(summary)
Expand Down
7 changes: 7 additions & 0 deletions cmd/pint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
disabledFlag = "disabled"
offlineFlag = "offline"
noColorFlag = "no-color"
workersFlag = "workers"
)

var (
Expand All @@ -33,6 +34,12 @@ func newApp() *cli.App {
Value: ".pint.hcl",
Usage: "Configuration file to use",
},
&cli.IntFlag{
Name: workersFlag,
Aliases: []string{"w"},
Value: 10,
Usage: "Number of worker threads for running checks",
},
&cli.StringFlag{
Name: logLevelFlag,
Aliases: []string{"l"},
Expand Down
9 changes: 4 additions & 5 deletions cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func scanProblem(path string, err error) reporter.Report {
}
}

func scanFiles(ctx context.Context, cfg config.Config, fcs discovery.FileFindResults, ld discovery.LineFinder) (summary reporter.Summary) {
func scanFiles(ctx context.Context, workers int, cfg config.Config, fcs discovery.FileFindResults, ld discovery.LineFinder) (summary reporter.Summary) {
summary.FileChanges = fcs

scanJobs := []scanJob{}
Expand Down Expand Up @@ -119,12 +119,11 @@ func scanFiles(ctx context.Context, cfg config.Config, fcs discovery.FileFindRes
}
}

jobs := make(chan scanJob, 100)
results := make(chan reporter.Report, 100)
jobs := make(chan scanJob, workers*5)
results := make(chan reporter.Report, workers*5)
wg := sync.WaitGroup{}

// run 10 workers
for w := 1; w <= 10; w++ {
for w := 1; w <= workers; w++ {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down
15 changes: 10 additions & 5 deletions cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func actionWatch(c *cli.Context) (err error) {
return fmt.Errorf("failed to set log level: %w", err)
}

workers := c.Int(workersFlag)
if workers < 1 {
return fmt.Errorf("--%s flag must be > 0", workersFlag)
}

paths := c.Args().Slice()
if len(paths) == 0 {
return fmt.Errorf("at least one file or directory required")
Expand Down Expand Up @@ -118,7 +123,7 @@ func actionWatch(c *cli.Context) (err error) {
// start timer to run every $interval
interval := c.Duration(intervalFlag)
ack := make(chan bool, 1)
stop := startTimer(mainCtx, cfg, interval, ack, collector)
stop := startTimer(mainCtx, cfg, workers, interval, ack, collector)

quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
Expand All @@ -140,7 +145,7 @@ func actionWatch(c *cli.Context) (err error) {
return nil
}

func startTimer(ctx context.Context, cfg config.Config, interval time.Duration, ack chan bool, collector *problemCollector) chan bool {
func startTimer(ctx context.Context, cfg config.Config, workers int, interval time.Duration, ack chan bool, collector *problemCollector) chan bool {
ticker := time.NewTicker(time.Second)
stop := make(chan bool, 1)
wasBootstrapped := false
Expand All @@ -156,7 +161,7 @@ func startTimer(ctx context.Context, cfg config.Config, interval time.Duration,
ticker.Reset(interval)
wasBootstrapped = true
}
if err := collector.scan(ctx); err != nil {
if err := collector.scan(ctx, workers); err != nil {
log.Error().Err(err).Msg("Got an error when running checks")
}
checkIterationsTotal.Inc()
Expand Down Expand Up @@ -194,7 +199,7 @@ func newProblemCollector(cfg config.Config, paths []string) *problemCollector {
}
}

func (c *problemCollector) scan(ctx context.Context) error {
func (c *problemCollector) scan(ctx context.Context, workers int) error {
d := discovery.NewGlobFileFinder()
toScan, err := d.Find(c.paths...)
if err != nil {
Expand All @@ -205,7 +210,7 @@ func (c *problemCollector) scan(ctx context.Context) error {
return fmt.Errorf("no matching files")
}

s := scanFiles(ctx, c.cfg, toScan, &discovery.NoopLineFinder{})
s := scanFiles(ctx, workers, c.cfg, toScan, &discovery.NoopLineFinder{})

c.lock.Lock()
c.summary = &s
Expand Down

0 comments on commit f2481e9

Please sign in to comment.