Skip to content

Commit 18d8884

Browse files
committed
perf: fetch open issues while looking through the fs
1 parent d7dd221 commit 18d8884

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

cmd/root.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/alexdor/issue-syncer/storer"
1818
"github.com/lmittmann/tint"
1919
"github.com/spf13/cobra"
20+
"golang.org/x/sync/errgroup"
2021
)
2122

2223
var (
@@ -75,25 +76,43 @@ var rootCmd = &cobra.Command{
7576
cob.SetContext(context.WithValue(cob.Context(), storer.DryRunKey, true))
7677
}
7778

78-
for i := range WordsToLookFor {
79-
WordsToLookFor[i] = strings.ToLower(WordsToLookFor[i])
80-
}
81-
comments, err := parser.ParseDirectory(Path, WordsToLookFor, DirsToSkip, UseGitIgnore)
82-
if err != nil {
83-
return fmt.Errorf("failed to parse directory: %w", err)
84-
}
79+
var (
80+
comments []parser.Comment
81+
currentIssues map[string]storer.Issue
82+
)
8583

86-
err = storerToUse.Init(cob.Context())
87-
if err != nil {
88-
return fmt.Errorf("failed to create storer: %w", err)
89-
}
84+
ctx := cob.Context()
85+
g, gctx := errgroup.WithContext(ctx)
86+
87+
g.Go(func() error {
88+
if err := storerToUse.Init(ctx); err != nil {
89+
return fmt.Errorf("failed to initialize storer: %w", err)
90+
}
91+
var err error
92+
currentIssues, err = storerToUse.FetchCurrentOpenIssues(gctx)
93+
if err != nil {
94+
return fmt.Errorf("failed to fetch current open issues: %w", err)
95+
}
96+
return nil
97+
})
98+
99+
g.Go(func() error {
100+
for i := range WordsToLookFor {
101+
WordsToLookFor[i] = strings.ToLower(WordsToLookFor[i])
102+
}
103+
var err error
104+
comments, err = parser.ParseDirectory(gctx, Path, WordsToLookFor, DirsToSkip, UseGitIgnore)
105+
if err != nil {
106+
return fmt.Errorf("failed to parse directory: %w", err)
107+
}
108+
return nil
109+
})
90110

91-
currentIssues, err := storerToUse.FetchCurrentOpenIssues(cob.Context())
92-
if err != nil {
93-
return fmt.Errorf("failed to fetch current open issues: %w", err)
111+
if err := g.Wait(); err != nil {
112+
return err
94113
}
95114

96-
return storer.UpdateIssues(cob.Context(), storerToUse, currentIssues, comments)
115+
return storer.UpdateIssues(ctx, storerToUse, currentIssues, comments)
97116
},
98117
}
99118

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/lmittmann/tint v1.0.7
88
github.com/spf13/cobra v1.9.1
99
golang.org/x/oauth2 v0.29.0
10+
golang.org/x/sync v0.13.0
1011
)
1112

1213
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
1717
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1818
golang.org/x/oauth2 v0.29.0 h1:WdYw2tdTK1S8olAzWHdgeqfy+Mtm9XNhv/xJsY65d98=
1919
golang.org/x/oauth2 v0.29.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
20+
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
21+
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
2022
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2123
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2224
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

parser/parser.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parser
22

33
import (
44
"bufio"
5+
"context"
56
"errors"
67
"fmt"
78
"log/slog"
@@ -141,13 +142,15 @@ func parseFile(filePath string, marker CommentMarker, wordsToLookFor []string) (
141142
}
142143

143144
// ParseDirectory walks through a directory and finds all comments
144-
func ParseDirectory(rootPath string, wordsToLookFor []string, pathsToSkip []string, useGitIgnore bool) ([]Comment, error) {
145+
func ParseDirectory(ctx context.Context, rootPath string, wordsToLookFor, pathsToSkip []string, useGitIgnore bool) ([]Comment, error) {
145146
var comments []Comment
146-
147147
err := filepath.WalkDir(rootPath, func(path string, d os.DirEntry, err error) error {
148148
if err != nil {
149149
return err
150150
}
151+
if ctx.Err() != nil {
152+
return ctx.Err()
153+
}
151154

152155
if slices.Contains(pathsToSkip, d.Name()) {
153156
return filepath.SkipDir

0 commit comments

Comments
 (0)