in benchmarks/stat_files/main.go [50:113]
func createFiles(
dir string,
numFiles int) (files []*os.File, err error) {
group, ctx := errgroup.WithContext(context.Background())
// Create files in parallel, and write them to a channel.
const parallelism = 128
var counter uint64
fileChan := make(chan *os.File)
var wg sync.WaitGroup
for i := 0; i < parallelism; i++ {
wg.Add(1)
group.Go(func() (err error) {
defer wg.Done()
for {
// Should we create another?
count := atomic.AddUint64(&counter, 1)
if count > uint64(numFiles) {
return
}
// Create it.
var f *os.File
f, err = fsutil.AnonymousFile(dir)
if err != nil {
err = fmt.Errorf("AnonymousFile: %w", err)
return
}
// Write it to the channel.
select {
case fileChan <- f:
case <-ctx.Done():
err = ctx.Err()
return
}
}
})
}
go func() {
wg.Wait()
close(fileChan)
}()
// Accumulate into the slice.
group.Go(func() (err error) {
for f := range fileChan {
files = append(files, f)
}
return
})
err = group.Wait()
if err != nil {
closeAll(files)
files = nil
}
return
}