From f0d80e25c0f32891419ed3883a31604b9022b7f0 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sun, 20 Mar 2016 18:44:28 -0300 Subject: [PATCH] reuse the same mechanism that limits parallel dhtgets also for decodeshorturl --- js/interface_common.js | 19 ++++++++------- js/twister_io.js | 53 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/js/interface_common.js b/js/interface_common.js index 2916102..f048ae0 100644 --- a/js/interface_common.js +++ b/js/interface_common.js @@ -832,16 +832,17 @@ function fetchShortenedURI(req) { console.warn('can\'t fetch URI "' + req + '": daemon is obsolete, version 0.9.35 or higher is required'); return; } - twisterRpc('decodeshorturl', [req], + + decodeshorturl( req, function (req, ret) { - twister.URIs[req] = ret; - $.localStorage.set('twistaURIs', twister.URIs); - applyShortenedURI(req, ret); - }, req, - function (req, ret) { - console.warn('can\'t fetch URI "' + req + '": ' + ret.message); - }, req - ); + if( ret ) { + twister.URIs[req] = ret; + $.localStorage.set('twistaURIs', twister.URIs); + applyShortenedURI(req, ret); + } else { + console.warn('can\'t fetch URI "' + req + '": ' + ret.message); + } + }, req); } function applyShortenedURI(short, long) { diff --git a/js/twister_io.js b/js/twister_io.js index adbe1b3..16a9594 100644 --- a/js/twister_io.js +++ b/js/twister_io.js @@ -55,7 +55,10 @@ function _dhtgetProcessPending(locator, multi, ret) var cbFunc = _dhtgetPendingMap[locator][i].cbFunc; var cbArg = _dhtgetPendingMap[locator][i].cbArg; - if( multi == 's' ) { + if( multi == 'url' ) { + // here is decodeshorturl case + cbFunc(cbArg, ret); + } else if( multi == 's' ) { if( ret[0] != undefined ) { cbFunc(cbArg, ret[0]["p"]["v"], ret); } else { @@ -110,6 +113,25 @@ function dhtget( username, resource, multi, cbFunc, cbArg, timeoutArgs ) { } } +// decode shortened url +// the expanded url is returned to callback +// null is passed to callback in case of an error +function decodeshorturl( locator, cbFunc, cbArg, timeoutArgs ) { + if( locator in _dhtgetPendingMap) { + _dhtgetAddPending(locator, cbFunc, cbArg); + } else { + _dhtgetAddPending(locator, cbFunc, cbArg); + // limit the number of simultaneous decodeshorturl's and dhtgets. + // this should leave some sockets for other non-blocking daemon requests. + if( _dhtgetsInProgress < _maxDhtgets ) { + _decodeshorturlInternal( locator, timeoutArgs ); + } else { + // just queue the locator. it will be unqueue when some dhtget completes. + _queuedDhtgets.push(locator); + } + } +} + function _dhtgetInternal( username, resource, multi, timeoutArgs ) { var locator = _dhtgetLocator(username, resource, multi); _dhtgetsInProgress++; @@ -131,10 +153,35 @@ function _dhtgetInternal( username, resource, multi, timeoutArgs ) { }, locator); } +function _decodeshorturlInternal( locator, timeoutArgs ) { + _dhtgetsInProgress++; + argsList = [locator]; + if( typeof timeoutArgs !== 'undefined' ) { + argsList = argsList.concat(timeoutArgs); + } + twisterRpc("decodeshorturl", argsList, + function(args, ret) { + _dhtgetsInProgress--; + _dhtgetProcessPending(args.locator, "url", ret); + _dhtgetDequeue(); + }, {locator:locator}, + function(cbArg, ret) { + console.log("ajax error:" + ret); + _dhtgetsInProgress--; + _dhtgetAbortPending(locator); + _dhtgetDequeue(); + }, locator); +} + function _dhtgetDequeue() { if( _queuedDhtgets.length ) { - var locatorSplit = _queuedDhtgets.pop().split(";"); - _dhtgetInternal(locatorSplit[0], locatorSplit[1], locatorSplit[2]); + var locator = _queuedDhtgets.pop(); + var locatorSplit = locator.split(";"); + if( locatorSplit.length == 3) { + _dhtgetInternal(locatorSplit[0], locatorSplit[1], locatorSplit[2]); + } else { + _decodeshorturlInternal( locator ) + } } }