From 111672bbade8f6d3b7411df12c8cf8f280ce1f68 Mon Sep 17 00:00:00 2001 From: William Phetsinorath Date: Thu, 23 Jan 2025 13:18:13 +0000 Subject: [PATCH] feat: migrate to immediate processing --- cmd/format/format.go | 13 +++++++++---- format/composite.go | 6 ++++-- format/scheduler.go | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cmd/format/format.go b/cmd/format/format.go index 041a28de..80e30b3e 100644 --- a/cmd/format/format.go +++ b/cmd/format/format.go @@ -167,6 +167,7 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string) // start traversing files := make([]*walk.File, BatchSize) +LOOP: for { // read the next batch readCtx, cancel := context.WithTimeout(ctx, 1*time.Second) @@ -180,17 +181,21 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string) return fmt.Errorf("formatting failure: %w", err) } - if errors.Is(err, io.EOF) { + switch { + case errors.Is(err, io.EOF): // we have finished traversing - break - } else if err != nil { + break LOOP + case cfg.Watch && errors.Is(err, context.DeadlineExceeded): + // we timed out reading files, try again + continue + case err != nil: // something went wrong return fmt.Errorf("failed to read files: %w", err) } } // finalize formatting - formatErr := formatter.Close(ctx) + formatErr := formatter.Close() // close the walker, ensuring any pending file release hooks finish if err = walker.Close(); err != nil { diff --git a/format/composite.go b/format/composite.go index d5ac4558..5556b30b 100644 --- a/format/composite.go +++ b/format/composite.go @@ -107,6 +107,8 @@ func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) erro } } + c.scheduler.apply(ctx) + return nil } @@ -136,8 +138,8 @@ func (c *CompositeFormatter) signature() (signature, error) { // Close finalizes the processing of the CompositeFormatter, ensuring that any remaining batches are applied and // all formatters have completed their tasks. It returns an error if any formatting failures were detected. -func (c *CompositeFormatter) Close(ctx context.Context) error { - return c.scheduler.close(ctx) +func (c *CompositeFormatter) Close() error { + return c.scheduler.close() } func NewCompositeFormatter( diff --git a/format/scheduler.go b/format/scheduler.go index beac3292..3891819a 100644 --- a/format/scheduler.go +++ b/format/scheduler.go @@ -203,14 +203,16 @@ func (s *scheduler) schedule(ctx context.Context, key batchKey, batch []*walk.Fi }) } -func (s *scheduler) close(ctx context.Context) error { +func (s *scheduler) apply(ctx context.Context) { // schedule any partial batches that remain for key, batch := range s.batches { if len(batch) > 0 { s.schedule(ctx, key, batch) } } +} +func (s *scheduler) close() error { // wait for processing to complete if err := s.eg.Wait(); err != nil { return fmt.Errorf("failed to wait for formatters: %w", err)