Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support concurrent-safe WriteSyncer #359

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion zapcore/write_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type WriteSyncer interface {
Sync() error
}

// LockedWriteSyncer is the interface that groups sync.Locker and WriteSyncer methods.
type LockedWriteSyncer interface {
sync.Locker
WriteSyncer
}

// AddSync converts an io.Writer to a WriteSyncer. It attempts to be
// intelligent: if the concrete type of the io.Writer implements WriteSyncer,
// we'll use the existing Sync method. If it doesn't, we'll add a no-op Sync.
Expand All @@ -54,7 +60,7 @@ type lockedWriteSyncer struct {
// Lock wraps a WriteSyncer in a mutex to make it safe for concurrent use. In
// particular, *os.Files must be locked before use.
func Lock(ws WriteSyncer) WriteSyncer {
if _, ok := ws.(*lockedWriteSyncer); ok {
if _, ok := ws.(LockedWriteSyncer); ok {
// no need to layer on another lock
return ws
}
Expand Down
10 changes: 10 additions & 0 deletions zapcore/write_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ func TestAddSyncWriteSyncer(t *testing.T) {
assert.Error(t, ws.Sync(), "Expected to propagate errors from concrete type's Sync method.")
}

func TestLockedWriteSyncerInterface(t *testing.T) {
buf := &bytes.Buffer{}
ws := AddSync(buf)
var lws WriteSyncer = &lockedWriteSyncer{ws: ws}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be even better if the test uses a concrete type separately implemented from the internal lockedWriteSyncer since, as I understand it, the entire point of this PR is to support external implementations (and avoid double-locking them).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, half part. It also makes sure the internal lockedWriteSyncer is okay.
A separately implementation would be almost same as our default one, so I don't think it necessary.
Would some rewords help? Any thoughts?


_, ok := lws.(LockedWriteSyncer)
require.True(t, ok, "Wrong implementation of the builtin lockedWriteSyncer")
require.Equal(t, lws, Lock(lws), "Unnessessary wrapped lock for locked WriteSyncer")
}

func TestAddSyncWriter(t *testing.T) {
// If we pass a plain io.Writer, make sure that we still get a WriteSyncer
// with a no-op Sync.
Expand Down