get_updates: Replace "failures" with the new dont_block option.

(imported from commit 2b4ecb35c4f14b6c408323662ef6f39c6485c62d)
This commit is contained in:
Tim Abbott
2012-11-27 15:06:17 -05:00
parent a1d8e9d30f
commit d23a83ee81
3 changed files with 13 additions and 19 deletions

View File

@@ -63,8 +63,6 @@ class HumbugAPI(object):
if not (isinstance(val, str) or isinstance(val, unicode)): if not (isinstance(val, str) or isinstance(val, unicode)):
request[key] = simplejson.dumps(val) request[key] = simplejson.dumps(val)
request["failures"] = 0
while True: while True:
try: try:
res = requests.post(urlparse.urljoin(self.base_url, url), data=request, res = requests.post(urlparse.urljoin(self.base_url, url), data=request,
@@ -76,7 +74,7 @@ class HumbugAPI(object):
if not had_error_retry: if not had_error_retry:
sys.stdout.write("connection error %s -- retrying." % (res.status_code,)) sys.stdout.write("connection error %s -- retrying." % (res.status_code,))
had_error_retry = True had_error_retry = True
request["failures"] += 1 request["dont_block"] = simplejson.dumps(True)
else: else:
sys.stdout.write(".") sys.stdout.write(".")
sys.stdout.flush() sys.stdout.flush()
@@ -102,7 +100,7 @@ class HumbugAPI(object):
if not had_error_retry: if not had_error_retry:
sys.stdout.write("connection error -- retrying.") sys.stdout.write("connection error -- retrying.")
had_error_retry = True had_error_retry = True
request["failures"] += 1 request["dont_block"] = simplejson.dumps(True)
else: else:
sys.stdout.write(".") sys.stdout.write(".")
sys.stdout.flush() sys.stdout.flush()

View File

@@ -10,10 +10,10 @@ var selected_message = $(); /* = rows.get(selected_message_id) */
var get_updates_params = { var get_updates_params = {
last: -1, last: -1,
pointer: -1, pointer: -1,
failures: 0,
server_generation: -1, /* to be filled in on document.ready */ server_generation: -1, /* to be filled in on document.ready */
reload_pending: 0 reload_pending: 0
}; };
var get_updates_failures = 0;
// The "message groups", i.e. blocks of messages collapsed by recipient. // The "message groups", i.e. blocks of messages collapsed by recipient.
// Each message table has a list of lists. // Each message table has a list of lists.
@@ -559,7 +559,7 @@ function get_updates(options) {
get_updates_params.pointer = furthest_read; get_updates_params.pointer = furthest_read;
get_updates_params.reload_pending = Number(reload.is_pending()); get_updates_params.reload_pending = Number(reload.is_pending());
get_updates_params.dont_block = options.dont_block; get_updates_params.dont_block = options.dont_block || get_updates_failures > 0;
get_updates_xhr = $.ajax({ get_updates_xhr = $.ajax({
type: 'POST', type: 'POST',
@@ -578,7 +578,7 @@ function get_updates(options) {
return; return;
} }
get_updates_params.failures = 0; get_updates_failures = 0;
$('#connection-error').hide(); $('#connection-error').hide();
if (get_updates_params.server_generation === -1) { if (get_updates_params.server_generation === -1) {
@@ -614,19 +614,19 @@ function get_updates(options) {
error: function (xhr, error_type, exn) { error: function (xhr, error_type, exn) {
if (error_type === 'timeout') { if (error_type === 'timeout') {
// Retry indefinitely on timeout. // Retry indefinitely on timeout.
get_updates_params.failures = 0; get_updates_failures = 0;
$('#connection-error').hide(); $('#connection-error').hide();
} else { } else {
get_updates_params.failures += 1; get_updates_failures += 1;
} }
if (get_updates_params.failures >= 5) { if (get_updates_failures >= 5) {
$('#connection-error').show(); $('#connection-error').show();
} else { } else {
$('#connection-error').hide(); $('#connection-error').hide();
} }
var retry_sec = Math.min(90, Math.exp(get_updates_params.failures/2)); var retry_sec = Math.min(90, Math.exp(get_updates_failures/2));
get_updates_timeout = setTimeout(get_updates, retry_sec*1000); get_updates_timeout = setTimeout(get_updates, retry_sec*1000);
} }
}); });
@@ -741,7 +741,7 @@ setInterval(function () {
// Our app's JS wasn't running (the machine was probably // Our app's JS wasn't running (the machine was probably
// asleep). Now that we're running again, immediately poll for // asleep). Now that we're running again, immediately poll for
// new updates. // new updates.
get_updates_params.failures = 0; get_updates_failures = 0;
restart_get_updates(); restart_get_updates();
} }
watchdog_time = new_time; watchdog_time = new_time;

View File

@@ -331,13 +331,13 @@ def format_updates_response(messages=[], apply_markdown=True,
return ret return ret
def return_messages_immediately(user_profile, client_id, last, def return_messages_immediately(user_profile, client_id, last,
failures, client_server_generation, client_server_generation,
client_reload_pending, client_pointer, client_reload_pending, client_pointer,
dont_block, **kwargs): dont_block, **kwargs):
if last is None: if last is None:
# When an API user is first querying the server to subscribe, # When an API user is first querying the server to subscribe,
# there's no reason to reply immediately. # there's no reason to reply immediately.
# TODO: Make this work with server_generation/failures # TODO: Make this work with server_generation
return None return None
if UserMessage.objects.filter(user_profile=user_profile).count() == 0: if UserMessage.objects.filter(user_profile=user_profile).count() == 0:
@@ -378,9 +378,6 @@ def return_messages_immediately(user_profile, client_id, last,
new_pointer = ptr new_pointer = ptr
update_types.append("pointer_update") update_types.append("pointer_update")
if failures >= 1:
update_types.append("reset_failure_counter")
if update_types: if update_types:
return format_updates_response(messages=messages, return format_updates_response(messages=messages,
user_profile=user_profile, user_profile=user_profile,
@@ -409,13 +406,12 @@ def send_with_safety_check(response, handler, apply_markdown=True, **kwargs):
@has_request_variables @has_request_variables
def get_updates_backend(request, user_profile, handler, client_id, def get_updates_backend(request, user_profile, handler, client_id,
last = POST(converter=int, default=None), last = POST(converter=int, default=None),
failures = POST(converter=int, default=None),
client_server_generation = POST(whence='server_generation', default=None), client_server_generation = POST(whence='server_generation', default=None),
client_reload_pending = POST(whence='reload_pending', default=None), client_reload_pending = POST(whence='reload_pending', default=None),
client_pointer = POST(whence='pointer', converter=int, default=None), client_pointer = POST(whence='pointer', converter=int, default=None),
dont_block = POST(converter=simplejson.loads, default=False), dont_block = POST(converter=simplejson.loads, default=False),
**kwargs): **kwargs):
resp = return_messages_immediately(user_profile, client_id, last, failures, resp = return_messages_immediately(user_profile, client_id, last,
client_server_generation, client_server_generation,
client_reload_pending, client_reload_pending,
client_pointer, client_pointer,