Skip to content

Commit

Permalink
fix: concurrently fetch metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AnshulMalik committed Apr 7, 2024
1 parent 8228f28 commit 9734a95
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions internal/insight/insight.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package insight

import (
"context"
"sync"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -65,17 +66,36 @@ func (in *Insight) Fetch(ctx context.Context, dbiResourceId string, dur time.Dur
chunks = append(chunks, metrics[i:end])
}

// TODO: concurrent
childContext, cancel := context.WithCancel(ctx)
defer cancel()

samples := map[string]Samples{}
for _, chunk := range chunks {
set, err := in.fetch(ctx, dbiResourceId, dur, chunk...)
if err != nil {
return nil, err
}
var wg sync.WaitGroup
var err error

for k, v := range set {
samples[k] = v
}
for _, chunk := range chunks {
chunk := chunk
go func() {
wg.Add(1)
defer wg.Done()

set, e := in.fetch(childContext, dbiResourceId, dur, chunk...)
if e != nil {
if e != context.Canceled {
cancel()
err = e
}
return
}

for k, v := range set {
samples[k] = v
}
}()
}
wg.Wait()
if err != nil {
return nil, err
}

return samples, nil
Expand Down

0 comments on commit 9734a95

Please sign in to comment.