Skip to content

Commit

Permalink
Adds retry jitter to the retry for 429 requests (#593)
Browse files Browse the repository at this point in the history
Co-authored-by: michelle <Michelle Wan>
  • Loading branch information
mwan-ep committed Feb 25, 2022
1 parent e13c9af commit 2c1d230
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Config {
reauth,
protocol,
store_id,
retryDelay
retryDelay,
retryJitter
} = options

this.application = application
Expand All @@ -45,6 +46,7 @@ class Config {
this.disableCart = disableCart || false
this.reauth = reauth || true
this.retryDelay = retryDelay || 1000
this.retryJitter = retryJitter || 500
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/factories/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ const fetchRetry = (
)
.then(result => resolve(result))
.catch(error => reject(error)),
attempt * config.retryDelay
attempt * config.retryDelay +
Math.floor(Math.random() * config.retryJitter)
)
} else {
reject(response.json)
Expand Down
4 changes: 3 additions & 1 deletion src/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ConfigOptions {
disableCart?: Boolean
reauth?: Boolean,
retryDelay?: number
retryJitter?: number
}

export interface Config {
Expand All @@ -57,7 +58,8 @@ export interface Config {
language: 'JS'
}
storage?: StorageFactory
retryDelay: number
retryDelay?: number
retryJitter?: number

constructor(options: ConfigOptions): void
}
Expand Down
3 changes: 2 additions & 1 deletion test/unit/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const apiUrl = 'https://api.moltin.com/v2'
describe('Moltin error handling', () => {
const Moltin = MoltinGateway({
client_id: 'XXX',
retryDelay: 10 // Reduce retryDelay for retries during testing
retryDelay: 10, // Reduce retryDelay/retryJitter for retries during testing
retryJitter: 1
})

it('should handle a 429 correctly', () => {
Expand Down

0 comments on commit 2c1d230

Please sign in to comment.