Skip to content

Commit

Permalink
Flip boolean logic & check for zero value as well
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Aug 25, 2023
1 parent 1b1ea01 commit 3269fb2
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sfstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ func (g *Group) doWork(key string, fn func() (io.ReadCloser, error)) func() (int
if chans, ok := g.calls[key]; !ok {
return nil, fmt.Errorf("expected to find singleflight key \"%s\", but didn't", key)
} else {
skipStream := fnRes == nil
var zero io.ReadCloser
canStream := fnRes != nil && fnRes != zero
writers := make([]*io.PipeWriter, 0)
for _, ch := range chans {
if skipStream {
if !canStream {
// This needs to be async to prevent a deadlock
go func(ch chan<- io.ReadCloser) {
ch <- nil
Expand All @@ -111,7 +112,7 @@ func (g *Group) doWork(key string, fn func() (io.ReadCloser, error)) func() (int
}
delete(g.calls, key) // we've done all we can for this call: clear it before we unlock

if !skipStream {
if canStream {
// Do the io copy async to prevent holding up other singleflight calls
go finishCopy(writers, fnRes)
}
Expand Down

0 comments on commit 3269fb2

Please sign in to comment.