Last active
May 10, 2018 21:32
-
-
Save lbragstad/980c07f3684a70ffb3dfb920f435249c to your computer and use it in GitHub Desktop.
oslo.limit example usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from oslo_limit import limit | |
LIMIT_ENFORCER = limit.Enforcer() | |
def create_foobar(self, context, foobar): | |
# check if project is over the limit prior to creating the resource | |
claim = 1 | |
resource_name = 'foobars' | |
usage_callback = self._list_foobars_for_project | |
LIMIT_ENFORCER.enforce( | |
resource_name, claim, context.project_id, usage_callback | |
) | |
# execute business logic, or whatever the thing is that increments project | |
# usage of a resource | |
driver.create_foobar(foobar) | |
# make sure we're still within the project limit and avoid race conditions | |
# where another thread or process put us at the limit before we exit the | |
# API | |
try: | |
LIMIT_ENFORCER.verify( | |
resource_name, claim, context.project_id, usage_callback | |
) | |
except limit.UsageExceededLimitException(): | |
driver.rollback(foobar) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment