feat: add TTL-based eviction to data_svc in-memory RAM store#3324
Open
deacon-mp wants to merge 2 commits into
Open
feat: add TTL-based eviction to data_svc in-memory RAM store#3324deacon-mp wants to merge 2 commits into
deacon-mp wants to merge 2 commits into
Conversation
Evict finished operations older than 7 days via hourly background task.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a TTL-based eviction mechanism for data_svc’s in-memory ram['operations'] to prevent unbounded growth, along with a unit test covering the eviction predicate.
Changes:
- Introduces a background async task that periodically evicts finished operations older than a configured TTL.
- Adds a default TTL configuration for operations (7 days).
- Adds a unit test validating the eviction filtering logic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| tests/security/test_ttl_eviction.py | Adds a unit test for TTL eviction filtering behavior. |
| app/service/data_svc.py | Adds TTL configuration and a periodic background eviction task for ram['operations']. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+147
to
+150
| async def load_data(self, plugins=()): | ||
| loop = asyncio.get_event_loop() | ||
| loop.create_task(self._load(plugins)) | ||
| loop.create_task(self._evict_expired_objects()) |
Comment on lines
+126
to
+145
| async def _evict_expired_objects(self): | ||
| """Background task to evict expired objects based on TTL config.""" | ||
| while True: | ||
| await asyncio.sleep(3600) # Run every hour | ||
| try: | ||
| ttl = self._ttl_config.get('operations') | ||
| if ttl and 'operations' in self.ram: | ||
| now = datetime.datetime.utcnow() | ||
| before = len(self.ram['operations']) | ||
| self.ram['operations'] = [ | ||
| op for op in self.ram['operations'] | ||
| if not (getattr(op, 'finish', None) and | ||
| hasattr(op, 'start') and op.start and | ||
| (now - op.start).total_seconds() > ttl) | ||
| ] | ||
| evicted = before - len(self.ram['operations']) | ||
| if evicted: | ||
| self.log.info('TTL eviction: removed %d expired operations', evicted) | ||
| except Exception as e: | ||
| self.log.error('TTL eviction error: %s', e) |
Comment on lines
+144
to
+145
| except Exception as e: | ||
| self.log.error('TTL eviction error: %s', e) |
|
|
||
| class DataService(DataServiceInterface, BaseService): | ||
|
|
||
| _DEFAULT_TTL_OPERATION_DAYS = 7 |
| self.schema = dict(agents=[], planners=[], adversaries=[], abilities=[], sources=[], operations=[], | ||
| schedules=[], plugins=[], obfuscators=[], objectives=[], data_encoders=[]) | ||
| self.ram = copy.deepcopy(self.schema) | ||
| self._ttl_config = {'operations': self._DEFAULT_TTL_OPERATION_DAYS * 86400} |
Comment on lines
+24
to
+30
| operations = [old_op, new_op, running_op] | ||
| filtered = [ | ||
| op for op in operations | ||
| if not (getattr(op, 'finish', None) and | ||
| hasattr(op, 'start') and op.start and | ||
| (now - op.start).total_seconds() > ttl) | ||
| ] |
|
- Use asyncio.get_running_loop() instead of get_event_loop() in load_data() - Guard _evict_expired_objects() task with self._eviction_task to prevent multiple eviction loops if load_data() is called more than once - Switch eviction exception handler to log.exception() to preserve traceback - Refactor tests to call _evict_expired_objects() directly instead of duplicating the production predicate; add tests for TTL-not-set, running operations, and exception-logging paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


No eviction policy on data_svc.ram. Added configurable TTL eviction to prevent unbounded memory growth. With tests.