-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #556 from rgooch/master
Add lib/bufwriter package and use it to fix idle flushing corner case in lib/logbuf package.
- Loading branch information
Showing
4 changed files
with
92 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Package bufwriter implements a simplified buffered writer, similar to the | ||
// bufio package in the Go standard library, but adds automatic flushing. | ||
package bufwriter | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type flushingWriter interface { | ||
Flush() error | ||
io.Writer | ||
} | ||
|
||
type Writer struct { | ||
flushDelay time.Duration | ||
flushingWriter flushingWriter | ||
mutex sync.Mutex // Protect everything below. | ||
err error | ||
flushPending bool | ||
} | ||
|
||
func NewWriter(writer io.Writer, flushDelay time.Duration) *Writer { | ||
return newWriter(writer, flushDelay) | ||
} | ||
|
||
func (b *Writer) Flush() error { return b.flush() } | ||
|
||
func (b *Writer) Write(p []byte) (int, error) { return b.write(p) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bufwriter | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"time" | ||
) | ||
|
||
func newWriter(writer io.Writer, flushDelay time.Duration) *Writer { | ||
b := &Writer{flushDelay: flushDelay} | ||
if bufWriter, ok := writer.(flushingWriter); ok { | ||
b.flushingWriter = bufWriter | ||
} else { | ||
b.flushingWriter = bufio.NewWriter(writer) | ||
} | ||
return b | ||
} | ||
|
||
func (b *Writer) delayedFlush() { | ||
time.Sleep(b.flushDelay) | ||
b.flush() | ||
} | ||
|
||
func (b *Writer) flush() error { | ||
b.mutex.Lock() | ||
defer b.mutex.Unlock() | ||
b.flushPending = false | ||
if b.err != nil { | ||
return b.err | ||
} | ||
b.err = b.flushingWriter.Flush() | ||
return b.err | ||
} | ||
|
||
func (b *Writer) lockAndScheduleFlush() { | ||
b.mutex.Lock() | ||
if b.flushPending { | ||
return | ||
} | ||
b.flushPending = true | ||
go b.delayedFlush() | ||
} | ||
|
||
func (b *Writer) write(p []byte) (int, error) { | ||
b.lockAndScheduleFlush() | ||
defer b.mutex.Unlock() | ||
if b.err != nil { | ||
return 0, b.err | ||
} | ||
nWritten, err := b.flushingWriter.Write(p) | ||
b.err = err | ||
return nWritten, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters