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

more conservative channel behavior for waiters #756

Merged
merged 2 commits into from
Oct 5, 2020
Merged
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
10 changes: 7 additions & 3 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ RESET:
h.latestRound = next.Round()
pending := h.pending
h.pending = make([]chan []byte, 0)
h.pendingLk.Unlock()

for _, waiter := range pending {
waiter <- b
}
h.pendingLk.Unlock()

select {
case <-ctx.Done():
Expand Down Expand Up @@ -186,7 +186,8 @@ func (h *handler) getRand(ctx context.Context, info *chain.Info, round uint64) (
h.pendingLk.RUnlock()
// If so, prepare, and if we're still sync'd, add ourselves to the list of waiters.
if block {
ch := make(chan []byte)
ch := make(chan []byte, 1)
defer close(ch)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to use the more safe Go pattern where the sender closes the channel, instead of the receiver ? so the receiver can simply for _ := range myChan { .. } safely.

Copy link
Member Author

Choose a reason for hiding this comment

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

would you do it with a different channel for the receiver to notify the sender that its context has closed then?

h.pendingLk.Lock()
block = (h.latestRound+1 == round) && h.latestRound != 0
if block {
Expand All @@ -207,7 +208,10 @@ func (h *handler) getRand(ctx context.Context, info *chain.Info, round uint64) (
break
}
}
close(ch)
select {
case <-ch:
default:
}
return nil, ctx.Err()
}
}
Expand Down