-
Notifications
You must be signed in to change notification settings - Fork 339
perf(server): release Mutex early for performance #213
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThis change updates the locking mechanism in three methods of the MCPServer implementation: AddResource, AddResourceTemplate, and AddPrompt. Instead of using deferred statements to unlock mutexes at the end of each method, the code now explicitly unlocks the mutex immediately after the critical section that modifies shared maps. No changes were made to the method signatures or the logic outside of mutex handling. Changes
Possibly related PRs
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🔇 Additional comments (4)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
lgtm |
Note that in func
MCPServer.AddResource()/AddResourceTemplate()/AddPrompt()
, we utilizedefer s.resourcesMu.Unlock() / defer s.promptsMu.Unlock()
to release the corresponding exclusive mutex, which makes the functions mentioned could only release the mutex after we finish calling funcSendNotificationToAllClients()
to send notifications to all clients connected to MCPServer. And this could be a bottleneck in concurrent scenario when there are too many clients and there are also too many request of calling funcAddResource() / AddResourceTemplate() /AddPrompt()
Is it safe to release the mutex as soon as we finish modifying
s.resources/s.resourceTemplates/s.prompts
but don't wait funcSendNotificationToAllClients
to finish?I think the answer to this question is YES, because func
SendNotificationToAllClients
only iterates over all registered sessions and send notifications to them, it doesn't modifys.resources/s.resourceTemplates/s.prompts
and even don't read these fields.experiment


Summary by CodeRabbit