<s> import threading <EOL> import time <EOL> class RateLimitLock : <EOL> '''<STR_LIT>''' <EOL> MAX_DELAY = <NUM_LIT:5> * <NUM_LIT> <EOL> def __init__ ( self , delay = <NUM_LIT:1.0> ) : <EOL> '''<STR_LIT>''' <EOL> self . lock = threading . Lock ( ) <EOL> self . delay = delay <EOL> self . err_count = <NUM_LIT:0> <EOL> def __enter__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . lock . acquire ( ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( value , Exception ) : <EOL> self . err_count += <NUM_LIT:1> <EOL> else : <EOL> self . err_count = <NUM_LIT:0> <EOL> delay = min ( ( <NUM_LIT:2> ** self . err_count ) * self . delay , self . MAX_DELAY ) <EOL> if self . err_count > <NUM_LIT:0> : <EOL> print '<STR_LIT>' % delay <EOL> time . sleep ( delay ) <EOL> self . lock . release ( ) </s>
