Skip to content

Commit 111672b

Browse files
committed
feat: migrate to immediate processing
1 parent 45881a4 commit 111672b

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

cmd/format/format.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
167167
// start traversing
168168
files := make([]*walk.File, BatchSize)
169169

170+
LOOP:
170171
for {
171172
// read the next batch
172173
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)
180181
return fmt.Errorf("formatting failure: %w", err)
181182
}
182183

183-
if errors.Is(err, io.EOF) {
184+
switch {
185+
case errors.Is(err, io.EOF):
184186
// we have finished traversing
185-
break
186-
} else if err != nil {
187+
break LOOP
188+
case cfg.Watch && errors.Is(err, context.DeadlineExceeded):
189+
// we timed out reading files, try again
190+
continue
191+
case err != nil:
187192
// something went wrong
188193
return fmt.Errorf("failed to read files: %w", err)
189194
}
190195
}
191196

192197
// finalize formatting
193-
formatErr := formatter.Close(ctx)
198+
formatErr := formatter.Close()
194199

195200
// close the walker, ensuring any pending file release hooks finish
196201
if err = walker.Close(); err != nil {

format/composite.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) erro
107107
}
108108
}
109109

110+
c.scheduler.apply(ctx)
111+
110112
return nil
111113
}
112114

@@ -136,8 +138,8 @@ func (c *CompositeFormatter) signature() (signature, error) {
136138

137139
// Close finalizes the processing of the CompositeFormatter, ensuring that any remaining batches are applied and
138140
// all formatters have completed their tasks. It returns an error if any formatting failures were detected.
139-
func (c *CompositeFormatter) Close(ctx context.Context) error {
140-
return c.scheduler.close(ctx)
141+
func (c *CompositeFormatter) Close() error {
142+
return c.scheduler.close()
141143
}
142144

143145
func NewCompositeFormatter(

format/scheduler.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,16 @@ func (s *scheduler) schedule(ctx context.Context, key batchKey, batch []*walk.Fi
203203
})
204204
}
205205

206-
func (s *scheduler) close(ctx context.Context) error {
206+
func (s *scheduler) apply(ctx context.Context) {
207207
// schedule any partial batches that remain
208208
for key, batch := range s.batches {
209209
if len(batch) > 0 {
210210
s.schedule(ctx, key, batch)
211211
}
212212
}
213+
}
213214

215+
func (s *scheduler) close() error {
214216
// wait for processing to complete
215217
if err := s.eg.Wait(); err != nil {
216218
return fmt.Errorf("failed to wait for formatters: %w", err)

0 commit comments

Comments
 (0)