Skip to content

Commit

Permalink
chore(refactor): Avoid goroutine leak in case of fetching errors (#15018
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cyriltovena authored Nov 22, 2024
1 parent c4ced3f commit 0f242e7
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions pkg/storage/async_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/c2h5oh/datasize"
"github.com/opentracing/opentracing-go"
"golang.org/x/sync/errgroup"

"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/storage/stores"
Expand Down Expand Up @@ -72,23 +73,23 @@ func (a *AsyncStore) GetChunks(ctx context.Context,
predicate chunk.Predicate,
storeChunksOverride *logproto.ChunkRefGroup,
) ([][]chunk.Chunk, []*fetcher.Fetcher, error) {
errs := make(chan error)

var storeChunks [][]chunk.Chunk
g, ctx := errgroup.WithContext(ctx)

var fetchers []*fetcher.Fetcher
go func() {
g.Go(func() error {
var err error
storeChunks, fetchers, err = a.Store.GetChunks(ctx, userID, from, through, predicate, storeChunksOverride)
errs <- err
}()
return err
})

var ingesterChunks []string

go func() {
g.Go(func() error {
if !a.shouldQueryIngesters(through, model.Now()) {
level.Debug(util_log.Logger).Log("msg", "skipping querying ingesters for chunk ids", "query-from", from, "query-through", through)
errs <- nil
return
return nil
}

var err error
Expand All @@ -100,14 +101,11 @@ func (a *AsyncStore) GetChunks(ctx context.Context,
}
level.Debug(util_log.Logger).Log("msg", "got chunk ids from ingester", "count", len(ingesterChunks))
}
errs <- err
}()
return err
})

for i := 0; i < 2; i++ {
err := <-errs
if err != nil {
return nil, nil, err
}
if err := g.Wait(); err != nil {
return nil, nil, err
}

if len(ingesterChunks) == 0 {
Expand Down

0 comments on commit 0f242e7

Please sign in to comment.