Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion rate_limit/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def __init__(self, service_type, logger=log.Logger(__name__), **kwargs):
super(ConfigurationRateLimitProvider, self).__init__(
service_type=service_type, logger=logger, kwargs=kwargs
)
# Custom rate limits per scope.
self.custom_ratelimits = {}

def get_global_rate_limits(self, action, target_type_uri, **kwargs):
"""
Expand Down Expand Up @@ -101,7 +103,10 @@ def get_local_rate_limits(self, scope, action, target_type_uri, **kwargs):
:param kwargs: optional, additional parameters
:return: the local rate limit or -1 if not set
"""
ttu_ratelimits = self.local_ratelimits.get(target_type_uri, [])
_ratelimits = self.custom_ratelimits.get(scope, None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are custom limits fetched via the get_global_rate_limits? Could it be part of the get_local_rate_limits?
If yes, we could thing about merging def get_local_rate_limits(self, scope, action, target_type_uri, **kwargs) and f get_global_rate_limits(self, action, target_type_uri, **kwargs) into a single method. Those are nearly identical.

 # Get local (for a certain scope) rate limits from provider.
        local_rate_limit = self.ratelimit_provider.get_local_rate_limits(
            scope, action, trimmed_target_type_uri
        )

move to

 # Get local (for a certain scope) rate limits from provider.
        local_rate_limit = self.ratelimit_provider.get_rate_limits(
            scope, action, trimmed_target_type_uri, 'local')
        ...
        custom_rate_limit = self.ratelimit_provider.get_rate_limits(
            scope, action, trimmed_target_type_uri, '<custom>')

if not _ratelimits:
_ratelimits = self.local_ratelimits
ttu_ratelimits = _ratelimits.get(target_type_uri, [])
if not ttu_ratelimits:
ttu_ratelimits = self._get_wildcard_ratelimits(
self.local_ratelimits,
Expand Down Expand Up @@ -157,6 +162,9 @@ def read_rate_limits_from_config(self, config_path):
rates = config.get('rates', {})
self.global_ratelimits = rates.get('global', {})
self.local_ratelimits = rates.get('default', {})
for scope, rl in rates.items():
if scope not in ['global', 'default']:
self.custom_ratelimits[scope] = rl


class LimesRateLimitProvider(RateLimitProvider):
Expand Down