From e4c530dcc3e06e0890ea12f9454f343655fdfff3 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 12 Sep 2010 22:36:18 +0400 Subject: [PATCH] digest: add rate limit for headline requests when catching up; control OOE responses with seq numbers --- digest.js | 33 ++++++++++++++++++++++++++++++--- modules/backend-rpc.php | 3 +++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/digest.js b/digest.js index 5cf8d4ec9..cf816cf95 100644 --- a/digest.js +++ b/digest.js @@ -3,7 +3,9 @@ var last_feeds = []; var _active_feed_id = false; var _active_feed_offset = false; var _update_timeout = false; +var _view_update_timeout = false; var _feedlist_expanded = false; +var _update_seq = 1; function article_appear(article_id) { try { @@ -181,14 +183,23 @@ function remove_headline_entry(article_id) { } } +function view_update() { + try { + viewfeed(_active_feed_id, _active_feed_offset, false, true); + update(); + } catch (e) { + exception_error("view_update", e); + } +} + function view(article_id, dismiss_only) { try { remove_headline_entry(article_id); catchup_article(article_id, function() { - viewfeed(_active_feed_id, _active_feed_offset, false, true); - update(); + window.clearTimeout(_view_update_timeout); + _view_update_timeout = window.setTimeout("view_update()", 1000); }); return dismiss_only != true; @@ -210,8 +221,11 @@ function viewfeed(feed_id, offset, replace, no_effects) { if (replace == undefined) replace = (offset == 0); + _update_seq = _update_seq + 1; + var query = "backend.php?op=rpc&subop=digest-update&feed_id=" + - param_escape(feed_id) + "&offset=" + offset; + param_escape(feed_id) + "&offset=" + offset + + "&seq=" + _update_seq; console.log(query); @@ -222,6 +236,7 @@ function viewfeed(feed_id, offset, replace, no_effects) { if ($('H-LOADING-IMG')) Element.show("H-LOADING-IMG"); + new Ajax.Request("backend.php", { parameters: query, onComplete: function(transport) { @@ -465,6 +480,18 @@ function parse_headlines(transport, replace, no_effects) { try { if (!transport.responseXML) return; + var seq = transport.responseXML.getElementsByTagName('seq')[0]; + + if (seq) { + seq = seq.firstChild.nodeValue; + if (seq != _update_seq) { + console.log("parse_headlines: wrong sequence received."); + return; + } + } else { + return; + } + var headlines = transport.responseXML.getElementsByTagName('headlines')[0]; var headlines_title = transport.responseXML.getElementsByTagName('headlines-title')[0]; diff --git a/modules/backend-rpc.php b/modules/backend-rpc.php index 592c8ab28..aa05e8e8e 100644 --- a/modules/backend-rpc.php +++ b/modules/backend-rpc.php @@ -1003,11 +1003,14 @@ if ($subop == "digest-update") { $feed_id = db_escape_string($_REQUEST['feed_id']); $offset = db_escape_string($_REQUEST['offset']); + $seq = db_escape_string($_REQUEST['seq']); if (!$feed_id) $feed_id = -4; if (!$offset) $offset = 0; print ""; + print "$seq"; + $headlines = api_get_headlines($link, $feed_id, 10, $offset, '', ($feed_id == -4), true, false, "unread", "updated DESC");