Skip to content

Commit

Permalink
add auto gc support for rotating file writer
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Apr 23, 2021
1 parent f465aac commit 9b17f4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 7 additions & 5 deletions writer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ type RotatingFileWriter struct {
lock sync.Mutex
}

func NewDefaultRotatingFileWriter(fn RotatingFileFn) *RotatingFileWriter {
return NewRotatingFileWriter(os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666, fn)
func NewDefaultRotatingFileWriter(ctx context.Context, fn RotatingFileFn) *RotatingFileWriter {
return NewRotatingFileWriter(ctx, 10*time.Minute, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666, fn)
}

func NewRotatingFileWriter(flag int, perm os.FileMode, fn RotatingFileFn) *RotatingFileWriter {
return &RotatingFileWriter{fn: fn, flag: flag, perm: perm, openedFiles: make(map[string]*FileWriter)}
func NewRotatingFileWriter(ctx context.Context, gcDuration time.Duration, flag int, perm os.FileMode, fn RotatingFileFn) *RotatingFileWriter {
wr := &RotatingFileWriter{fn: fn, flag: flag, perm: perm, openedFiles: make(map[string]*FileWriter)}
wr.autoGC(ctx, gcDuration)
return wr
}

func (writer *RotatingFileWriter) Write(le level.Level, module string, message string) error {
return writer.getWriter(writer.fn(le, module)).Write(le, module, message)
}

func (writer *RotatingFileWriter) AutoGC(ctx context.Context, interval time.Duration) {
func (writer *RotatingFileWriter) autoGC(ctx context.Context, interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
Expand Down
20 changes: 12 additions & 8 deletions writer/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestRotatingFileWriter_Write(t *testing.T) {

fileNo := fileNo1

fw := writer.NewDefaultRotatingFileWriter(func(le level.Level, module string) string {
fw := writer.NewDefaultRotatingFileWriter(context.TODO(), func(le level.Level, module string) string {
return fileNo
})

Expand Down Expand Up @@ -79,7 +79,7 @@ func TestRotatingFileWriter_Write(t *testing.T) {

func TestRotatingFileWriter_ReOpen(t *testing.T) {
filename := "test_rotating_file.log"
fw := writer.NewDefaultRotatingFileWriter(func(le level.Level, module string) string {
fw := writer.NewDefaultRotatingFileWriter(context.TODO(), func(le level.Level, module string) string {
return filename
})

Expand All @@ -97,7 +97,7 @@ func TestRotatingFileWriter_ReOpen(t *testing.T) {
}

func TestRotatingFileWriter_GC(t *testing.T) {
fw := writer.NewDefaultRotatingFileWriter(func(le level.Level, module string) string {
fw := writer.NewDefaultRotatingFileWriter(context.TODO(), func(le level.Level, module string) string {
return fmt.Sprintf("%s-%s.log", module, le.GetLevelName())
})

Expand All @@ -123,11 +123,15 @@ func TestRotatingFileWriter_GC(t *testing.T) {
}

func TestRotatingFileWriter_AutoGC(t *testing.T) {
fw := writer.NewDefaultRotatingFileWriter(func(le level.Level, module string) string {
return fmt.Sprintf("%s-%s.log", module, le.GetLevelName())
})

fw.AutoGC(context.Background(), 1*time.Millisecond)
fw := writer.NewRotatingFileWriter(
context.TODO(),
1*time.Millisecond,
os.O_CREATE|os.O_APPEND|os.O_WRONLY,
0666,
func(le level.Level, module string) string {
return fmt.Sprintf("%s-%s.log", module, le.GetLevelName())
},
)

defer func() {
_ = os.Remove("test1-DEBUG.log")
Expand Down

0 comments on commit 9b17f4c

Please sign in to comment.