-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added force ttl check option to kv-table functions #725
Changes from 3 commits
8d5808c
29a8541
6f4bc55
72a9b42
aed7074
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,12 +186,21 @@ def kv_table() -> boto3.resource: | |
return _KV_TABLE | ||
|
||
|
||
def get_counter(key: str) -> int: | ||
def ttl_expired(response: dict) -> bool: | ||
"""Checks whether a response from the panther-kv table has passed it's TTL date""" | ||
# This can be used when the TTL timing is very exacting and DDB's cleanup is too slow | ||
expiration = response.get("Item", {}).get("expiresAt", 0) | ||
return expiration and float(expiration) <= (datetime.now()).timestamp() | ||
|
||
|
||
def get_counter(key: str, force_ttl_check: bool = False) -> int: | ||
"""Get a counter's current value (defaulting to 0 if key does not exist).""" | ||
response = kv_table().get_item( | ||
Key={"key": key}, | ||
ProjectionExpression=_COUNT_COL, | ||
) | ||
if force_ttl_check and ttl_expired(response): | ||
return 0 | ||
return response.get("Item", {}).get(_COUNT_COL, 0) | ||
|
||
|
||
|
@@ -271,7 +280,7 @@ def put_dictionary(key: str, val: dict, epoch_seconds: int = None): | |
set_key_expiration(key, epoch_seconds) | ||
|
||
|
||
def get_dictionary(key: str) -> dict: | ||
def get_dictionary(key: str, force_ttl_check: bool = False) -> dict: | ||
# Retrieve the item from DynamoDB | ||
response = kv_table().get_item(Key={"key": key}) | ||
|
||
|
@@ -281,6 +290,9 @@ def get_dictionary(key: str) -> dict: | |
if not item: | ||
return {} | ||
|
||
if force_ttl_check and ttl_expired(response): | ||
return {} | ||
|
||
try: | ||
# Deserialize from JSON to a Python dictionary | ||
return json.loads(item) | ||
|
@@ -291,12 +303,14 @@ def get_dictionary(key: str) -> dict: | |
) from exc | ||
|
||
|
||
def get_string_set(key: str) -> Set[str]: | ||
def get_string_set(key: str, force_ttl_check: bool = False) -> Set[str]: | ||
"""Get a string set's current value (defaulting to empty set if key does not exit).""" | ||
response = kv_table().get_item( | ||
Key={"key": key}, | ||
ProjectionExpression=_STRING_SET_COL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If i'm reading this correctly this will only pull back the stringset column and won't include the ttl There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good catch, of course my live test was only on the dictionary one where that isn't the case 🤦 I was going to test the other two methods just for certainty's sake before merging just hadn't got to it yet! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @darwayne do you think there is any downside to simply removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There could be some edge case where a user has a ton of data in some other column that they aren't trying to retrieve. So tradeoffs are mostly latency based |
||
) | ||
if force_ttl_check and ttl_expired(response): | ||
return set() | ||
return response.get("Item", {}).get(_STRING_SET_COL, set()) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we go with this approach you'll need to update all the ProjectionExpressions to also return the TTL. I see in a few spots we are only getting back a single column