def is_ratelimited(user, domain='all'):
 (list_key, set_key, blocking_key) = redis_key(user, domain)
 rules = _rules_for_user(user)
 if (len(rules) == 0):
  return (False, 0.0)
 with client.pipeline() as pipe:
  for (_, request_count) in rules:
   pipe.lindex(list_key, (request_count - 1))
  pipe.get(blocking_key)
  pipe.ttl(blocking_key)
  rule_timestamps = pipe.execute()
 blocking_ttl = rule_timestamps.pop()
 key_blocked = rule_timestamps.pop()
 if (key_blocked is not None):
  if (blocking_ttl is None):
   blocking_ttl = 0.5
  else:
   blocking_ttl = int(blocking_ttl)
  return (True, blocking_ttl)
 now = time.time()
 for (timestamp, (range_seconds, num_requests)) in zip(rule_timestamps, rules):
  if (timestamp is None):
   continue
  timestamp = float(timestamp)
  boundary = (timestamp + range_seconds)
  if (boundary > now):
   free = (boundary - now)
   return (True, free)
 return (False, 0.0)

def is_ratelimited ( user , domain = 'all' ) : ( list_key , set_key , blocking_key ) = redis_key ( user , domain ) rules = _rules_for_user ( user ) if ( len ( rules ) == 0 ) : return ( False , 0.0 ) with client . pipeline ( ) as pipe : for ( _ , request_count ) in rules : pipe . lindex ( list_key , ( request_count - 1 ) ) pipe . get ( blocking_key ) pipe . ttl ( blocking_key ) rule_timestamps = pipe . execute ( ) blocking_ttl = rule_timestamps . pop ( ) key_blocked = rule_timestamps . pop ( ) if ( key_blocked is not None ) : if ( blocking_ttl is None ) : blocking_ttl = 0.5 else : blocking_ttl = int ( blocking_ttl ) return ( True , blocking_ttl ) now = time . time ( ) for ( timestamp , ( range_seconds , num_requests ) ) in zip ( rule_timestamps , rules ) : if ( timestamp is None ) : continue timestamp = float ( timestamp ) boundary = ( timestamp + range_seconds ) if ( boundary > now ) : free = ( boundary - now ) return ( True , free ) return ( False , 0.0 )
reimplement ratelimit .