10 18, 2021 8:46 AM
Hello,
I am implementing hubspot api rate limit policy in nodejs using cisco node/rate-limiter module. I am using some reference code and there is configuration file like this
10 18, 2021 9:15 AM
10 18, 2021 9:07 AM - 編集済み 10 18, 2021 9:17 AM
Hi @VPatil3 ,
I do not know about node/rate-limiter, but I use rate-limiter-flexible myself for the burst limit, and it works great. I can show you how I have set it up. I have the below code in my main app file and pass the limiter variable to all functions that make an API call to HubSpot:
const { RateLimiterMemory, RateLimiterQueue } = require('rate-limiter-flexible');
// Set this a little lower to be on the safe side.
const burstOptions = {
points: 100, // Requests
duration: 10 // Seconds
}
// Set this a little lower to be on the safe side.
const dailyOptions = {
points: 250000, // Requests
duration: 5184000 // Seconds
}
// Initiate ratelimiter
const burstLimiter = new RateLimiterMemory(burstOptions)
const dailyLimiter = new RateLimiterMemory(dailyOptions)
const burstLimits = new RateLimiterQueue(burstLimiter)
const dailyLimits = new RateLimiterQueue(dailyLimiter)
await randomAPICall(burstLimits, dailyLimits)
So in a function that calls the API, you can do something like this:
const randomAPICall = async (burstLimits, dailyLimits) => {
const remainingBurstTokens = await burstLimits.removeTokens(1)
const remainingDailyTokens = await dailyLimits.removeTokens(1)
return axios({
method: 'get',
url: ''
}).then((response) => {
}).catch((error) => {
})
}
If you use async await in your functions, the node package will pause all next calls until you have new tokens available.