From 1c9d3d5cedce022b7dffe4a702ed4da4ff6a80b9 Mon Sep 17 00:00:00 2001 From: chris48s Date: Mon, 6 May 2024 19:59:37 +0100 Subject: [PATCH] tweaks to libraries.io token pooling code (#10074) * decrease batch size * set nextReset in seconds * update test assertions --- services/librariesio/librariesio-api-provider.js | 5 +++-- .../librariesio/librariesio-api-provider.spec.js | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/services/librariesio/librariesio-api-provider.js b/services/librariesio/librariesio-api-provider.js index 4eaf5f54badd1..523d8b0e6b8c6 100644 --- a/services/librariesio/librariesio-api-provider.js +++ b/services/librariesio/librariesio-api-provider.js @@ -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)) } } @@ -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, diff --git a/services/librariesio/librariesio-api-provider.spec.js b/services/librariesio/librariesio-api-provider.spec.js index 52841d5d2dad8..b2604d1c7f6eb 100644 --- a/services/librariesio/librariesio-api-provider.spec.js +++ b/services/librariesio/librariesio-api-provider.spec.js @@ -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 }) @@ -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 }) @@ -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 }) })