'Returns the amount of time (a float in seconds) that the app
should sleep.
:param key: a memcache key
:param max_rate: maximum rate allowed in requests per second
:raises MaxSleepTimeHitError: if max sleep time is exceeded.'

def _get_sleep_time(self, key, max_rate):

    try:
        now_m = int(round((time.time() * self.clock_accuracy)))
        time_per_request_m = int(round((self.clock_accuracy / max_rate)))
        running_time_m = self.memcache_client.incr(key, delta=time_per_request_m)
        need_to_sleep_m = 0
        if ((now_m - running_time_m) > (self.rate_buffer_seconds * self.clock_accuracy)):
            next_avail_time = int((now_m + time_per_request_m))
            self.memcache_client.set(key, str(next_avail_time), serialize=False)
        else:
            need_to_sleep_m = max(((running_time_m - now_m) - time_per_request_m), 0)
        max_sleep_m = (self.max_sleep_time_seconds * self.clock_accuracy)
        if ((max_sleep_m - need_to_sleep_m) <= (self.clock_accuracy * 0.01)):
            self.memcache_client.decr(key, delta=time_per_request_m)
            raise MaxSleepTimeHitError(('Max    Sleep    Time    Exceeded:    %.2f' % (float(need_to_sleep_m) / self.clock_accuracy)))
        return (float(need_to_sleep_m) / self.clock_accuracy)
    except MemcacheConnectionError:
        return 0
