Add semaphore to limit concurrent API calls during Kubernetes observer startup#165
Conversation
…r startup closes PrefectHQ#19937 When the observer starts, it receives events for all existing pods/jobs and checks for duplicates via API calls. This can overwhelm the server when there are many pods. This change adds a configurable semaphore (default: 5 concurrent calls) to rate-limit these startup checks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Code Review by Qodo
1. Semaphore misses API call
|
Comment on lines
+135
to
159
| # Use semaphore to limit concurrent API calls during startup to prevent | ||
| # overwhelming the API server when there are many existing pods/jobs | ||
| async with _startup_event_semaphore: | ||
| # Use the Kubernetes event timestamp for the filter to avoid "Query time range is too large" error | ||
| event_filter = EventFilter( | ||
| event=EventNameFilter(name=[f"prefect.kubernetes.pod.{phase.lower()}"]), | ||
| resource=EventResourceFilter( | ||
| id=[f"prefect.kubernetes.pod.{uid}"], | ||
| ), | ||
| occurred=EventOccurredFilter( | ||
| since=( | ||
| k8s_created_time | ||
| if k8s_created_time | ||
| else (datetime.now(timezone.utc) - timedelta(hours=1)) | ||
| ) | ||
| ), | ||
| ) | ||
|
|
||
| response = await orchestration_client.request( | ||
| "POST", | ||
| "/events/filter", | ||
| json=dict(filter=event_filter.model_dump(exclude_unset=True, mode="json")), | ||
| json=dict( | ||
| filter=event_filter.model_dump(exclude_unset=True, mode="json") | ||
| ), | ||
| ) |
There was a problem hiding this comment.
1. Semaphore misses api call 🐞 Bug ⛯ Reliability
• The new startup semaphore is intended to limit concurrent /events/filter calls, but it is only held while building the EventFilter, then released before the actual orchestration_client.request(...) runs. • This defeats the throttling goal and can still overwhelm the Prefect API during startup scans with many existing pods. • The current behavior also adds avoidable scheduling overhead (queueing for a semaphore that doesn’t protect the expensive operation).
Agent prompt
### Issue description
The startup semaphore is intended to limit concurrent `/events/filter` calls, but it currently wraps only `EventFilter` construction. The actual `orchestration_client.request(...)` happens after the semaphore is released, so concurrency is not throttled.
### Issue Context
This branch runs when `event_type is None` (startup/backfill-style events), which is precisely when many concurrent dedupe checks can occur.
### Fix Focus Areas
- src/integrations/prefect-kubernetes/prefect_kubernetes/observer.py[129-162]
### Suggested change
Move the `orchestration_client.request(...)` and the `if response.json()["events"]: return` logic inside the `async with _startup_event_semaphore:` block so the semaphore actually limits concurrent requests.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
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.
Benchmark PR from agentic-review-benchmarks#11