Datasets:
File size: 6,128 Bytes
4e3f22a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | diff --git a/aws_advanced_python_wrapper/utils/concurrent.py b/aws_advanced_python_wrapper/utils/concurrent.py
index 048369324..679933a00 100644
--- a/aws_advanced_python_wrapper/utils/concurrent.py
+++ b/aws_advanced_python_wrapper/utils/concurrent.py
@@ -14,13 +14,9 @@
from __future__ import annotations
-from typing import TYPE_CHECKING, Dict, Iterator, Set, Union, ValuesView
-
-if TYPE_CHECKING:
- from typing import ItemsView
-
from threading import Condition, Lock, RLock
-from typing import Callable, Generic, KeysView, List, Optional, TypeVar
+from typing import (Callable, Dict, Generic, Iterator, List, Optional, Set,
+ TypeVar, Union)
K = TypeVar('K')
V = TypeVar('V')
@@ -111,14 +107,20 @@ def apply_if(self, predicate: Callable, apply: Callable):
if predicate(key, value):
apply(key, value)
- def keys(self) -> KeysView:
- return self._dict.keys()
+ def keys(self) -> List[K]:
+ """Returns a thread-safe snapshot of keys."""
+ with self._lock:
+ return list(self._dict.keys())
- def values(self) -> ValuesView:
- return self._dict.values()
+ def values(self) -> List[V]:
+ """Returns a thread-safe snapshot of values."""
+ with self._lock:
+ return list(self._dict.values())
- def items(self) -> ItemsView:
- return self._dict.items()
+ def items(self) -> List[tuple[K, V]]:
+ """Returns a thread-safe snapshot of items."""
+ with self._lock:
+ return list(self._dict.items())
class ConcurrentSet(Generic[V]):
diff --git a/aws_advanced_python_wrapper/utils/sliding_expiration_cache.py b/aws_advanced_python_wrapper/utils/sliding_expiration_cache.py
index 8dd9c2198..4085e43c7 100644
--- a/aws_advanced_python_wrapper/utils/sliding_expiration_cache.py
+++ b/aws_advanced_python_wrapper/utils/sliding_expiration_cache.py
@@ -16,7 +16,7 @@
from threading import Thread
from time import perf_counter_ns, sleep
-from typing import Callable, Generic, ItemsView, KeysView, Optional, TypeVar
+from typing import Callable, Generic, List, Optional, Tuple, TypeVar
from aws_advanced_python_wrapper.utils.atomic import AtomicInt
from aws_advanced_python_wrapper.utils.concurrent import ConcurrentDict
@@ -46,10 +46,10 @@ def __len__(self):
def set_cleanup_interval_ns(self, interval_ns):
self._cleanup_interval_ns = interval_ns
- def keys(self) -> KeysView:
+ def keys(self) -> List[K]:
return self._cdict.keys()
- def items(self) -> ItemsView:
+ def items(self) -> List[Tuple[K, CacheItem[V]]]:
return self._cdict.items()
def compute_if_absent(self, key: K, mapping_func: Callable, item_expiration_ns: int) -> Optional[V]:
@@ -73,32 +73,28 @@ def _remove_and_dispose(self, key: K):
self._item_disposal_func(cache_item.item)
def _remove_if_expired(self, key: K):
- item = None
-
def _remove_if_expired_internal(_, cache_item):
if self._should_cleanup_item(cache_item):
- nonlocal item
- item = cache_item.item
+ # Dispose while holding the lock to prevent race conditions
+ if self._item_disposal_func is not None:
+ self._item_disposal_func(cache_item.item)
return None
-
return cache_item
self._cdict.compute_if_present(key, _remove_if_expired_internal)
- if item is None or self._item_disposal_func is None:
- return
-
- self._item_disposal_func(item)
-
def _should_cleanup_item(self, cache_item: CacheItem) -> bool:
if self._should_dispose_func is not None:
return perf_counter_ns() > cache_item.expiration_time and self._should_dispose_func(cache_item.item)
return perf_counter_ns() > cache_item.expiration_time
def clear(self):
- for _, cache_item in self._cdict.items():
- if cache_item is not None and self._item_disposal_func is not None:
- self._item_disposal_func(cache_item.item)
+ # Dispose all items while holding the lock
+ if self._item_disposal_func is not None:
+ self._cdict.apply_if(
+ lambda k, v: True, # Apply to all items
+ lambda k, cache_item: self._item_disposal_func(cache_item.item)
+ )
self._cdict.clear()
def _cleanup(self):
@@ -107,7 +103,7 @@ def _cleanup(self):
return
self._cleanup_time_ns.set(current_time + self._cleanup_interval_ns)
- keys = [key for key, _ in self._cdict.items()]
+ keys = self._cdict.keys()
for key in keys:
self._remove_if_expired(key)
@@ -129,29 +125,21 @@ def compute_if_absent_with_disposal(self, key: K, mapping_func: Callable, item_e
return None if cache_item is None else cache_item.update_expiration(item_expiration_ns).item
def _remove_if_disposable(self, key: K):
- item = None
-
def _remove_if_disposable_internal(_, cache_item):
if self._should_dispose_func is not None and self._should_dispose_func(cache_item.item):
- nonlocal item
- item = cache_item.item
+ if self._item_disposal_func is not None:
+ self._item_disposal_func(cache_item.item)
return None
-
return cache_item
self._cdict.compute_if_present(key, _remove_if_disposable_internal)
- if item is None or self._item_disposal_func is None:
- return
-
- self._item_disposal_func(item)
-
def _cleanup_thread_internal(self):
while True:
try:
sleep(self._cleanup_interval_ns / 1_000_000_000)
self._cleanup_time_ns.set(perf_counter_ns() + self._cleanup_interval_ns)
- keys = [key for key, _ in self._cdict.items()]
+ keys = self._cdict.keys()
for key in keys:
try:
self._remove_if_expired(key)
|