You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently working with the slowapi package, trying to setup some dynamic limits that depend on the headers of the incoming requests. (Concretely, when a request with the proper header is there, I want to apply a very high limit instead of the usual one.)
So I tried to provide a list of callables to the default_limits argument in the constructor of the Limiter object. (see snippet below)
Turned out this broke the whole thing so I dug down in the source code to find out why. And I finally found out the break came from the LimitGroup.__iter__ method, in the case there was a limite that was callable AND had a key argument. (see code below)
# slowapi/wrappers.pyifcallable(self.__limit_provider):
if"key"ininspect.signature(self.__limit_provider).parameters.keys():
assert (
"request"ininspect.signature(self.key_function).parameters.keys()
), f"Limit provider function {self.key_function.__name__} needs a `request` argument"ifself.requestisNone:
raiseException("`request` object can't be None") # CODE IS BREAKING HERElimit_raw=self.__limit_provider(self.key_function(self.request))
else:
limit_raw=self.__limit_provider()
So I dug more, and I found out why self.request was None at that time.
I seems the LimitGroup.request is only set by the with_request method (see below)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am currently working with the
slowapi
package, trying to setup some dynamic limits that depend on the headers of the incoming requests. (Concretely, when a request with the proper header is there, I want to apply a very high limit instead of the usual one.)So I tried to provide a list of callables to the
default_limits
argument in the constructor of theLimiter
object. (see snippet below)Turned out this broke the whole thing so I dug down in the source code to find out why. And I finally found out the break came from the
LimitGroup.__iter__
method, in the case there was a limite that was callable AND had akey
argument. (see code below)So I dug more, and I found out why
self.request
wasNone
at that time.I seems the
LimitGroup.request
is only set by thewith_request
method (see below)And this one is only called if the
in_middleware
argument of theLimiter._check_request_limit
isTrue
.So I got two questions:
default_limits
argument AND adding the limiter in the app's middleware?Beta Was this translation helpful? Give feedback.
All reactions