Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
address performance issues with many tabs and publisher tracking
Browse files Browse the repository at this point in the history
Fixes #4274
  • Loading branch information
mrose17 committed Nov 18, 2016
1 parent 9653daf commit 464e0ba
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const clientOptions = { debugP: process.env.LEDGER_DEBUG,
server: process.env.LEDGER_SERVER_URL
}

var doneTimer

/*
* publisher globals
*/
Expand Down Expand Up @@ -199,11 +201,15 @@ var init = () => {

appDispatcher.register(doAction)
initialize(getSetting(settings.PAYMENTS_ENABLED))

doneTimer = setInterval(doneWriter, 1 * msecs.hour)
} catch (ex) { console.log('ledger.js initialization failed: ' + ex.toString() + '\n' + ex.stack) }
}

var quit = () => {
visit('NOOP', underscore.now(), null)
clearInterval(doneTimer)
doneWriter()
}

var boot = () => {
Expand Down Expand Up @@ -388,7 +394,8 @@ eventStore.addChangeListener(() => {

if ((!synopsis) || (!util.isArray(info))) return

info.forEach((page) => {
// NB: in theory we have already seen every element in info except for (perhaps) the last one...
underscore.rest(info, info.length - 1).forEach((page) => {
var entry, faviconURL, publisher, siteSetting
var location = page.url

Expand Down Expand Up @@ -466,7 +473,7 @@ eventStore.addChangeListener(() => {
faviconURL = page.faviconURL || entry.protocol + '//' + url.parse(location).host + '/favicon.ico'
entry.faviconURL = null

if (publisherInfo._internal.debugP) console.log('request: ' + faviconURL)
if (publisherInfo._internal.debugP) console.log('\nrequest: ' + faviconURL)
fetch(faviconURL)
}
})
Expand Down Expand Up @@ -535,7 +542,7 @@ var initialize = (paymentsEnabled) => {
}
getStateInfo(stateResult)

syncWriter(pathName(statePath), stateResult, () => {})
syncWriter(pathName(statePath), stateResult, { flushP: true }, () => {})
})
}
} catch (ex) {
Expand All @@ -561,10 +568,8 @@ var enable = (paymentsEnabled) => {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
}

if (!paymentsEnabled) {
synopsis = null
return updatePublisherInfo()
}
publisherInfo._internal.enabled = paymentsEnabled
if (synopsis) return updatePublisherInfo()

synopsis = new (ledgerPublisher.Synopsis)()
fs.readFile(pathName(synopsisPath), (err, data) => {
Expand Down Expand Up @@ -658,6 +663,8 @@ var publisherInfo = {
synopsis: undefined,

_internal: {
enabled: false,

ruleset: { raw: [], cooked: [] }
}
}
Expand All @@ -666,8 +673,6 @@ var updatePublisherInfo = () => {
var data = {}
var then = underscore.now() - msecs.week

if (!synopsis) return

underscore.keys(publishers).sort().forEach((publisher) => {
var entries = []

Expand All @@ -683,8 +688,9 @@ var updatePublisherInfo = () => {
syncWriter(pathName(scoresPath), synopsis.allN(), () => {})

syncWriter(pathName(synopsisPath), synopsis, () => {})
publisherInfo.synopsis = synopsisNormalizer()
if (!publisherInfo._internal.enabled) return

publisherInfo.synopsis = synopsisNormalizer()
publisherInfo.synopsisOptions = synopsis.options

if (publisherInfo._internal.debugP) {
Expand Down Expand Up @@ -1064,7 +1070,7 @@ var callback = (err, result, delayTime) => {
}
cacheRuleSet(result.ruleset)

syncWriter(pathName(statePath), result, () => {})
syncWriter(pathName(statePath), result, { flushP: true }, () => {})
run(delayTime)
}

Expand Down Expand Up @@ -1160,7 +1166,7 @@ var run = (delayTime) => {
result = client.vote(winner)
if (result) state = result
})
if (state) syncWriter(pathName(statePath), state, () => {})
if (state) syncWriter(pathName(statePath), state, { flushP: true }, () => {})
} catch (ex) {
console.log('ledger client error(2): ' + ex.toString() + (ex.stack ? ('\n' + ex.stack) : ''))
}
Expand Down Expand Up @@ -1205,7 +1211,6 @@ var getStateInfo = (state) => {
var then = underscore.now() - msecs.year

ledgerInfo.paymentId = state.properties.wallet.paymentId
ledgerInfo.address = state.properties.wallet.address
ledgerInfo.passphrase = state.properties.wallet.keychains.passphrase

ledgerInfo.minDuration = synopsis.options.minDuration
Expand Down Expand Up @@ -1373,7 +1378,7 @@ var setPaymentInfo = (amount) => {
client.setBraveryProperties(bravery, (err, result) => {
if (err) return console.log('ledger setBraveryProperties: ' + err.toString())

if (result) syncWriter(pathName(statePath), result, () => {})
if (result) syncWriter(pathName(statePath), result, { flushP: true }, () => {})
})
if (ledgerInfo.created) getPaymentInfo()
}
Expand Down Expand Up @@ -1414,7 +1419,7 @@ var networkConnected = underscore.debounce(() => {
if (client.sync(callback) === true) run(random.randomInt({ min: msecs.minute, max: 10 * msecs.minute }))

if (balanceTimeoutId) clearTimeout(balanceTimeoutId)
balanceTimeoutId = setTimeout(getBalance, 1 * msecs.second)
balanceTimeoutId = setTimeout(getBalance, 5 * msecs.second)
}, 1 * msecs.minute, true)

/*
Expand All @@ -1430,20 +1435,21 @@ var syncWriter = (path, obj, options, cb) => {
}
options = underscore.defaults(options || {}, { encoding: 'utf8', mode: parseInt('644', 8) })

if ((!options.flushP) && (!syncingP[path])) syncingP[path] = true
if (syncingP[path]) {
syncingP[path] = { obj: obj, options: options, cb: cb }
if (ledgerInfo._internal.debugP) console.log('deferring ' + path)
if (ledgerInfo._internal.debugP) console.log('\ndeferring ' + path)
return
}
syncingP[path] = true

if (ledgerInfo._internal.debugP) console.log('writing ' + path)
if (ledgerInfo._internal.debugP) console.log('\nwriting ' + path)
fs.writeFile(path, JSON.stringify(obj, null, 2), options, (err) => {
var deferred = syncingP[path]

delete syncingP[path]
if (typeof deferred === 'object') {
if (ledgerInfo._internal.debugP) console.log('restarting ' + path)
if (ledgerInfo._internal.debugP) console.log('\nrestarting ' + path)
syncWriter(path, deferred.obj, deferred.options, deferred.cb)
}

Expand All @@ -1453,6 +1459,19 @@ var syncWriter = (path, obj, options, cb) => {
})
}

var doneWriter = () => {
underscore.keys(syncingP).forEach((path) => {
var deferred = syncingP[path]

if (typeof deferred !== 'object') return

delete syncingP[path]
if (ledgerInfo._internal.debugP) console.log('\nflushing ' + path)
deferred.options.flushP = true
syncWriter(path, deferred.obj, deferred.options, deferred.cb)
})
}

var pathName = (name) => {
var parts = path.parse(name)

Expand Down

0 comments on commit 464e0ba

Please sign in to comment.