@@ -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