-
Notifications
You must be signed in to change notification settings - Fork 6
chann: support to consume all data after Close the channel. #5
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ func Cap(n int) Opt { | |
// one, and use Cap to configure the capacity of the channel. | ||
type Chann[T any] struct { | ||
in, out chan T | ||
backlog chan T | ||
close chan struct{} | ||
cfg *config | ||
q []T | ||
|
@@ -129,6 +130,7 @@ func New[T any](opts ...Opt) *Chann[T] { | |
case unbounded: | ||
ch.in = make(chan T, 16) | ||
ch.out = make(chan T, 16) | ||
ch.backlog = make(chan T, 1024) | ||
go ch.unboundedProcessing() | ||
} | ||
return ch | ||
|
@@ -208,14 +210,13 @@ func (ch *Chann[T]) unboundedTerminate() { | |
} | ||
for len(ch.q) > 0 { | ||
select { | ||
// Note if receiver doesn't consume all data that has been sent to input | ||
// channel, the `unboundedProcessing` goroutine will leak forever. | ||
// Ref: https://github.com/golang-design/chann/issues/3 | ||
case ch.out <- ch.q[0]: | ||
// The default branch exists because we need guarantee | ||
// the loop can terminate. If there is a receiver, the | ||
// first case will ways be selected. See #3. | ||
default: | ||
ch.q[0] = nilT // de-reference earlier to help GC | ||
ch.q = ch.q[1:] | ||
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. This reads like a previous implementation that was reported to be problematic, as discussed in #3. Could you help me to understand how your changes can improve the overall situation? 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. Yep, in this PR, it behaves as follows
|
||
} | ||
ch.q[0] = nilT // de-reference earlier to help GC | ||
ch.q = ch.q[1:] | ||
} | ||
close(ch.out) | ||
close(ch.close) | ||
|
Uh oh!
There was an error while loading. Please reload this page.