Skip to content

fix(grpc-client): add early returns to avoid redundant operations#7672

Merged
limingxinleo merged 11 commits intohyperf:masterfrom
huangdijia:fix/grpc-client-early-return-optimization
Dec 17, 2025
Merged

fix(grpc-client): add early returns to avoid redundant operations#7672
limingxinleo merged 11 commits intohyperf:masterfrom
huangdijia:fix/grpc-client-early-return-optimization

Conversation

@huangdijia
Copy link
Member

Summary

  • Add early return in start() method when client is already running
  • Add early return in init() method when already initialized
  • Prevents unnecessary lock acquisition and redundant checks

Test plan

  • Test grpc client startup with multiple concurrent requests
  • Verify initialization is not called multiple times
  • Ensure no regressions in existing functionality

- Add early return in start() method when client is already running
- Add early return in init() method when already initialized
- Prevents unnecessary lock acquisition and redundant checks
Copilot AI review requested due to automatic review settings December 17, 2025 04:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds early return optimizations to the GRPC client's initialization and startup flow to reduce unnecessary lock contention in concurrent scenarios. By checking the client state before attempting lock acquisition, the code can avoid the overhead of locking when the client is already in the desired state.

Key changes:

  • Early return in start() method when client is already running (lines 193-195)
  • Early return in init() method when already initialized (lines 219-221)
Comments suppressed due to low confidence (1)

src/grpc-client/src/BaseClient.php:219

  • The comment mentions "double-checked locking" but the pattern here is slightly different. The first check at line 219 is outside any lock, making it an unlocked pre-check optimization rather than traditional double-checked locking. Traditional double-checked locking refers to checking both before and after acquiring the lock. While the implementation is correct, the comment could be more precise by describing this as "early return optimization to avoid lock contention" or "unlocked pre-check before attempting lock acquisition".
        $lockKey = sprintf('%s:init', spl_object_hash($this));

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@limingxinleo limingxinleo merged commit 7cfa2bb into hyperf:master Dec 17, 2025
77 checks passed
@huangdijia huangdijia deleted the fix/grpc-client-early-return-optimization branch December 17, 2025 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants