Added harder timeouts to http utility. Split owner and query database users.
This commit is contained in:
parent
99cfd3dc3f
commit
cb447da7d0
|
@ -1,9 +1,17 @@
|
|||
module.exports = {
|
||||
database: {
|
||||
host: '127.0.0.1',
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
database: 'traxxx',
|
||||
owner: {
|
||||
host: '127.0.0.1',
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
database: 'traxxx',
|
||||
},
|
||||
query: {
|
||||
host: '127.0.0.1',
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
database: 'traxxx',
|
||||
},
|
||||
},
|
||||
web: {
|
||||
host: '0.0.0.0',
|
||||
|
|
|
@ -3,6 +3,6 @@
|
|||
const config = require('config');
|
||||
|
||||
module.exports = {
|
||||
client: 'pg',
|
||||
connection: config.database,
|
||||
client: 'pg',
|
||||
connection: config.database.owner,
|
||||
};
|
||||
|
|
|
@ -5,7 +5,7 @@ const knex = require('knex');
|
|||
|
||||
module.exports = knex({
|
||||
client: 'pg',
|
||||
connection: config.database,
|
||||
connection: config.database.owner,
|
||||
// performance overhead, don't use asyncStackTraces in production
|
||||
asyncStackTraces: process.env.NODE_ENV === 'development',
|
||||
// debug: process.env.NODE_ENV === 'development',
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
const Promise = require('bluebird');
|
||||
const bhttp = require('bhttp');
|
||||
const util = require('util');
|
||||
const stream = require('stream');
|
||||
|
@ -15,7 +16,12 @@ const argv = require('../argv');
|
|||
const pipeline = util.promisify(stream.pipeline);
|
||||
const limiters = {};
|
||||
|
||||
Promise.config({
|
||||
cancellation: true,
|
||||
});
|
||||
|
||||
const defaultOptions = {
|
||||
timeout: 60000,
|
||||
encodeJSON: true,
|
||||
headers: {
|
||||
'user-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1',
|
||||
|
@ -67,6 +73,7 @@ function getLimiter(options = {}, url) {
|
|||
limiters[interval][concurrency] = new Bottleneck({
|
||||
minTime: interval,
|
||||
maxConcurrent: concurrency,
|
||||
timeout: (options.timeout || defaultOptions.timeout) + 10000, // timeout 10 seconds after bhttp should
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -83,7 +90,7 @@ async function request(method = 'get', url, body, requestOptions = {}, limiter)
|
|||
...defaultOptions.headers,
|
||||
...requestOptions.headers,
|
||||
},
|
||||
responseTimeout: requestOptions.responseTimeout || requestOptions.timeout || 60000,
|
||||
responseTimeout: requestOptions.responseTimeout || requestOptions.timeout || defaultOptions.timeout,
|
||||
stream: !!requestOptions.destination,
|
||||
session: null,
|
||||
};
|
||||
|
@ -131,10 +138,25 @@ async function request(method = 'get', url, body, requestOptions = {}, limiter)
|
|||
};
|
||||
}
|
||||
|
||||
function getTimeout(options, url) {
|
||||
return new Promise((resolve, reject, onCancel) => {
|
||||
const timeoutId = setTimeout(() => {
|
||||
reject(new Error(`URL ${url} timed out`));
|
||||
}, (options.timeout || defaultOptions.timeout) + 10000);
|
||||
|
||||
onCancel(() => clearTimeout(timeoutId));
|
||||
});
|
||||
}
|
||||
|
||||
async function scheduleRequest(method = 'get', url, body, options) {
|
||||
const limiter = getLimiter(options, url);
|
||||
const timeout = getTimeout(options, url);
|
||||
|
||||
return limiter.schedule(() => request(method, url, body, options, limiter));
|
||||
const result = await limiter.schedule(() => Promise.race([request(method, url, body, options, limiter), timeout]));
|
||||
|
||||
timeout.cancel();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function get(url, options) {
|
||||
|
|
|
@ -10,7 +10,7 @@ const PgOrderByRelatedPlugin = require('@graphile-contrib/pg-order-by-related');
|
|||
|
||||
const { ActorPlugins, SitePlugins, ReleasePlugins } = require('./plugins/plugins');
|
||||
|
||||
const connectionString = `postgres://${config.database.user}:${config.database.password}@${config.database.host}:5432/${config.database.database}`;
|
||||
const connectionString = `postgres://${config.database.query.user}:${config.database.query.password}@${config.database.query.host}:5432/${config.database.query.database}`;
|
||||
|
||||
module.exports = postgraphile(
|
||||
connectionString,
|
||||
|
|
Loading…
Reference in New Issue