Package: rlim
Install$ pip install rlimBasic UsageCreate and use aRateLimiterinstance:@RateLimiter(Rate(2),Limit(50,40))deff():...@RateLimiter(Rate(2),Limit(50,40))asyncdeff():...Apply aRateLimiterinstance to a function decorated withplaceholder:@placeholderdeff():...rl_set(f,RateLimiter(Rate(2),Limit(50,40)))@placeholderasyncdeff():...rl_set(f,RateLimiter(Rate(2),Limit(50,40)))Use an instance as a context manager:rl=RateLimiter(Rate(2),Limit(50,40))deff():withrl:...asyncdeff():asyncwithrl:...Notice that in the above,RateandLimitare two distinct types.Rateis used to define a constant calling speed - for example,Rate(2)would equate to1call every0.5seconds.Limitis used to define a maximum number of calls (atanyspeed) within a certain period of time (sliding window) - for example,Limit(50, 40)would mean the user could make calls at any speed, so long as they don't surpass 50 calls within the last 40 seconds. Together, this means the user can many calls at a max speed of0.5s/call, and must stay below (or equal to)50calls in the past40seconds.BundlesBundles allow you to bundle together numerous rate limiters, with methods for applying them to the methods of a given class or class instance. When a bundle is applied to a class instance, theRateLimiterinstances (or copies of them, if desired) within the bundle will be applied to each of the class's methods upon class instantiation.Creating aBundle:bdl=Bundle(fn1=RateLimiter(...),fn2=RateLimiter(...),...)Applying aBundleinstance to a class:@bdlclassExample:deffn1(self)->None:returndeffn2(self)->None:returnNow, when you create an instance ofExample,fn1andfn2will have their correspondingRateLimiterinstances applied to them.Additional Functions/MethodsRateLimiter.copy(**overrides)->RateLimiterCreate a copy of theRateLimiterinstance with optional overrides (that will be passed intoRateLimiter.__init__).RateLimiter.apply(func:Callable[_P,_R_co])->Callable[_P,_R_co]RateLimiter.apply(func:Callable[_P,Awaitable[_R_co]])->Callable[_P,Awaitable[_R_co]]Manually wrap the given function to use theRateLimiterinstance for rate limiting.RateLimiter.__call__(the function that makes it possible to decorate another function with aRateLimiterinstance) is simply an alias ofRateLimiter.apply.Bundle.apply(inst:object,ignore:bool=MISSING,copy:bool=MISSING,**overrides)->NoneApply theRateLimiterinstances in thisBundleto the given class instance.ignore(defaultFalse) will make it soRateLimiterErrorwill not be raised if a function in the decorated class does not have a correspondingRateLimiter.copy(defaultTrue) will make it so copies of theRateLimiterinstances are applied, instead of the same instances.**overridesare keyword overrides that will be passed intoRateLimiter.copy(only ifcopyisTrue).Bundle.decorate(ignore:bool=MISSING,copy:bool=MISSING,**overrides)->Callable[[Type[T]],Callable[_P,T]]This function allows the user to have more control over how theRateLimiterinstances are applied to the decorated class's methods. It returns a decorator.ignore,copy, and**overrideswill be passed intoBundle.apply.Bundle.bake(ignore:bool=MISSING,copy:bool=MISSING,**overrides)->NoneBake arguments for calls toBundle.applyandBundle.decorateinto theBundleinstance. Any arguments provided toBundle.applyorBundle.decoratehave precedence over baked arguments.