Skip to content

Commit

Permalink
diagnostic settings scanner max concurrent requests are set to 200. A…
Browse files Browse the repository at this point in the history
…lso removed the use of WaitGroups
  • Loading branch information
cmendible committed Aug 20, 2024
1 parent 243f42a commit 1e28c8e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 27 deletions.
9 changes: 0 additions & 9 deletions internal/aprl_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io/fs"
"math"
"strings"
"sync"
"time"

"github.com/Azure/azqr/internal/azqr"
Expand Down Expand Up @@ -128,14 +127,7 @@ func (sc AprlScanner) Scan(ctx context.Context, cred azcore.TokenCredential, ser

batches := int(math.Ceil(float64(len(rules)) / 12))

var wg sync.WaitGroup
ch := make(chan []azqr.AprlResult, 12)
wg.Add(batches)

go func() {
wg.Wait()
close(ch)
}()

batchSzie := 12
batchNumber := 0
Expand All @@ -146,7 +138,6 @@ func (sc AprlScanner) Scan(ctx context.Context, cred azcore.TokenCredential, ser
}

go func(r []azqr.AprlRecommendation, b int) {
defer wg.Done()
if b > 0 {
// Staggering queries to avoid throttling. Max 15 queries each 5 seconds.
// https://learn.microsoft.com/en-us/azure/governance/resource-graph/concepts/guidance-for-throttled-requests#staggering-queries
Expand Down
10 changes: 0 additions & 10 deletions internal/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"strings"
"sync"
"time"

"github.com/Azure/azqr/internal/azqr"
Expand Down Expand Up @@ -169,14 +168,7 @@ func (sc Scanner) Scan(params *ScanParams) {
}

// scan each resource group
var wg sync.WaitGroup
ch := make(chan []azqr.AzqrServiceResult, 5)
wg.Add(len(params.ServiceScanners))

go func() {
wg.Wait()
close(ch)
}()

for _, s := range params.ServiceScanners {
err := s.Init(config)
Expand All @@ -185,8 +177,6 @@ func (sc Scanner) Scan(params *ScanParams) {
}

go func(s azqr.IAzureScanner) {
defer wg.Done()

res, err := sc.retry(3, 10*time.Millisecond, s, &scanContext)
if err != nil {
cancel()
Expand Down
12 changes: 4 additions & 8 deletions internal/scanners/diagnostics_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math"
"net/http"
"strings"
"sync"

"github.com/Azure/azqr/internal/azqr"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand Down Expand Up @@ -41,14 +40,10 @@ func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings(resource

batches := int(math.Ceil(float64(len(resources)) / 20))

var wg sync.WaitGroup
ch := make(chan map[string]bool, 5)
wg.Add(batches)

go func() {
wg.Wait()
close(ch)
}()
maxConcurrency := 200 // max number of concurrent requests
limiter := make(chan struct{}, maxConcurrency)

azqr.LogResourceTypeScan("Diagnostic Settings")

Expand All @@ -59,8 +54,9 @@ func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings(resource
if j > len(resources) {
j = len(resources)
}
limiter <- struct{}{} // Acquire a token. Waits here for token releases from the limiter.
go func(r []*string) {
defer wg.Done()
defer func() { <-limiter }() // Release the token
resp, err := d.restCall(d.ctx, r)
if err != nil {
log.Fatal().Err(err).Msg("Failed to get diagnostic settings")
Expand Down

0 comments on commit 1e28c8e

Please sign in to comment.