Skip to content

Commit f2a2553

Browse files
committed
rotating.File.MinAge
1 parent 5e89d00 commit f2a2553

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

ext/tlflag/flag.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ func openwfile(u *url.URL) (interface{}, error) {
252252
}
253253
}
254254

255+
if v := q.Get("rotating_max_total_files"); v != "" {
256+
x, err := strconv.Atoi(v)
257+
if err == nil {
258+
f.MaxTotalFiles = x
259+
}
260+
}
261+
262+
if v := q.Get("rotating_min_age"); v != "" {
263+
x, err := time.ParseDuration(v)
264+
if err == nil {
265+
f.MinAge = x
266+
}
267+
}
268+
255269
return f, nil
256270
}
257271

rotating/file.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type (
3131
MaxFileSize int64
3232
MaxFileAge time.Duration
3333

34+
MinAge time.Duration
35+
3436
MaxTotalSize int64
3537
MaxTotalAge time.Duration
3638
MaxTotalFiles int // including current
@@ -65,20 +67,26 @@ const (
6567
TB = 1e12
6668
)
6769

68-
var (
69-
// SubstPattern = "XXXX"
70-
// TimeFormat = "2006-01-02_15-04"
70+
// SubstPattern = "XXXX"
71+
// TimeFormat = "2006-01-02_15-04"
7172

72-
patterns = []string{"xxx", "XXX", "ddd"}
73-
)
73+
var patterns = []string{"xxx", "XXX", "ddd"}
7474

7575
func Create(name string) (f *File) {
76+
const day = 24 * time.Hour
77+
7678
f = &File{
7779
name: name,
7880

79-
MaxFileSize: 128 * MiB,
80-
MaxTotalAge: 28 * 24 * time.Hour,
81-
MaxTotalFiles: 10,
81+
MaxFileSize: 128 * MiB,
82+
MaxFileAge: 7 * day,
83+
84+
MinAge: 3 * day,
85+
86+
MaxTotalSize: 128 * GiB,
87+
MaxTotalAge: 28 * day,
88+
89+
MaxTotalFiles: 20,
8290

8391
// SubstPattern: SubstPattern,
8492
// TimeFormat: TimeFormat,
@@ -271,14 +279,9 @@ func (f *File) filesToRemove(dir, base string, now time.Time, files []string) ([
271279
files = files[:p]
272280
size := int64(0)
273281

274-
for p > 0 {
282+
for ; p > 0; p-- {
275283
prev := p - 1
276284

277-
if f.MaxTotalFiles != 0 && len(files)-prev+1 > f.MaxTotalFiles {
278-
// tlog.Printw("remove files", "reason", "max_total_files", "x", len(files)-prev, "of", f.MaxTotalFiles, "files", files[:p])
279-
break
280-
}
281-
282285
n := filepath.Join(dir, files[prev])
283286

284287
inf, err := f.fstat(n)
@@ -293,12 +296,19 @@ func (f *File) filesToRemove(dir, base string, now time.Time, files []string) ([
293296
break
294297
}
295298

299+
if f.MinAge != 0 && now.Sub(ctime(inf, time.Time{})) <= f.MinAge {
300+
continue
301+
}
302+
296303
if f.MaxTotalAge != 0 && now.Sub(ctime(inf, now)) > f.MaxTotalAge {
297304
// tlog.Printw("remove files", "reason", "max_total_age", "total_age", now.Sub(ctime(inf, now)), "of", f.MaxTotalAge, "files", files[:p])
298305
break
299306
}
300307

301-
p--
308+
if f.MaxTotalFiles != 0 && len(files)-prev+1 > f.MaxTotalFiles {
309+
// tlog.Printw("remove files", "reason", "max_total_files", "x", len(files)-prev, "of", f.MaxTotalFiles, "files", files[:p])
310+
break
311+
}
302312
}
303313

304314
return files[:p], nil

0 commit comments

Comments
 (0)