Skip to content

Commit

Permalink
nsq_to_file: refactor confusing Write interface
Browse files Browse the repository at this point in the history
Part of this change is to only increment `f.filesize` with the actual
number of bytes written, rather than supplied.
  • Loading branch information
mreiferson committed Jan 3, 2019
1 parent 07222b5 commit 5805d68
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions apps/nsq_to_file/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func (f *FileLogger) router() {
f.updateFile()
sync = true
}
_, err := f.writer.Write(m.Body)
_, err := f.Write(m.Body)
if err != nil {
log.Fatalf("ERROR: writing message to disk - %s", err)
}
_, err = f.writer.Write([]byte("\n"))
_, err = f.Write([]byte("\n"))
if err != nil {
log.Fatalf("ERROR: writing newline to disk - %s", err)
}
Expand Down Expand Up @@ -205,9 +205,10 @@ func (f *FileLogger) Close() {
f.out = nil
}

func (f *FileLogger) Write(p []byte) (n int, err error) {
f.filesize += int64(len(p))
return f.out.Write(p)
func (f *FileLogger) Write(p []byte) (int, error) {
n, err := f.writer.Write(p)
f.filesize += int64(n)
return err
}

func (f *FileLogger) Sync() error {
Expand Down Expand Up @@ -323,10 +324,10 @@ func (f *FileLogger) updateFile() {
}

if f.opts.GZIP {
f.gzipWriter, _ = gzip.NewWriterLevel(f, f.opts.GZIPLevel)
f.gzipWriter, _ = gzip.NewWriterLevel(f.out, f.opts.GZIPLevel)
f.writer = f.gzipWriter
} else {
f.writer = f
f.writer = f.out
}
}

Expand Down

0 comments on commit 5805d68

Please sign in to comment.