Query and code performance optimizations in services#1527
Merged
Conversation
madhav165
approved these changes
Dec 2, 2025
Collaborator
madhav165
left a comment
There was a problem hiding this comment.
Normal functionality works as expected
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com>
d15df51 to
e6b44fa
Compare
kcostell06
pushed a commit
to kcostell06/mcp-context-forge
that referenced
this pull request
Feb 24, 2026
Query and code performance optimizations in services
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.
🐛 Bug-fix PR
Closes #1522
Closes #1523
📌 Summary
This PR resolves major performance degradation across
gateway_service.py,tool_service.py, andserver_service.py.Implemented concurrent Gateway health checks with batches and configurable batch sizes.
🐞 Root Cause
💡 Fix Description
gateway_service.pylist_gateways()andlist_gateways_for_user(), reducing lookups from O(n) to O(1)._update_or_create_tools(),_update_or_create_resources(), and_update_or_create_prompts()to use bulk fetching (IN clause), reducing queries from O(n) to O(1) for entity creation/updates.check_health_of_gateways()to Useasyncio.gather()for Parallel ExecutionUpdated the
check_health_of_gateways()function to leverageasyncio.gather()for executing health checks concurrently.Introduced a dynamic concurrency_limit that adapts based on system capabilities. The new limit is calculated as the minimum of the configured
MAX_CONCURRENT_HEALTH_CHECKSand an adaptive value based on the system's CPU count.This ensures that the system doesn't overload when the specified concurrent checks exceed the system's capacity.
tool_service.py:aggregate_metrics(): Reduced from 8 separate queries to a single, aggregated SQL query (87.5% reduction in network round-trips).list_tools()andlist_tools_for_user(), reducing queries from ~101 to ~2 for 100 tools (98% reduction).server_service.pylist_servers()andlist_servers_for_user(), reducing queries from O(n) to O(1) (up to ~100x faster).aggregate_metrics(): Reduced from 7 queries to a single query (85.7% reduction, 7x faster).register_server()andupdate_server(), resulting in ~4.5 speedup for typical cases._convert_server_to_read()for single-pass metrics calculation, reducing iterations from 8 to 1 (~8x faster).🧪 Verification
make lintmake testmake coverage📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)