-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add concurrency to mdformatter
#63
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,11 @@ func (v GitHubValidator) IsValid(k futureKey, r *validator) (bool, error) { | |
// RoundTripValidator.IsValid returns true if url is checked by colly. | ||
func (v RoundTripValidator) IsValid(k futureKey, r *validator) (bool, error) { | ||
// Result will be in future. | ||
r.destFutures[k].resultFn = func() error { return r.remoteLinks[k.dest] } | ||
prevResult, _ := r.destFutures.LoadAndDelete(k) | ||
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. Lot's of races when we delete, unfortunately.. we need to make this operation locked potentially with Mutex. But it has to be the full operation Locked:
We might need to check |
||
newResult := prevResult.(*futureResult) | ||
newResult.resultFn = func() error { return r.remoteLinks[k.dest] } | ||
r.destFutures.Store(k, newResult) | ||
|
||
r.rMu.RLock() | ||
if _, ok := r.remoteLinks[k.dest]; ok { | ||
r.rMu.RUnlock() | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |||||
"io/ioutil" | ||||||
"os" | ||||||
"sort" | ||||||
"sync" | ||||||
"time" | ||||||
|
||||||
"github.com/Kunde21/markdownfmt/v2/markdown" | ||||||
|
@@ -217,7 +218,7 @@ func newSpinner(suffix string) (*yacspin.Spinner, error) { | |||||
|
||||||
// Format formats given markdown files in-place. IsFormatted `With...` function to see what modifiers you can add. | ||||||
func Format(ctx context.Context, logger log.Logger, files []string, opts ...Option) error { | ||||||
spin, err := newSpinner(" Formatting: ") | ||||||
spin, err := newSpinner(" Formatting... ") | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -228,7 +229,7 @@ func Format(ctx context.Context, logger log.Logger, files []string, opts ...Opti | |||||
// If diff is empty it means all files are formatted. | ||||||
func IsFormatted(ctx context.Context, logger log.Logger, files []string, opts ...Option) (diffs Diffs, err error) { | ||||||
d := Diffs{} | ||||||
spin, err := newSpinner(" Checking: ") | ||||||
spin, err := newSpinner(" Checking... ") | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
@@ -240,57 +241,73 @@ func IsFormatted(ctx context.Context, logger log.Logger, files []string, opts .. | |||||
|
||||||
func format(ctx context.Context, logger log.Logger, files []string, diffs *Diffs, spin *yacspin.Spinner, opts ...Option) error { | ||||||
f := New(ctx, opts...) | ||||||
b := bytes.Buffer{} | ||||||
// TODO(bwplotka): Add concurrency (collector will need to redone). | ||||||
errorChannel := make(chan error) | ||||||
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.
Suggested change
|
||||||
var wg sync.WaitGroup | ||||||
|
||||||
errs := merrors.New() | ||||||
if spin != nil { | ||||||
errs.Add(spin.Start()) | ||||||
} | ||||||
|
||||||
wg.Add(len(files)) | ||||||
|
||||||
go func() { | ||||||
wg.Wait() | ||||||
close(errorChannel) | ||||||
}() | ||||||
|
||||||
for _, fn := range files { | ||||||
select { | ||||||
case <-ctx.Done(): | ||||||
return ctx.Err() | ||||||
default: | ||||||
} | ||||||
if spin != nil { | ||||||
spin.Message(fn + "...") | ||||||
} | ||||||
errs.Add(func() error { | ||||||
go func(fn string) { | ||||||
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. Let's use https://pkg.go.dev/golang.org/x/sync/errgroup maybe, so semantics are easier. |
||||||
defer wg.Done() | ||||||
b := bytes.Buffer{} | ||||||
|
||||||
file, err := os.OpenFile(fn, os.O_RDWR, 0) | ||||||
if err != nil { | ||||||
return errors.Wrapf(err, "open %v", fn) | ||||||
errorChannel <- errors.Wrapf(err, "open %v", fn) | ||||||
return | ||||||
} | ||||||
defer logerrcapture.ExhaustClose(logger, file, "close file %v", fn) | ||||||
|
||||||
b.Reset() | ||||||
if err := f.Format(file, &b); err != nil { | ||||||
return err | ||||||
errorChannel <- err | ||||||
return | ||||||
} | ||||||
|
||||||
if diffs != nil { | ||||||
if _, err := file.Seek(0, 0); err != nil { | ||||||
return err | ||||||
errorChannel <- err | ||||||
return | ||||||
} | ||||||
|
||||||
in, err := ioutil.ReadAll(file) | ||||||
if err != nil { | ||||||
return errors.Wrapf(err, "read all %v", fn) | ||||||
errorChannel <- errors.Wrapf(err, "read all %v", fn) | ||||||
return | ||||||
} | ||||||
|
||||||
if !bytes.Equal(in, b.Bytes()) { | ||||||
*diffs = append(*diffs, gitdiff.CompareBytes(in, fn, b.Bytes(), fn+" (formatted)")) | ||||||
} | ||||||
return nil | ||||||
return | ||||||
} | ||||||
|
||||||
n, err := file.WriteAt(b.Bytes(), 0) | ||||||
if err != nil { | ||||||
return errors.Wrapf(err, "write %v", fn) | ||||||
errorChannel <- errors.Wrapf(err, "write %v", fn) | ||||||
return | ||||||
} | ||||||
if err := file.Truncate(int64(n)); err != nil { | ||||||
errorChannel <- err | ||||||
return | ||||||
} | ||||||
return file.Truncate(int64(n)) | ||||||
}()) | ||||||
}(fn) | ||||||
} | ||||||
|
||||||
for err := range errorChannel { | ||||||
errs.Add(err) | ||||||
} | ||||||
|
||||||
if spin != nil { | ||||||
errs.Add(spin.Stop()) | ||||||
} | ||||||
|
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.
I think there is a race during replacement operation.