Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>
  • Loading branch information
Daxin Wang committed Dec 30, 2022
1 parent 34c408d commit fe2ce53
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func (fw *fileWriter) rotateFiles(baseDir string) error {
return nil
}
// Remove the older files and remove half of them one time to decrease the frequency
// of deleting files. Note this is different from rotating log files. For log files,
// we could delete one file at a time because the action "rotate" is in a low frequency.
toBeRotated = toBeRotated[:len(toBeRotated)-fw.config.MaxFileCount/2+1]
// Remove the stale files asynchronously
go func() {
Expand All @@ -135,7 +137,8 @@ func (fw *fileWriter) rotateFiles(baseDir string) error {
return nil
}

// getDirEntryInTimeOrder returns the directory entries slice in chronological order
// getDirEntryInTimeOrder returns the directory entries slice in chronological order.
// The result files are sorted based on their modification time.
func getDirEntryInTimeOrder(path string) ([]os.DirEntry, error) {
f, err := os.Open(path)
if err != nil {
Expand All @@ -148,6 +151,9 @@ func getDirEntryInTimeOrder(path string) ([]os.DirEntry, error) {
}
}(f)
dirs, err := f.ReadDir(-1)
// Sort the files based on their modification time. We don't sort them based on the
// timestamp in the file name because they are similar but the later one costs more CPU
// considering that we have to split the file name first.
sort.Slice(dirs, func(i, j int) bool {
fileInfoA, err := dirs[i].Info()
if err != nil {
Expand Down

0 comments on commit fe2ce53

Please sign in to comment.