fix: avoid goroutine leak after timeouts in PeerMetrics (#16569)

This commit is contained in:
jiuker 2023-02-09 01:11:16 +08:00 committed by GitHub
parent c97f50e274
commit 1828fb212a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions

View File

@ -838,14 +838,20 @@ func (client *peerRESTClient) GetPeerMetrics(ctx context.Context) (<-chan Metric
dec := gob.NewDecoder(respBody)
ch := make(chan Metric)
go func(ch chan<- Metric) {
defer func() {
xhttp.DrainBody(respBody)
close(ch)
}()
for {
var metric Metric
if err := dec.Decode(&metric); err != nil {
xhttp.DrainBody(respBody)
close(ch)
return
}
ch <- metric
select {
case <-ctx.Done():
return
case ch <- metric:
}
}
}(ch)
return ch, nil