feat(429): retry 429 request errors

This commit is contained in:
jfrazx 2024-01-22 22:19:05 -08:00
parent 73c21242b4
commit 70827727aa
No known key found for this signature in database
GPG Key ID: 7E72C3BCC0F85A7B

View File

@ -1,7 +1,7 @@
const axios = require('axios')
const { levenshteinDistance } = require('../utils/index')
const Logger = require('../Logger')
const pThrottle = require('p-throttle')
const Throttle = require('p-throttle')
class Audnexus {
static _instance = null
@ -16,13 +16,13 @@ class Audnexus {
// Rate limit is 100 requests per minute.
// @see https://github.com/laxamentumtech/audnexus#-deployment-
this.limiter = pThrottle({
// Setting the limit to 1 allows for a short pause between requests that is almost imperceptible to
// the end user. A larger limit will grab blocks faster and then wait for the alloted time(interval) before
// fetching another batch.
this.limiter = Throttle({
// Setting the limit to 1 allows for a short pause between requests that is imperceptible to the end user.
// A larger limit will grab blocks faster and then wait for the alloted time(interval) before
// fetching another batch, but with a discernable pause from the user perspective.
limit: 1,
strict: true,
interval: 300
interval: 150
})
Audnexus._instance = this
@ -35,8 +35,8 @@ class Audnexus {
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
const throttle = this.limiter(() => axios.get(authorRequestUrl))
return throttle().then((res) => res.data || [])
return this._processRequest(this.limiter(() => axios.get(authorRequestUrl)))
.then((res) => res.data || [])
.catch((error) => {
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
return []
@ -50,8 +50,8 @@ class Audnexus {
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
const throttle = this.limiter(() => axios.get(authorRequestUrl))
return throttle().then((res) => res.data)
return this._processRequest(this.limiter(() => axios.get(authorRequestUrl)))
.then((res) => res.data)
.catch((error) => {
Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
return null
@ -93,13 +93,35 @@ class Audnexus {
getChaptersByASIN(asin, region) {
Logger.debug(`[Audnexus] Get chapters for ASIN ${asin}/${region}`)
const throttle = this.limiter(() => axios.get(`${this.baseUrl}/books/${asin}/chapters?region=${region}`))
return throttle().then((res) => res.data)
return this._processRequest(this.limiter(() => axios.get(`${this.baseUrl}/books/${asin}/chapters?region=${region}`)))
.then((res) => res.data)
.catch((error) => {
Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}/${region}`, error)
return null
})
}
/**
* Internal method to process requests and retry if rate limit is exceeded.
*/
async _processRequest(request) {
try {
const response = await request()
return response
} catch (error) {
if (error.response?.status === 429) {
const retryAfter = parseInt(error.response.headers?.['retry-after'], 10) || 5
Logger.warn(`[Audnexus] Rate limit exceeded. Retrying in ${retryAfter} seconds.`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return this._processRequest(request)
}
throw error
}
}
}
module.exports = Audnexus
module.exports = Audnexus