Thank you. And for completeness sake, here is the full function I'm using, just in case something else is wrong. Rather than calling the capabilities APIs on every hit, I cache the value in a global variable and check every 30 seconds.
# Store in a global variable so it's cached between requests.
capabilities_check_time = None;
datastore_writes_enabled = True;
def in_read_only_mode():
"""We return True if we're in read-only mode or going to read-only one in
30 seconds or less. But we check the APIs every once in a while to avoid
the extra load that causes."""
global capabilities_check_time, datastore_writes_enabled
buffer_in_seconds = 30 # consider us in read-only mode this much time before we actually get there
check_period_in_seconds = 30 # time between checking the capabilities APIs
now = datetime.datetime.now()
if capabilities_check_time and capabilities_check_time > now:
# we don't need to check the APIs we have enough info cached
return not(datastore_writes_enabled)
else:
# we need to check the capabilities
# todo: check we check memcache as well?
datastore_writes.will_remain_enabled_for(buffer_in_seconds + check_period_in_seconds)
capabilities_check_time = now + datetime.timedelta(seconds=check_period_in_seconds)
return not(datastore_writes_enabled)