diff --git a/documentation.md b/documentation.md index a876142..bb2af3a 100644 --- a/documentation.md +++ b/documentation.md @@ -634,6 +634,15 @@ extend: true # defaults to 10 _parallel: 20 + +# throttle API request to gitlab to max at throttleMaxRequestsPerInterval per throttleInterval (ms) +# defaults to 10 +throttleMaxRequestsPerInterval: 10 +# throttle API request to gitlab to max at throttleMaxRequestsPerInterval per throttleInterval (ms) +# defaults to 1000 +throttleInterval: 1000 + + # Change rows per page (max. 100) # defaults to 100 _perPage: 100 diff --git a/src/include/config.js b/src/include/config.js index 22a28d3..a34150e 100755 --- a/src/include/config.js +++ b/src/include/config.js @@ -43,7 +43,9 @@ const defaults = { _parallel: 10, _verbose: false, _checkToken: true, - _skipDescriptionParsing: false + _skipDescriptionParsing: false, + throttleMaxRequestsPerInterval: 10, + throttleInterval: 1000, }; /** diff --git a/src/models/base.js b/src/models/base.js index d33e09b..8c13839 100755 --- a/src/models/base.js +++ b/src/models/base.js @@ -1,19 +1,27 @@ import request from 'request-promise-native'; -import url from 'url'; import async from 'async'; import crypto from 'crypto'; import throttleFactory from 'throttled-queue'; -const throttle = throttleFactory(10, 1000); /** * base model */ class base { + static throttle; + + static init(config) { + if(base.throttle == undefined){ + base.throttle = throttleFactory(config.data.throttleMaxRequestsPerInterval, config.data.throttleInterval); + } + } + + /** * construct * @param config */ constructor(config) { + base.init(config); this.config = config; this.url = config.get('url').endsWith('/') ? config.get('url') : `${config.get('url')}/`; @@ -37,7 +45,7 @@ class base { data.private_token = this.token; - return new Promise((resolve, reject) => throttle(() => { + return new Promise((resolve, reject) => base.throttle(() => { request.post(`${this.url}${path}`, { json: true, body: data, @@ -68,7 +76,7 @@ class base { let key = base.createDumpKey(path, data); if (this.config.dump) return this.getDump(key); - return new Promise((resolve, reject) => throttle(() => { + return new Promise((resolve, reject) => base.throttle(() => { request.post(`${path}`, { json: true, body: data, @@ -101,7 +109,7 @@ class base { path += (path.includes('?') ? '&' : '?') + `private_token=${this.token}`; path += `&page=${page}&per_page=${perPage}`; - return new Promise((resolve, reject) => throttle(() => { + return new Promise((resolve, reject) => base.throttle(() => { request(`${this.url}${path}`, { json: true, insecure: this._insecure,