Skip to content

Commit

Permalink
tweaks to libraries.io token pooling code (#10074)
Browse files Browse the repository at this point in the history
* decrease batch size

* set nextReset in seconds

* update test assertions
  • Loading branch information
chris48s authored May 6, 2024
1 parent 2857d9b commit 1c9d3d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions services/librariesio/librariesio-api-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class LibrariesIoApiProvider {
})

if (this.withPooling) {
this.standardTokens = new TokenPool({ batchSize: 45 })
this.standardTokens = new TokenPool({ batchSize: 10 })
tokens.forEach(t => this.standardTokens.add(t, {}, defaultRateLimit))
}
}
Expand Down Expand Up @@ -48,7 +48,8 @@ export default class LibrariesIoApiProvider {
// If the header is absent, we just use the current timestamp to
// advance the value to _something_
const retryAfter = headers['retry-after']
const nextReset = Date.now() + (retryAfter ? +retryAfter * 1000 : 0)
const nextReset =
((Date.now() + (retryAfter ? +retryAfter * 1000 : 0)) / 1000) >>> 0

return {
rateLimit,
Expand Down
12 changes: 9 additions & 3 deletions services/librariesio/librariesio-api-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('LibrariesIoApiProvider', function () {

expect(token.update).to.have.been.calledWith(
remaining,
nextReset * 1000 + tickTime,
((nextReset * 1000 + tickTime) / 1000) >>> 0,
)
expect(token.invalidate).not.to.have.been.called
})
Expand All @@ -98,7 +98,10 @@ describe('LibrariesIoApiProvider', function () {
const mockRequest = sinon.stub().resolves(response)
await provider.fetch(mockRequest, '/npm/badge-maker')

expect(token.update).to.have.been.calledWith(remaining, tickTime)
expect(token.update).to.have.been.calledWith(
remaining,
(tickTime / 1000) >>> 0,
)
expect(token.invalidate).not.to.have.been.called
})

Expand All @@ -109,7 +112,10 @@ describe('LibrariesIoApiProvider', function () {
const mockRequest = sinon.stub().resolves(response)
await provider.fetch(mockRequest, '/npm/badge-maker')

expect(token.update).to.have.been.calledWith(remaining - 1, tickTime)
expect(token.update).to.have.been.calledWith(
remaining - 1,
(tickTime / 1000) >>> 0,
)
expect(token.invalidate).not.to.have.been.called
})
})
Expand Down

0 comments on commit 1c9d3d5

Please sign in to comment.