Adding throttle to http request node (#1095)

Works by setting a timeout between each batch of requests.
Batch size and interval are configurable.
This commit is contained in:
Omar Ajoue 2020-10-26 09:27:12 -03:00 committed by GitHub
parent 134c49dfd8
commit 26de4a86c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -335,6 +335,26 @@ export class HttpRequest implements INodeType {
default: 10000, default: 10000,
description: 'Time in ms to wait for the server to send response headers (and start the response body) before aborting the request.', description: 'Time in ms to wait for the server to send response headers (and start the response body) before aborting the request.',
}, },
{
displayName: 'Batch size',
name: 'batchSize',
type: 'number',
typeOptions: {
minValue: -1
},
default: 50,
description: 'Input will be split in batches to throttle requests. -1 for disabled. 0 will be treated as 1.',
},
{
displayName: 'Batch interval',
name: 'batchInterval',
type: 'number',
typeOptions: {
minValue: 0
},
default: 1000,
description: 'Time (in milliseconds) between each batch of requests. 0 for disabled.',
},
], ],
}, },
@ -626,6 +646,14 @@ export class HttpRequest implements INodeType {
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject; const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
const url = this.getNodeParameter('url', itemIndex) as string; const url = this.getNodeParameter('url', itemIndex) as string;
if (itemIndex > 0 && options.batchSize as number >= 0 && options.batchInterval as number > 0) {
// defaults batch size to 1 of it's set to 0
const batchSize: number = options.batchSize as number > 0 ? options.batchSize as number : 1;
if (itemIndex % batchSize === 0) {
await new Promise(resolve => setTimeout(resolve, options.batchInterval as number))
}
}
const fullResponse = !!options.fullResponse as boolean; const fullResponse = !!options.fullResponse as boolean;
requestOptions = { requestOptions = {