Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
147 additions
and
65 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package clog | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
// syncWriter is a thread safe io.Writer | ||
type syncWriter struct { | ||
io.Writer | ||
sync.Mutex | ||
} | ||
|
||
// Write data (thread safe) | ||
func (w *syncWriter) Write(p []byte) (n int, err error) { | ||
w.Lock() | ||
defer w.Unlock() | ||
|
||
return w.Writer.Write(p) | ||
} | ||
|
||
// fdWriter is an io.Writer that also has an Fd method. The most common | ||
// example of an fdWriter is an *os.File. | ||
type fdWriter interface { | ||
io.Writer | ||
Fd() uintptr | ||
} | ||
|
||
// syncFdWriter is a thread safe io.Writer with Fd() | ||
// Inspired by https://github.com/go-kit/log/ | ||
type syncFdWriter struct { | ||
fdWriter | ||
sync.Mutex | ||
} | ||
|
||
// Write data (thread safe) | ||
func (w *syncFdWriter) Write(p []byte) (n int, err error) { | ||
w.Lock() | ||
defer w.Unlock() | ||
|
||
return w.fdWriter.Write(p) | ||
} | ||
|
||
// NewSyncWriter creates a thread-safe io.Writer | ||
func NewSyncWriter(writer io.Writer) io.Writer { | ||
fdWriter, ok := writer.(fdWriter) | ||
if ok { | ||
return &syncFdWriter{ | ||
fdWriter: fdWriter, | ||
} | ||
} | ||
return &syncWriter{ | ||
Writer: writer, | ||
} | ||
} | ||
|
||
// Verify interface | ||
var ( | ||
_ io.Writer = &syncWriter{} | ||
_ io.Writer = &syncFdWriter{} | ||
) |
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,84 @@ | ||
package clog | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSyncWriterConcurrency(t *testing.T) { | ||
iterations := 10000 | ||
buffer := &bytes.Buffer{} | ||
writer := NewSyncWriter(buffer) | ||
wg := sync.WaitGroup{} | ||
wg.Add(iterations) | ||
for i := 0; i < iterations; i++ { | ||
go func(num int) { | ||
n, err := writer.Write([]byte("123456789\n")) | ||
assert.Equal(t, 10, n) | ||
assert.NoError(t, err) | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
lines := 0 | ||
for line, err := buffer.ReadString('\n'); err == nil; line, err = buffer.ReadString('\n') { | ||
assert.Len(t, line, 10) | ||
lines++ | ||
} | ||
assert.Equal(t, iterations, lines) | ||
} | ||
|
||
type fdBuffer struct { | ||
bytes.Buffer | ||
} | ||
|
||
func (b fdBuffer) Fd() uintptr { | ||
return 0 | ||
} | ||
|
||
func TestSyncFdWriterConcurrency(t *testing.T) { | ||
iterations := 10000 | ||
buffer := &fdBuffer{} | ||
writer := NewSyncWriter(buffer) | ||
wg := sync.WaitGroup{} | ||
wg.Add(iterations) | ||
for i := 0; i < iterations; i++ { | ||
go func(num int) { | ||
n, err := writer.Write([]byte("123456789\n")) | ||
assert.Equal(t, 10, n) | ||
assert.NoError(t, err) | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
lines := 0 | ||
for line, err := buffer.ReadString('\n'); err == nil; line, err = buffer.ReadString('\n') { | ||
assert.Len(t, line, 10) | ||
lines++ | ||
} | ||
assert.Equal(t, iterations, lines) | ||
} | ||
|
||
func TestSyncWriterNoFd(t *testing.T) { | ||
_, ok := NewSyncWriter(&bytes.Buffer{}).(interface { | ||
Fd() uintptr | ||
}) | ||
|
||
if ok { | ||
t.Error("NewSyncWriter should not expose a Fd method") | ||
} | ||
} | ||
|
||
func TestSyncWriterFd(t *testing.T) { | ||
_, ok := NewSyncWriter(os.Stdout).(interface { | ||
Fd() uintptr | ||
}) | ||
|
||
if !ok { | ||
t.Error("NewSyncWriter does not pass through Fd method") | ||
} | ||
} |