Skip to content

Commit

Permalink
Merge pull request #6 from ajainc/feature/fix-memory-leak
Browse files Browse the repository at this point in the history
fix memory leak
  • Loading branch information
0tarof authored Jul 25, 2018
2 parents f40d7dd + 1eb29e3 commit 07f5326
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
11 changes: 7 additions & 4 deletions appender_file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package golog

import (
"fmt"
"os"
"sync"

)

// defaultBufferSize
Expand Down Expand Up @@ -56,8 +56,11 @@ func NewFileAppenderWithBufferSize(fileName string, bufferSize int) (asyncFileAp
func (appender *FileAppender) Write(data []byte) (n int, err error) {
appender.mu.Lock()
defer appender.mu.Unlock()
data = append(data, '\n')
return appender.bufferedWriter.Write(data)
if appender.activated {
data = append(data, '\n')
return appender.bufferedWriter.Write(data)
}
return 0, fmt.Errorf("appender is closed")
}

// Close implements io.Closer
Expand All @@ -78,4 +81,4 @@ func (appender *FileAppender) Close() error {
}

return nil
}
}
13 changes: 9 additions & 4 deletions appender_file_rotatable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package golog
import (
"os"
"os/signal"
"syscall"
"sync"
"syscall"
)

// RotatableFileAppender RotatableFileAppender struct
type RotatableFileAppender struct {
*FileAppender
mu *sync.Mutex
}

// NewRotatableFileAppender returns new FileAppender
Expand All @@ -27,16 +28,16 @@ func NewRotatableFileAppenderWithBufferSize(fileName string, bufferSize int) (as

appender := &RotatableFileAppender{
FileAppender: fileAppender,
mu: new(sync.Mutex),
}

hup := make(chan os.Signal, 1)
signal.Notify(hup, syscall.SIGHUP)
mutex := new(sync.Mutex)

go func() {
for {
<-hup
mutex.Lock()
appender.mu.Lock()

appender.FileAppender.Close()

Expand All @@ -46,7 +47,7 @@ func NewRotatableFileAppenderWithBufferSize(fileName string, bufferSize int) (as
}
appender.FileAppender = newAppender

mutex.Unlock()
appender.mu.Unlock()
}
}()

Expand All @@ -55,10 +56,14 @@ func NewRotatableFileAppenderWithBufferSize(fileName string, bufferSize int) (as

// Write implements io.Write
func (appender *RotatableFileAppender) Write(data []byte) (n int, err error) {
appender.mu.Lock()
defer appender.mu.Unlock()
return appender.FileAppender.Write(data)
}

// Close implements io.Closer
func (appender *RotatableFileAppender) Close() error {
appender.mu.Lock()
defer appender.mu.Unlock()
return appender.FileAppender.Close()
}

0 comments on commit 07f5326

Please sign in to comment.