#NEWLINE# def incr_ratelimit( user , domain = 'all' ) : #NEWLINE# #INDENT# 'Increases the rate-limit for the specified user' #NEWLINE# ( list_key , set_key , _ ) = redis_key ( user , domain ) #NEWLINE# now = time . time ( ) #NEWLINE# if ( len ( rules ) == 0 ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# with client . pipeline ( ) as pipe : #NEWLINE# #INDENT# count = 0 #NEWLINE# while True : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# pipe . watch ( list_key ) #NEWLINE# last_val = pipe . lindex ( list_key , ( max_api_calls ( user ) - 1 ) ) #NEWLINE# pipe . multi ( ) #NEWLINE# pipe . lpush ( list_key , now ) #NEWLINE# pipe . ltrim ( list_key , 0 , ( max_api_calls ( user ) - 1 ) ) #NEWLINE# pipe . zadd ( set_key , now , now ) #NEWLINE# if ( last_val is not None ) : #NEWLINE# #INDENT# pipe . zrem ( set_key , last_val ) #NEWLINE# #UNINDENT# api_window = max_api_window ( user ) #NEWLINE# pipe . expire ( list_key , api_window ) #NEWLINE# pipe . expire ( set_key , api_window ) #NEWLINE# pipe . execute ( ) #NEWLINE# break #NEWLINE# #UNINDENT# except redis . WatchError : #NEWLINE# #INDENT# if ( count > 10 ) : #NEWLINE# #INDENT# logging . error ( 'Failed to complete incr_ratelimit transaction without interference 10 times in a row! Aborting rate-limit increment' ) #NEWLINE# break #NEWLINE# #UNINDENT# count += 1 #NEWLINE# continue