Skip to content

Commit

Permalink
perf(replication): replication thread optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Aug 5, 2018
1 parent 015447c commit c5427a6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 169 deletions.
11 changes: 8 additions & 3 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const asyncForEach = require('./asyncForEach')

module.exports = async ({
sphinx,
sphinxSingle,
send,
recive,
p2p,
Expand Down Expand Up @@ -207,8 +208,12 @@ module.exports = async ({
p2p.on('randomTorrents', (nil, callback) => {
if(typeof callback != 'function')
return;

sphinx.query('SELECT * FROM `torrents` ORDER BY rand() limit 5', (error, torrents) => {

// ignore sql requests on closing
if(sphinxSingle.state === 'disconnected')
return

sphinxSingle.query('SELECT * FROM `torrents` ORDER BY rand() limit 5', (error, torrents) => {
if(!torrents || torrents.length == 0) {
callback(undefined)
return;
Expand All @@ -222,7 +227,7 @@ module.exports = async ({
}

const inSql = Object.keys(hashes).map(hash => sphinx.escape(hash)).join(',');
sphinx.query(`SELECT * FROM files WHERE hash IN(${inSql}) limit 50000`, (error, files) => {
sphinxSingle.query(`SELECT * FROM files WHERE hash IN(${inSql}) limit 50000`, (error, files) => {
if(!files)
{
files = []
Expand Down
100 changes: 54 additions & 46 deletions src/background/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,57 +122,65 @@ const pool = () => {
return expand(sphinx)
}

let mysqlSingle = {
_mysql: null
};
const proxySingle = new Proxy(mysqlSingle, {
get(target, prop) {
if(!target[prop])
{
let ret = target._mysql[prop]
if(typeof ret === 'function')
ret = ret.bind(target._mysql)
return ret
}
return target[prop]
}
})
const single = (callback) => {
mysqlSingle._mysql = mysql.createConnection({
host : config.sphinx.host,
port : config.sphinx.port
});
let mysqlSingle = {
_mysql: null
};

let promiseResolve;
const connectionPromise = new Promise((resolve) => {
promiseResolve = resolve
})
mysqlSingle.waitConnection = () => connectionPromise;
mysqlSingle._mysql.connect((mysqlError) => {
if (mysqlError) {
console.error('error connecting: ' + mysqlError.stack);
return;
const proxySingle = new Proxy(mysqlSingle, {
get(target, prop) {
if(!target[prop])
{
let ret = target._mysql[prop]
if(typeof ret === 'function')
ret = ret.bind(target._mysql)
return ret
}
return target[prop]
}

if(callback)
callback(proxySingle)
})

promiseResolve(proxySingle)
});

mysqlSingle._mysql.on('error', (err) => {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
mysqlSingle._mysql = undefined
single(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
const start = () =>
{
mysqlSingle._mysql = mysql.createConnection({
host : config.sphinx.host,
port : config.sphinx.port
});

let promiseResolve;
const connectionPromise = new Promise((resolve) => {
promiseResolve = resolve
})
mysqlSingle.waitConnection = () => connectionPromise;

mysqlSingle._mysql.connect((mysqlError) => {
if (mysqlError) {
console.error('error connecting: ' + mysqlError.stack);
return;
}

if(callback)
callback(proxySingle)

promiseResolve(proxySingle)
});

mysqlSingle._mysql.on('error', (err) => {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
console.log('restart single sql connection')
mysqlSingle._mysql = undefined
start(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});

mysqlSingle._mysql = expand(mysqlSingle._mysql)
return proxySingle
}

mysqlSingle._mysql = expand(mysqlSingle._mysql)
return proxySingle
return start()
}

module.exports = {pool, single}

0 comments on commit c5427a6

Please sign in to comment.