Skip to content

Commit

Permalink
safe writer renamed to sync writer
Browse files Browse the repository at this point in the history
(inspired by go-kit/log)
  • Loading branch information
creativeprojects committed Sep 10, 2021
1 parent 8335b65 commit 90b9625
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 65 deletions.
31 changes: 0 additions & 31 deletions safe_writer.go

This file was deleted.

32 changes: 0 additions & 32 deletions safe_writer_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions standard_output.go
Expand Up @@ -14,7 +14,7 @@ var (
// Calling this function multiple times always returns the same instance of io.Writer.
func Stdout() io.Writer {
if stdout == nil {
stdout = NewSafeWriter(os.Stdout)
stdout = NewSyncWriter(os.Stdout)
}
return stdout
}
Expand All @@ -23,7 +23,7 @@ func Stdout() io.Writer {
// Calling this function multiple times always returns the same instance of io.Writer.
func Stderr() io.Writer {
if stderr == nil {
stderr = NewSafeWriter(os.Stderr)
stderr = NewSyncWriter(os.Stderr)
}
return stderr
}
61 changes: 61 additions & 0 deletions sync_writer.go
@@ -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{}
)
84 changes: 84 additions & 0 deletions sync_writer_test.go
@@ -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")
}
}

0 comments on commit 90b9625

Please sign in to comment.