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

Add BufferedWriteSyncer #1

Closed
wants to merge 1 commit into from
Closed

Conversation

moisesvega
Copy link
Owner

This diff addresses comments in uber-go#782

  • Add BufferedWriteSyncer
  • Use new Clock Interface for testing

API for BufferedWriteSyncer:

type BufferedWriteSyncer struct {
	WriteSyncer
	sync.Mutex

	Size          int
	FlushInterval time.Duration
	Clock         Clock

	// unexported fields for state
}

buf := &bytes.Buffer{}
ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)} //  BufferedWriteSyncer with the default values
ws.Write([]byte("foo"))
ws.Close() // Close closes the buffer, cleans up background goroutines, and flushes remaining, unwritten data.

@moisesvega moisesvega closed this May 24, 2021
@moisesvega moisesvega deleted the refactor-sync-buffer branch May 24, 2021 22:25
Copy link

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

I know this is not the actual PR so providing some early feedback here.

Comment on lines +30 to +31
WriteSyncer
sync.Mutex
Copy link

Choose a reason for hiding this comment

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

FYI, anything you embed is part of the public API. That's probably not desirable for the mutex, but we can do it for the WriteSyncer if we want to.

https://github.com/uber-go/guide/blob/master/style.md#avoid-embedding-types-in-public-structs

Clock Clock

// unexported fields for state
size int
Copy link

Choose a reason for hiding this comment

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

unused field. can delete.

Comment on lines +84 to +86
s.Lock()
defer s.Unlock()

Copy link

Choose a reason for hiding this comment

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

If we're trying to make Write thread-safe, the initiallized check should also be thread safe.
The lock should be acquired before we change initialized.

if s.Clock != nil {
s.ticker = s.Clock.NewTicker(flushInterval)
} else {
s.ticker = time.NewTicker(flushInterval)
Copy link

Choose a reason for hiding this comment

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

We should use a default clock implementation if unset rather than if-else on using the clock versus using the system.

So the systemClock should be available in zapcore to be used.

)

func TestBufferWriter(t *testing.T) {
defer goleak.VerifyNone(t)
Copy link

Choose a reason for hiding this comment

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

not necessary if leak_test provides this via TestMain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants