Skip to content

Commit 252e83f

Browse files
committed
feat: Add rate limiting features
1 parent 988cf88 commit 252e83f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/contentful.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
* @see ContentfulClientAPI
66
*/
77

8+
import defaults from 'lodash/defaults'
89
import assign from 'lodash/assign'
10+
import cloneDeep from 'lodash/cloneDeep'
911
import version from '../version'
1012
import createHttpClient from 'contentful-sdk-core/create-http-client'
13+
import wrapHttpClient from 'contentful-sdk-core/wrap-http-client'
1114
import createContentfulApi from './create-contentful-api'
1215
import createLinkResolver from './create-link-resolver'
1316

@@ -23,6 +26,10 @@ import createLinkResolver from './create-link-resolver'
2326
* @prop {string=} params.host - API host (default: cdn.contentful.com). Also usable with preview.contentful.com.
2427
* @prop {Object=} params.agent - Optional Node.js HTTP agent for proxying (see <a href="https://nodejs.org/api/http.html#http_class_http_agent">Node.js docs</a> and <a href="https://www.npmjs.com/package/https-proxy-agent">https-proxy-agent</a>)
2528
* @prop {Object=} params.headers - Optional additional headers
29+
* @prop {number=} params.concurrency - Number of allowed concurrent requests. Changing this value is not recommended. (default: 6)
30+
* @prop {number=} params.delay - Delay in milliseconds for waiting after hitting the allowed number of concurrent requests. Changing this value is not recommended. (default: 1000)
31+
* @prop {number=} params.maxRetries - Maximum number of retries when a 429 is received (default: 5)
32+
* @prop {boolean=} params.retryOnTooManyRequests - If we should retry on 429s (default: true)
2633
* @returns {ContentfulClientAPI.ClientAPI}
2734
* @example
2835
* const client = contentful.createClient({
@@ -31,6 +38,13 @@ import createLinkResolver from './create-link-resolver'
3138
* })
3239
*/
3340
export default function createClient (axios, params) {
41+
params = defaults(cloneDeep(params), {
42+
rateLimit: 9,
43+
rateLimitPeriod: 1000,
44+
maxRetries: 5,
45+
retryOnTooManyRequests: true
46+
})
47+
3448
if (!params.accessToken) {
3549
throw new TypeError('Expected parameter accessToken')
3650
}
@@ -49,7 +63,13 @@ export default function createClient (axios, params) {
4963
'X-Contentful-User-Agent': 'contentful.js/' + version
5064
})
5165

52-
const http = createHttpClient(axios, params)
66+
const http = wrapHttpClient(createHttpClient(axios, params), {
67+
concurrency: params.rateLimit,
68+
delay: params.rateLimitPeriod,
69+
maxRetries: params.maxRetries,
70+
retryOnTooManyRequests: params.retryOnTooManyRequests
71+
})
72+
5373
return createContentfulApi({
5474
http: http,
5575
shouldLinksResolve: shouldLinksResolve

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
],
5252
"dependencies": {
5353
"babel-runtime": "^6.3.19",
54-
"contentful-sdk-core": "^2.2.2",
54+
"contentful-sdk-core": "^2.3.0",
5555
"json-stringify-safe": "^5.0.1",
5656
"lodash": "^4.2.0"
5757
},

0 commit comments

Comments
 (0)