Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make buffered_write safe for gomaxprox > 1

  • Loading branch information...
commit 2b7bd3944e17d1bb497780843511953dc0336b4b 1 parent 8d4c492
@dgrijalva dgrijalva authored
Showing with 28 additions and 8 deletions.
  1. +28 −8 buffered_writer.go
View
36 buffered_writer.go
@@ -10,29 +10,49 @@ import (
type BufferedWriter struct {
buf *bufio.Writer
writer io.WriteCloser
+ mc chan string
+ fc chan int
}
func NewBufferedWriter(writer io.WriteCloser) *BufferedWriter {
bw := new(BufferedWriter)
bw.writer = writer
bw.buf = bufio.NewWriter(writer)
+ bw.mc = make(chan string)
+ bw.fc = make(chan int)
+ go bw.writeLoop()
return bw
}
-func (bw *BufferedWriter) LogWrite(msg string) {
- _, err := bw.buf.Write([]byte(msg))
- if err != nil {
- // uh-oh... what do i do if logging fails; punt!
- log.Printf("TIMBER! epic fail: %v", err)
+func (bw *BufferedWriter) writeLoop() {
+ for {
+ select {
+ case msg, ok := <- bw.mc:
+ if !ok {
+ bw.buf.Flush()
+ bw.writer.Close()
+ return
+ }
+ _, err := bw.buf.Write([]byte(msg))
+ if err != nil {
+ // uh-oh... what do i do if logging fails; punt!
+ log.Printf("TIMBER! epic fail: %v", err)
+ }
+ case <- bw.fc:
+ bw.buf.Flush()
+ }
}
}
+func (bw *BufferedWriter) LogWrite(msg string) {
+ bw.mc <- msg
+}
+
// Force flush the buffer
func (bw *BufferedWriter) Flush() {
- bw.buf.Flush()
+ bw.fc <- 1
}
func (bw *BufferedWriter) Close() {
- bw.buf.Flush()
- bw.writer.Close()
+ close(bw.mc)
}
Please sign in to comment.
Something went wrong with that request. Please try again.