Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrently fetch metrics #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 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,42 @@ 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{}
var wg sync.WaitGroup
var err error
var mu sync.Mutex

for _, chunk := range chunks {
set, err := in.fetch(ctx, dbiResourceId, dur, chunk...)
if err != nil {
return nil, err
}
chunk := chunk
wg.Add(1)

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

set, e := in.fetch(childContext, dbiResourceId, dur, chunk...)

mu.Lock()
defer mu.Unlock()

if e != nil {
if err == nil {
cancel()
err = e
}
return
}

for k, v := range set {
samples[k] = v
fogfish marked this conversation as resolved.
Show resolved Hide resolved
}
}()
}
wg.Wait()
if err != nil {
return nil, err
}

return samples, nil
Expand Down
Loading