Skip to content

Commit

Permalink
[draft] feat: migrate to immediate processing
Browse files Browse the repository at this point in the history
  • Loading branch information
shikanime committed Jan 23, 2025
1 parent 45881a4 commit 1de4154
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
13 changes: 9 additions & 4 deletions cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions format/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) erro
}
}

c.scheduler.apply(ctx)

return nil
}

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion format/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1de4154

Please # to comment.