Skip to content

Commit

Permalink
Add a threshold so small batches don't have jitter. Bump jitter windo…
Browse files Browse the repository at this point in the history
…w. (#340)

* Add a threshold so small batches don't have jitter. Bump jitter window.

- 10 seconds should space out requests more to reduce congestion.
- Early requests don't have jitter appled to avoid slowing down small
  batch sizes.

See #337

Signed-off-by: Caleb Brown <[email protected]>

* Switch to a simpler toggle.

Signed-off-by: Caleb Brown <[email protected]>

---------

Signed-off-by: Caleb Brown <[email protected]>
  • Loading branch information
calebbrown authored May 11, 2023
1 parent 0922729 commit 1e17778
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/feeds/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ const (
// maxJitterMillis is the upper bound of random jitter introcude while
// issuing requests to NPM. Random jitter will be generated between 0 and
// maxJitterMillis.
maxJitterMillis = 1000
maxJitterMillis = 10000

// minJitterThreshold is the minimum number of packages that need to be fetched
// before the jitter is applied. This allows a number of packages to
// fetched without a delay. The number is chosen somewhat arbitrarily, but
// is 10% of the rssLimit above.
minJitterThreshold = 20
)

var (
Expand Down Expand Up @@ -179,11 +185,14 @@ func fetchAllPackages(registryURL string) ([]*feeds.Package, []error) {
uniquePackages[pkg.Title]++
}

applyJitter := len(uniquePackages) > minJitterThreshold
for pkgTitle, count := range uniquePackages {
go func(pkgTitle string, count int) {
// Before requesting, wait, so all the requests don't arrive at once.
jitter := time.Duration(rand.Intn(maxJitterMillis)) * time.Millisecond //nolint:gosec
time.Sleep(jitter)
if applyJitter {
// Before requesting, wait, so all the requests don't arrive at once.
jitter := time.Duration(rand.Intn(maxJitterMillis)) * time.Millisecond //nolint:gosec
time.Sleep(jitter)
}

pkgs, err := fetchPackage(registryURL, pkgTitle)
if err != nil {
Expand Down

0 comments on commit 1e17778

Please sign in to comment.