|
|
|
|
|
|
|
|
|
|
|
|
|
|
import fs from 'fs'; |
|
|
import path from 'path'; |
|
|
import inspect from 'rethinkdb-inspector'; |
|
|
import { statsd } from '../statsd'; |
|
|
|
|
|
const IS_PROD = !process.env.FORCE_DEV && process.env.NODE_ENV === 'production'; |
|
|
|
|
|
const CONNECTIONS = 20; |
|
|
const DEFAULT_CONFIG = { |
|
|
|
|
|
db: !process.env.TEST_DB ? 'spectrum' : 'testing', |
|
|
max: CONNECTIONS, |
|
|
buffer: CONNECTIONS, |
|
|
timeoutGb: 60 * 60 * 1000, |
|
|
timeout: 30, |
|
|
}; |
|
|
|
|
|
let ca; |
|
|
|
|
|
try { |
|
|
ca = fs.readFileSync(path.join(process.cwd(), 'cacert')); |
|
|
} catch (err) {} |
|
|
|
|
|
if (!ca && IS_PROD) |
|
|
throw new Error( |
|
|
'Please provide the SSL certificate to connect to the production database in a file called `cacert` in the root directory.' |
|
|
); |
|
|
|
|
|
const PRODUCTION_CONFIG = { |
|
|
servers: [ |
|
|
{ |
|
|
password: process.env.COMPOSE_RETHINKDB_PASSWORD, |
|
|
host: process.env.COMPOSE_RETHINKDB_URL, |
|
|
port: process.env.COMPOSE_RETHINKDB_PORT, |
|
|
...(ca |
|
|
? { |
|
|
ssl: { |
|
|
ca, |
|
|
}, |
|
|
} |
|
|
: {}), |
|
|
}, |
|
|
{ |
|
|
password: process.env.COMPOSE_RETHINKDB_PASSWORD, |
|
|
host: process.env.BACKUP_RETHINKDB_URL, |
|
|
port: process.env.BACKUP_RETHINKDB_PORT, |
|
|
...(ca |
|
|
? { |
|
|
ssl: { |
|
|
ca, |
|
|
}, |
|
|
} |
|
|
: {}), |
|
|
}, |
|
|
], |
|
|
}; |
|
|
|
|
|
const config = IS_PROD |
|
|
? { |
|
|
...DEFAULT_CONFIG, |
|
|
...PRODUCTION_CONFIG, |
|
|
} |
|
|
: { |
|
|
...DEFAULT_CONFIG, |
|
|
}; |
|
|
|
|
|
let r = require('rethinkhaberdashery')(config); |
|
|
const poolMaster = r.getPoolMaster(); |
|
|
|
|
|
poolMaster.on('queueing', size => { |
|
|
statsd.gauge('db.query_queue.size', size); |
|
|
}); |
|
|
|
|
|
setInterval(() => { |
|
|
statsd.gauge('db.connections.count', poolMaster.getLength()); |
|
|
}, 5000); |
|
|
|
|
|
|
|
|
if (process.env.TEST_DB) { |
|
|
poolMaster.on('healthy', healthy => { |
|
|
if (!healthy) { |
|
|
process.exit(1); |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
const queries = []; |
|
|
inspect(r, { |
|
|
onQueryComplete: (query, { size, time }) => { |
|
|
if (query.indexOf('.changes') > -1) return; |
|
|
statsd.increment('db.queries.count'); |
|
|
statsd.histogram('db.queries.response_time', time); |
|
|
statsd.histogram('db.queries.response_size', size); |
|
|
|
|
|
if (process.env.NODE_ENV === 'development' && process.env.TRACK_DB_PERF) { |
|
|
queries.push({ query, time, size }); |
|
|
fs.writeFileSync( |
|
|
'queries-by-time.js', |
|
|
JSON.stringify(queries.sort((a, b) => b.time - a.time), null, 2) |
|
|
); |
|
|
fs.writeFileSync( |
|
|
'queries-by-response-size.js', |
|
|
JSON.stringify(queries.sort((a, b) => b.size - a.size), null, 2) |
|
|
); |
|
|
} |
|
|
}, |
|
|
}); |
|
|
|
|
|
module.exports = { db: r }; |
|
|
|