-
-
Notifications
You must be signed in to change notification settings - Fork 3
feat(insights): Add timeout to image optimization analysis #614
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import io | ||
| import logging | ||
| import time | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
|
|
@@ -46,6 +47,7 @@ class BaseImageOptimizationInsight(Insight[ImageOptimizationInsightResult], ABC) | |
| TARGET_JPEG_QUALITY = 85 | ||
| TARGET_HEIC_QUALITY = 85 | ||
| _MAX_WORKERS = 4 | ||
| _TIMEOUT_SECONDS = 300 | ||
|
|
||
| @abstractmethod | ||
| def _find_images(self, input: InsightsInput) -> List[FileInfo]: | ||
|
|
@@ -73,17 +75,32 @@ def generate(self, input: InsightsInput) -> ImageOptimizationInsightResult | Non | |
| return None | ||
|
|
||
| results: List[OptimizableImageFile] = [] | ||
| timed_out = False | ||
| completed = 0 | ||
| deadline = time.monotonic() + self._TIMEOUT_SECONDS | ||
| with ThreadPoolExecutor(max_workers=min(self._MAX_WORKERS, len(files))) as executor: | ||
| future_to_file = {executor.submit(self._analyze_image_optimization, f): f for f in files} | ||
| for future in as_completed(future_to_file): | ||
| completed += 1 | ||
|
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. Deadline not enforced while blocked in
|
||
| try: | ||
| result = future.result() | ||
| if result and result.potential_savings >= self.MIN_SAVINGS_THRESHOLD: | ||
| results.append(result) | ||
| except Exception: # pragma: no cover | ||
| logger.exception("Failed to analyze image in thread pool") | ||
| if completed < len(files) and time.monotonic() >= deadline: | ||
| timed_out = True | ||
| logger.warning( | ||
| "size.insight.image_optimization.timeout | completed=%d/%d timeout=%ds", | ||
| completed, | ||
| len(files), | ||
| self._TIMEOUT_SECONDS, | ||
| ) | ||
| for fut in future_to_file: | ||
| fut.cancel() | ||
| break | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| if not results: | ||
| if not results and not timed_out: | ||
| return None | ||
|
|
||
| results.sort(key=lambda x: x.potential_savings, reverse=True) | ||
|
|
@@ -92,6 +109,7 @@ def generate(self, input: InsightsInput) -> ImageOptimizationInsightResult | Non | |
| return ImageOptimizationInsightResult( | ||
| optimizable_files=results, | ||
| total_savings=total_savings, | ||
| timed_out=timed_out, | ||
| ) | ||
|
|
||
| def _analyze_image_optimization( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,6 +213,7 @@ class ImageOptimizationInsightResult(BaseInsightResult): | |
| optimizable_files: List[OptimizableImageFile] = Field( | ||
| ..., description="Files that can be optimized with potential savings" | ||
| ) | ||
| timed_out: bool = Field(False, description="Whether analysis was cut short due to timeout") | ||
|
|
||
| def get_file_paths(self) -> List[str]: | ||
|
Contributor
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. Bug: The frontend does not handle the new Suggested FixUpdate the Prompt for AI AgentAlso affects:
Did we get this right? 👍 / 👎 to inform future reviews. |
||
| return [f.file_path for f in self.optimizable_files] | ||
|
|
||


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.
nit: is
completedhelpful here vslen(results)?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.
iirc The
completedcounter tracks all processed futures (including failures and below-threshold images), whilelen(results)only counts optimization candidates