Skip to content

Commit

Permalink
Query: fix streamSeriesSet defer in for loop (#4830)
Browse files Browse the repository at this point in the history
* Query: fix streamSeriesSet defer in for loop
which lead too many timer in heap.

Signed-off-by: Jimmiehan <[email protected]>

* Query: Add comment to handleRecvResponse

Signed-off-by: Jimmiehan <[email protected]>
  • Loading branch information
hanjm authored Nov 8, 2021
1 parent 102d294 commit abcf7c5
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pkg/store/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,28 +418,30 @@ func startStreamSeriesSet(
}
}
}()
for {
// The `defer` only executed when function return, we do `defer cancel` in for loop,
// so make the loop body as a function, release timers created by context as early.
handleRecvResponse := func() (next bool) {
frameTimeoutCtx, cancel := frameCtx(s.responseTimeout)
defer cancel()
var rr *recvResponse
select {
case <-ctx.Done():
s.handleErr(errors.Wrapf(ctx.Err(), "failed to receive any data from %s", s.name), done)
return
return false
case <-frameTimeoutCtx.Done():
s.handleErr(errors.Wrapf(frameTimeoutCtx.Err(), "failed to receive any data in %s from %s", s.responseTimeout.String(), s.name), done)
return
return false
case rr = <-rCh:
}

if rr.err == io.EOF {
close(done)
return
return false
}

if rr.err != nil {
s.handleErr(errors.Wrapf(rr.err, "receive series from %s", s.name), done)
return
return false
}
numResponses++
bytesProcessed += rr.r.Size()
Expand All @@ -455,9 +457,15 @@ func startStreamSeriesSet(
case s.recvCh <- series:
case <-ctx.Done():
s.handleErr(errors.Wrapf(ctx.Err(), "failed to receive any data from %s", s.name), done)
return
return false
}
}
return true
}
for {
if !handleRecvResponse() {
return
}
}
}()
return s
Expand Down

0 comments on commit abcf7c5

Please sign in to comment.