Refactored http timeout handling.
This commit is contained in:
@@ -21,7 +21,7 @@ function logActive() {
|
||||
setTimeout(() => {
|
||||
log();
|
||||
logActive();
|
||||
}, 600000);
|
||||
}, argv.logActive || 60000);
|
||||
}
|
||||
|
||||
async function init() {
|
||||
|
||||
@@ -48,19 +48,31 @@ async function stashScene(sceneId, stashId, sessionUser) {
|
||||
await knex('stashes_scenes')
|
||||
.insert({
|
||||
stash_id: stash.id,
|
||||
actor_id: sceneId,
|
||||
scene_id: sceneId,
|
||||
});
|
||||
}
|
||||
|
||||
async function unstashActor(actorId, stashId, sessionUser) {
|
||||
await knex
|
||||
.from('stashes_actors')
|
||||
.whereIn('stashes_actors.id', knex('stashes_actors')
|
||||
.select('stashes_actors.id')
|
||||
.from('stashes_actors AS deletable')
|
||||
.where('deletable.actor_id', actorId)
|
||||
.where('deletable.stash_id', stashId)
|
||||
.whereExists(knex('stashes_actors') // verify user owns this stash
|
||||
.leftJoin('stashes', 'stashes.id', 'stashes_actors.stash_id')
|
||||
.where('stashes.user_id', sessionUser.id) // verify user owns this stash
|
||||
.where('stashes_actors.actor_id', actorId)
|
||||
.where('stashes_actors.stash_id', stashId))
|
||||
.where('stashes_actors.stash_id', knex.raw('deletable.stash_id'))
|
||||
.where('stashes.user_id', sessionUser.id))
|
||||
.delete();
|
||||
}
|
||||
|
||||
async function unstashScene(sceneId, stashId, sessionUser) {
|
||||
await knex
|
||||
.from('stashes_scenes AS deletable')
|
||||
.where('deletable.scene_id', sceneId)
|
||||
.where('deletable.stash_id', stashId)
|
||||
.whereExists(knex('stashes_scenes') // verify user owns this stash
|
||||
.leftJoin('stashes', 'stashes.id', 'stashes_scenes.stash_id')
|
||||
.where('stashes_scenes.stash_id', knex.raw('deletable.stash_id'))
|
||||
.where('stashes.user_id', sessionUser.id))
|
||||
.delete();
|
||||
}
|
||||
|
||||
@@ -68,6 +80,6 @@ module.exports = {
|
||||
curateStash,
|
||||
stashActor,
|
||||
stashScene,
|
||||
// unstashScene,
|
||||
unstashScene,
|
||||
unstashActor,
|
||||
};
|
||||
|
||||
@@ -80,20 +80,9 @@ function getLimiter(options = {}, url) {
|
||||
return limiters[interval][concurrency];
|
||||
}
|
||||
|
||||
async function request(method = 'get', url, body, requestOptions = {}, limiter, timeout) {
|
||||
async function request(method = 'get', url, body, requestOptions = {}, limiter) {
|
||||
const http = requestOptions.session || bhttp;
|
||||
|
||||
const options = {
|
||||
...defaultOptions,
|
||||
...requestOptions,
|
||||
headers: {
|
||||
...defaultOptions.headers,
|
||||
...requestOptions.headers,
|
||||
},
|
||||
responseTimeout: requestOptions.responseTimeout || requestOptions.timeout || defaultOptions.timeout,
|
||||
stream: !!requestOptions.destination,
|
||||
session: null,
|
||||
};
|
||||
const options = requestOptions;
|
||||
|
||||
const withProxy = useProxy(url);
|
||||
|
||||
@@ -107,8 +96,10 @@ async function request(method = 'get', url, body, requestOptions = {}, limiter,
|
||||
? http[method](url, body, options)
|
||||
: http[method](url, options));
|
||||
|
||||
timeout.cancel();
|
||||
return res;
|
||||
}
|
||||
|
||||
async function finalizeResult(res, options) {
|
||||
if (options.destination) {
|
||||
// res.on('progress', (bytes, totalBytes) => logger.silly(`Downloaded ${Math.round((bytes / totalBytes) * 100)}% of ${url}`));
|
||||
|
||||
@@ -140,21 +131,39 @@ async function request(method = 'get', url, body, requestOptions = {}, limiter,
|
||||
|
||||
function getTimeout(options, url) {
|
||||
return new Promise((resolve, reject, onCancel) => {
|
||||
const timeoutId = setTimeout(() => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error(`URL ${url} timed out`));
|
||||
}, (options?.timeout || defaultOptions.timeout) + 10000);
|
||||
|
||||
onCancel(() => clearTimeout(timeoutId));
|
||||
onCancel(() => {
|
||||
clearTimeout(timeout);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function scheduleRequest(method = 'get', url, body, options) {
|
||||
async function scheduleRequest(method = 'get', url, body, requestOptions = {}) {
|
||||
const options = {
|
||||
...defaultOptions,
|
||||
...requestOptions,
|
||||
headers: {
|
||||
...defaultOptions.headers,
|
||||
...requestOptions.headers,
|
||||
},
|
||||
responseTimeout: requestOptions.responseTimeout || requestOptions.timeout || defaultOptions.timeout,
|
||||
stream: !!requestOptions.destination,
|
||||
session: null,
|
||||
};
|
||||
|
||||
const limiter = getLimiter(options, url);
|
||||
const timeout = getTimeout(options, url);
|
||||
|
||||
const result = await limiter.schedule(() => Promise.race([request(method, url, body, options, limiter, timeout), timeout]));
|
||||
const result = await limiter.schedule(async () => Promise.race([request(method, url, body, options, limiter), timeout]));
|
||||
|
||||
return result;
|
||||
timeout.cancel();
|
||||
|
||||
const curatedResult = await finalizeResult(result, options);
|
||||
|
||||
return curatedResult;
|
||||
}
|
||||
|
||||
async function get(url, options) {
|
||||
|
||||
Reference in New Issue
Block a user