Skip to content

Commit 9707064

Browse files
committed
Cache AWS.get_offers_post_filter
1 parent a8babca commit 9707064

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/dstack/_internal/core/backends/aws/compute.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ def __init__(self, config: AWSConfig):
111111
# Caches to avoid redundant API calls when provisioning many instances
112112
# get_offers is already cached but we still cache its sub-functions
113113
# with more aggressive/longer caches.
114+
self._offers_post_filter_cache_lock = threading.Lock()
115+
self._offers_post_filter_cache = TTLCache(maxsize=10, ttl=180)
114116
self._get_regions_to_quotas_cache_lock = threading.Lock()
115117
self._get_regions_to_quotas_execution_lock = threading.Lock()
116118
self._get_regions_to_quotas_cache = TTLCache(maxsize=10, ttl=300)
@@ -160,6 +162,15 @@ def get_offers_modifier(
160162
) -> Callable[[InstanceOfferWithAvailability], Optional[InstanceOfferWithAvailability]]:
161163
return get_offers_disk_modifier(CONFIGURABLE_DISK_SIZE, requirements)
162164

165+
def _get_offers_cached_key(self, requirements: Requirements) -> int:
166+
# Requirements is not hashable, so we use a hack to get arguments hash
167+
return hash(requirements.json())
168+
169+
@cachedmethod(
170+
cache=lambda self: self._offers_post_filter_cache,
171+
key=_get_offers_cached_key,
172+
lock=lambda self: self._offers_post_filter_cache_lock,
173+
)
163174
def get_offers_post_filter(
164175
self, requirements: Requirements
165176
) -> Optional[Callable[[InstanceOfferWithAvailability], bool]]:

0 commit comments

Comments
 (0)