Always implement proper network timeout handling and address formatting to ensure robust connectivity across different network conditions and IP protocols.
Timeouts:
RemoteOperationTimeout
) instead of hardcoding arbitrary valuesAddress formatting:
net.JoinHostPort()
when combining host and port values to ensure compatibility with both IPv4 and IPv6Example of proper implementation:
// GOOD: Using proper timeout and address formatting
ctx, cancel := context.WithTimeout(context.Background(), config.RemoteOperationTimeout)
defer cancel()
// Using net.JoinHostPort for proper IPv4/IPv6 handling
address := net.JoinHostPort(host, port)
conn, err := grpc.DialContext(ctx, address, grpc.WithBlock())
// BAD: Hardcoded timeout and manual address formatting
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Manual concatenation doesn't handle IPv6 properly
address := host + ":" + port
conn, err := grpc.DialContext(ctx, address, grpc.WithBlock())
This approach prevents connection hangs during network issues while ensuring compatibility across IP protocol versions.
Enter the URL of a public GitHub repository