Skip to content

Instantly share code, notes, and snippets.

@lbragstad
Last active May 10, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbragstad/980c07f3684a70ffb3dfb920f435249c to your computer and use it in GitHub Desktop.
Save lbragstad/980c07f3684a70ffb3dfb920f435249c to your computer and use it in GitHub Desktop.
oslo.limit example usage
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