model: Change resource.usage_tokens from list to dict

this way we can have efficient look-ups by token UUID
This commit is contained in:
Harald Welte 2023-06-04 20:39:56 +02:00
parent 1b0ae2814b
commit 6544151ff9
1 changed files with 9 additions and 3 deletions

View File

@ -125,7 +125,7 @@ class Resource:
self.description = desc
self.status = "unknown"
self.use_count = 0
self.usage_tokens = []
self.usage_tokens = {}
def usage_inc(self):
@ -144,15 +144,21 @@ class Resource:
def usage_token_get(self, user_name: str, usage: str, duration_s: int) -> UsageToken:
"""Create/obtain a new usage token."""
token = UsageToken(self, user_name, usage, duration_s)
self.usage_tokens[token.uuid.hex] = token
return token
def usage_token_put(self, token: UsageToken):
"""Release/put an existing usage token."""
uuid_hex = token.uuid.hex
del self.usage_tokens[uuid_hex]
def usage_token_find(self, uuid_hex: str):
if uuid_hex in self.usage_tokens:
return self.usage_tokens[uuid_hex]
return None
def get_status(self):
"""Return the (cached) status of the resource."""
return self.status