Skip to content

Commit

Permalink
move fileutil.readDirWithSuffix to normal function
Browse files Browse the repository at this point in the history
Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Jun 30, 2024
1 parent 854010f commit c26156d
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions client/pkg/fileutil/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,12 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval
zap.Uint("max", max),
zap.Duration("interval", interval))

readDirWithSuffix := func(dirname string) ([]string, error) {
fnames, err := ReadDir(dirname)
if err != nil {
return nil, err
}
// filter in place (ref. https://go.dev/wiki/SliceTricks#filtering-without-allocating)
fnamesWithSuffix := fnames[:0]
for _, fname := range fnames {
if strings.HasSuffix(fname, suffix) {
fnamesWithSuffix = append(fnamesWithSuffix, fname)
}
}

return fnamesWithSuffix, nil
}

go func() {
if donec != nil {
defer close(donec)
}
for {
fnamesWithSuffix, err := readDirWithSuffix(dirname)
fnamesWithSuffix, err := readDirWithSuffix(dirname, suffix)
if err != nil {
errC <- err
return
Expand Down Expand Up @@ -119,3 +103,18 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval
}()
return errC
}

func readDirWithSuffix(dirname string, suffix string) ([]string, error) {
fnames, err := ReadDir(dirname)
if err != nil {
return nil, err
}
// filter in place (ref. https://go.dev/wiki/SliceTricks#filtering-without-allocating)
fnamesWithSuffix := fnames[:0]
for _, fname := range fnames {
if strings.HasSuffix(fname, suffix) {
fnamesWithSuffix = append(fnamesWithSuffix, fname)
}
}
return fnamesWithSuffix, nil
}

0 comments on commit c26156d

Please sign in to comment.