[addon] adds request processing limiter

This commit is contained in:
TheBeastLT
2020-04-26 23:35:36 +02:00
parent ee24dbebb8
commit bd71311da6
3 changed files with 15 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
const Bottleneck = require('bottleneck');
const { addonBuilder } = require('stremio-addon-sdk'); const { addonBuilder } = require('stremio-addon-sdk');
const { Type } = require('./lib/types'); const { Type } = require('./lib/types');
const { manifest, DefaultProviders } = require('./lib/manifest'); const { manifest, DefaultProviders } = require('./lib/manifest');
@@ -14,16 +15,21 @@ const STALE_ERROR_AGE = 7 * 24 * 60 * 60; // 7 days
const defaultProviders = DefaultProviders.map(provider => provider.toLowerCase()); const defaultProviders = DefaultProviders.map(provider => provider.toLowerCase());
const builder = new addonBuilder(manifest()); const builder = new addonBuilder(manifest());
const limiter = new Bottleneck({
maxConcurrent: process.env.LIMIT_MAX_CONCURRENT || 20,
highWater: process.env.LIMIT_QUEUE_SIZE || 100,
strategy: Bottleneck.strategy.OVERFLOW
});
builder.defineStreamHandler((args) => { builder.defineStreamHandler((args) => {
if (!args.id.match(/tt\d+/i) && !args.id.match(/kitsu:\d+/i)) { if (!args.id.match(/tt\d+/i) && !args.id.match(/kitsu:\d+/i)) {
return Promise.resolve({ streams: [] }); return Promise.resolve({ streams: [] });
} }
return cacheWrapStream(args.id, () => streamHandler(args) return limiter.schedule(() => cacheWrapStream(args.id, () => streamHandler(args)
.then(records => records .then(records => records
.sort((a, b) => b.torrent.seeders - a.torrent.seeders || b.torrent.uploadDate - a.torrent.uploadDate) .sort((a, b) => b.torrent.seeders - a.torrent.seeders || b.torrent.uploadDate - a.torrent.uploadDate)
.map(record => toStreamInfo(record)))) .map(record => toStreamInfo(record)))))
.then(streams => filterByProvider(streams, args.extra.providers || defaultProviders)) .then(streams => filterByProvider(streams, args.extra.providers || defaultProviders))
.then(streams => applySorting(streams, args.extra)) .then(streams => applySorting(streams, args.extra))
.then(streams => applyMochs(streams, args.extra)) .then(streams => applyMochs(streams, args.extra))
@@ -35,7 +41,7 @@ builder.defineStreamHandler((args) => {
})) }))
.catch(error => { .catch(error => {
console.log(`Failed request ${args.id}: ${error}`); console.log(`Failed request ${args.id}: ${error}`);
throw error; throw Promise.reject(error);
}); });
}); });

View File

@@ -188,6 +188,11 @@
"type-is": "~1.6.17" "type-is": "~1.6.17"
} }
}, },
"bottleneck": {
"version": "2.19.5",
"resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz",
"integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw=="
},
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",

View File

@@ -8,6 +8,7 @@
"author": "TheBeastLT <pauliox@beyond.lt>", "author": "TheBeastLT <pauliox@beyond.lt>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"bottleneck": "^2.19.5",
"cache-manager": "^2.11.1", "cache-manager": "^2.11.1",
"cache-manager-mongodb": "^0.2.2", "cache-manager-mongodb": "^0.2.2",
"express-rate-limit": "^5.1.1", "express-rate-limit": "^5.1.1",