Add semaphore to limit concurrent API calls during Kubernetes observer startup#11
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>
Comment on lines
+149
to
159
| ) | ||
| ), | ||
| ) | ||
|
|
||
| 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.
Bug: The orchestration_client.request() call is outside the _startup_event_semaphore block, so the API requests are not actually rate-limited as intended.
Severity: CRITICAL
Suggested Fix
Move the response = await orchestration_client.request(...) call inside the async with _startup_event_semaphore: block. This will ensure that the actual network requests are being limited by the semaphore as intended.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/integrations/prefect-kubernetes/prefect_kubernetes/observer.py#L137-L159
Potential issue: The code introduces a semaphore, `_startup_event_semaphore`, to
rate-limit API calls during observer startup. However, the `async with` block for the
semaphore only wraps the creation of the `event_filter` object. The actual network
request, `await orchestration_client.request(...)`, is executed after the semaphore
context has been exited. As a result, the expensive API calls to the `/events/filter`
endpoint are not rate-limited, defeating the purpose of the change. This can lead to the
observer overwhelming the API server with concurrent requests upon startup when many
pods are present.
Did we get this right? 👍 / 👎 to inform future reviews.
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