Skip to content

Conversation

@sithu-go
Copy link

@sithu-go sithu-go commented Dec 4, 2025

Fixes #8739

Motivation

When migrating from the deprecated grpc.Dial with WithBlock() and WithTimeout() to grpc.NewClient, there is no built-in way to validate target reachability during client initialization without blocking behavior. This PR introduces WithTargetCheckas an alternative that validates DNS resolution and TCP reachability without the complexity and anti-patterns associated with WithBlock().

Use Case
This option is valuable for scenarios where:

  • Services need to "fail fast" during startup if critical backend dependencies are unreachable
  • Early validation prevents cascading failures in orchestrated systems
  • Development and testing environments benefit from immediate feedback

Unlike WithBlock(), WithTargetCheck performs a single validation check during NewClient() and does not interfere with the connection lifecycle or background reconnection logic.

Implementation

This PR adds:

  1. New DialOption: WithTargetCheck(timeout time.Duration) - performs DNS resolution and TCP dial validation for "dns" and "passthrough" schemes
  2. Validation logic: Added validateTarget method to ClientConn that attempts a TCP connection within the specified timeout
  3. Tests: unit tests covering success and failure cases

The implementation:

  • Only validates during NewClient() initialization
  • Returns an error immediately if the target cannot be reached within the timeout
  • Supports "dns" and "passthrough" schemes (other schemes are skipped)
  • Does not block ongoing connection attempts or interfere with reconnection behavior

RELEASE NOTES:

client: Added WithTargetCheck DialOption to validate target DNS resolution and TCP reachability during client initialization, providing a lightweight alternative to deprecated WithBlock() for fail-fast scenarios.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 4, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.61%. Comparing base (4c27cc8) to head (441c2a7).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
clientconn.go 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8743      +/-   ##
==========================================
- Coverage   83.28%   82.61%   -0.68%     
==========================================
  Files         418      418              
  Lines       32367    32399      +32     
==========================================
- Hits        26958    26765     -193     
- Misses       4034     4055      +21     
- Partials     1375     1579     +204     
Files with missing lines Coverage Δ
dialoptions.go 68.92% <100.00%> (-20.15%) ⬇️
clientconn.go 71.27% <92.30%> (-18.87%) ⬇️

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sithu-go sithu-go changed the title client: Add WithTargetCheck DialOption for early target validation Type: Feature client: Add WithTargetCheck DialOption for early target validation Dec 4, 2025
@sithu-go sithu-go closed this Dec 4, 2025
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.

Question: How to use Timeout and Blocking behavior with grpc.NewClient?

1 participant