Skip to content

Commit 28d6db5

Browse files
committed
modified rate limiter. created constant for max page size in api request
1 parent 56db30d commit 28d6db5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

pkg/sources/docker/registries.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/url"
1010
"path"
1111
"strings"
12-
"time"
1312

1413
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1514

@@ -20,9 +19,11 @@ import (
2019
var defaultHTTPClient = common.RetryableHTTPClientTimeout(10)
2120

2221
// registryRateLimiter limits how quickly we make registry API calls across all registries.
23-
// We allow roughly 5 requests every ~7.5 seconds (one token every 1.5s) as a simple
24-
// safeguard against overloading upstream APIs.
25-
var registryRateLimiter = rate.NewLimiter(rate.Every(1500*time.Millisecond), 1)
22+
// We allow roughly 1 event every 1.5s, with a burst of 2 as a simple safeguard against overloading upstream APIs.
23+
var registryRateLimiter = rate.NewLimiter(rate.Limit(2.0/3.0), 2)
24+
25+
// maxRegistryPageSize defines the maximum number of images to request per page from a registry API.
26+
const maxRegistryPageSize = 100
2627

2728
// Image represents a container image or repository entry in a registry API response.
2829
type Image struct {
@@ -93,7 +94,7 @@ func (d *DockerHub) ListImages(ctx context.Context, namespace string) ([]string,
9394
}
9495

9596
query := baseURL.Query()
96-
query.Set("page_size", "100") // fetch images in batches of 100 per page
97+
query.Set("page_size", fmt.Sprint(maxRegistryPageSize))
9798
baseURL.RawQuery = query.Encode()
9899

99100
allImages := []string{}
@@ -346,7 +347,7 @@ func (g *GHCR) ListImages(ctx context.Context, namespace string) ([]string, erro
346347
u := *baseURL
347348
q := u.Query()
348349
q.Set("package_type", "container")
349-
q.Set("per_page", "100") // fetch images in batches of 100 per page
350+
q.Set("per_page", fmt.Sprint(maxRegistryPageSize)) // fetch images in batches of 100 per page
350351
u.RawQuery = q.Encode()
351352
return u.String()
352353
}()

0 commit comments

Comments
 (0)