-
Notifications
You must be signed in to change notification settings - Fork 4.5k
buffer & grpcsync: various cleanups and improvements #6785
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -18,7 +18,10 @@ | |
// Package buffer provides an implementation of an unbounded buffer. | ||
package buffer | ||
|
||
import "sync" | ||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
// Unbounded is an implementation of an unbounded buffer which does not use | ||
// extra goroutines. This is typically used for passing updates from one entity | ||
|
@@ -36,6 +39,7 @@ import "sync" | |
type Unbounded struct { | ||
c chan any | ||
closed bool | ||
closing bool | ||
mu sync.Mutex | ||
backlog []any | ||
} | ||
|
@@ -45,39 +49,41 @@ func NewUnbounded() *Unbounded { | |
return &Unbounded{c: make(chan any, 1)} | ||
} | ||
|
||
var errBufferClosed = errors.New("Put called on closed buffer.Unbounded") | ||
|
||
// Put adds t to the unbounded buffer. | ||
func (b *Unbounded) Put(t any) { | ||
func (b *Unbounded) Put(t any) error { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
if b.closed { | ||
return | ||
if b.closing { | ||
return errBufferClosed | ||
} | ||
if len(b.backlog) == 0 { | ||
select { | ||
case b.c <- t: | ||
return | ||
return nil | ||
default: | ||
} | ||
} | ||
b.backlog = append(b.backlog, t) | ||
return nil | ||
} | ||
|
||
// Load sends the earliest buffered data, if any, onto the read channel | ||
// returned by Get(). Users are expected to call this every time they read a | ||
// Load sends the earliest buffered data, if any, onto the read channel returned | ||
// by Get(). Users are expected to call this every time they successfully read a | ||
// value from the read channel. | ||
func (b *Unbounded) Load() { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
if b.closed { | ||
return | ||
} | ||
if len(b.backlog) > 0 { | ||
select { | ||
case b.c <- b.backlog[0]: | ||
b.backlog[0] = nil | ||
b.backlog = b.backlog[1:] | ||
default: | ||
} | ||
} else if b.closing && !b.closed { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For correctness sake and to ensure that an extra call to |
||
close(b.c) | ||
} | ||
} | ||
|
||
|
@@ -88,18 +94,23 @@ func (b *Unbounded) Load() { | |
// send the next buffered value onto the channel if there is any. | ||
// | ||
// If the unbounded buffer is closed, the read channel returned by this method | ||
// is closed. | ||
// is closed after all data is drained. | ||
func (b *Unbounded) Get() <-chan any { | ||
return b.c | ||
} | ||
|
||
// Close closes the unbounded buffer. | ||
// Close closes the unbounded buffer. No subsequent data may be Put(), and the | ||
// channel returned from Get() will be closed after all the data is read and | ||
// Load() is called for the final time. | ||
func (b *Unbounded) Close() { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
if b.closed { | ||
if b.closing { | ||
return | ||
} | ||
b.closed = true | ||
close(b.c) | ||
b.closing = true | ||
if len(b.backlog) == 0 { | ||
b.closed = true | ||
close(b.c) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to move these fields to be under the
mu
since they are clearly accessed always only with the mutex.