Skip to content

Commit 8fc33c5

Browse files
committed
fix: more on throttling #249
1 parent 602dd2a commit 8fc33c5

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

internal/aprl_scanner.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ func (sc AprlScanner) graphScan(ctx context.Context, graphClient *graph.GraphQue
188188
subs = append(subs, &s)
189189
}
190190

191+
sentQueries := 0
191192
for _, rule := range rules {
192193
if rule.GraphQuery != "" {
193194
result := graphClient.Query(ctx, rule.GraphQuery, subs)
@@ -258,6 +259,12 @@ func (sc AprlScanner) graphScan(ctx context.Context, graphClient *graph.GraphQue
258259
})
259260
}
260261
}
262+
sentQueries++
263+
if sentQueries == 2 {
264+
// Staggering queries to avoid throttling. Max 10 queries each 5 seconds.
265+
// https://learn.microsoft.com/en-us/azure/governance/resource-graph/concepts/guidance-for-throttled-requests#staggering-queries
266+
time.Sleep(1 * time.Second)
267+
}
261268
}
262269
}
263270

internal/scanner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func (sc Scanner) Scan(params *ScanParams) {
139139
if err != nil {
140140
log.Fatal().Err(err).Msg("Failed to initialize diagnostic settings scanner")
141141
}
142+
142143
diagResults = diagnosticsScanner.Scan(reportData.ResourceIDs())
143144
}
144145

internal/scanners/diagnostics_settings.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"strings"
1212
"sync"
13+
"time"
1314

1415
"github.com/Azure/azqr/internal/azqr"
1516
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
@@ -59,20 +60,31 @@ func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings(resource
5960

6061
// Start workers
6162
// Based on: https://medium.com/insiderengineering/concurrent-http-requests-in-golang-best-practices-and-techniques-f667e5a19dea
62-
numWorkers := 200 // Define the number of workers in the pool
63+
numWorkers := 100 // Define the number of workers in the pool
6364
for w := 0; w < numWorkers; w++ {
6465
go d.worker(jobs, ch, &wg)
6566
}
6667
wg.Add(batches)
6768

6869
// Split resources into batches of 20 items.
6970
batchSize := 20
71+
batchCount := 0
7072
for i := 0; i < len(resources); i += batchSize {
7173
j := i + batchSize
7274
if j > len(resources) {
7375
j = len(resources)
7476
}
7577
jobs <- resources[i:j]
78+
79+
batchCount++
80+
if batchCount == numWorkers {
81+
log.Debug().Msgf("all %d workers are running. Sleeping for 4 seconds to avoid throttling", numWorkers)
82+
batchCount = 0
83+
// there are more batches to process
84+
// Staggering queries to avoid throttling. Max 15 queries each 5 seconds.
85+
// https://learn.microsoft.com/en-us/azure/governance/resource-graph/concepts/guidance-for-throttled-requests#staggering-queries
86+
time.Sleep(4 * time.Second)
87+
}
7688
}
7789

7890
// Wait for all workers to finish
@@ -127,7 +139,6 @@ func (d *DiagnosticSettingsScanner) restCall(ctx context.Context, resourceIds []
127139

128140
for _, resourceId := range resourceIds {
129141
batch.Requests = append(batch.Requests, ArmBatchRequestItem{
130-
Name: *resourceId,
131142
HttpMethod: http.MethodGet,
132143
RelativeUrl: *resourceId + "/providers/microsoft.insights/diagnosticSettings?api-version=2021-05-01-preview",
133144
})
@@ -167,7 +178,6 @@ type (
167178
}
168179

169180
ArmBatchRequestItem struct {
170-
Name string `json:"name"`
171181
HttpMethod string `json:"httpMethod"`
172182
RelativeUrl string `json:"relativeUrl"`
173183
}

0 commit comments

Comments
 (0)