Repository: etcd-io/etcd
File: client/v3/client.go (Go)

Code before:
		dctx, cancel := context.WithTimeout(c.ctx, dialTo)
		defer cancel()

		if err := waitForConnection(dctx, conn); err != nil {
			conn.Close()
			return nil, err
		}
	}
	return conn, nil
}

func waitForConnection(ctx context.Context, conn *grpc.ClientConn) error {
	cli := healthpb.NewHealthClient(conn)

	// Use WaitForReady to wait until the connection is ready. The health check
	// may return Unimplemented if the server does not expose the health endpoint,
	// or FailedPrecondition if the leader has not yet applied the configuration
	// change that enables it. In both cases, we can still treat the connection as
	// healthy enough to proceed.
	//
	// The downside is reduced error clarity: we may surface context deadline
	// exceeded instead of the underlying cause (for example, connection refused
	// because the endpoint is not ready, or an x509 error from TLS misconfiguration).
	// Users need to enable --debug mode to see the original error.
	//
	// Use withMax to disable retrying on Unimplemented, so that we can
	// return the original error immediately.
	_, err := cli.Check(ctx, &healthpb.HealthCheckRequest{}, grpc.WaitForReady(true), withMax(0))
	if err == nil {
		return nil
	}
	if cerr := ctx.Err(); cerr != nil {
		return fmt.Errorf("etcdclient: failed to connect to the etcd server: %w", cerr)
	}

	serr, ok := status.FromError(err)
	if ok {
		switch serr.Code() {
		case codes.Unimplemented, codes.FailedPrecondition:
			return nil
		}
	}
	return fmt.Errorf("etcdclient: failed to dial by invoking health endpoint: %w", err)
}

func authority(endpoint string) string {
	spl := strings.SplitN(endpoint, "://", 2)
	if len(spl) < 2 {
		if strings.HasPrefix(endpoint, "unix:") {
			return endpoint[len("unix:"):]
		}

Review comment (suggestion):
Instead of this use `WithTimeoutCause ` https://pkg.go.dev/context#WithTimeoutCause

Code after:
		defer cancel()

		if err := waitForConnection(dctx, conn); err != nil {
			conn.Close()
			return nil, err
		}
	}
	return conn, nil
}

func waitForConnection(ctx context.Context, conn *grpc.ClientConn) error {
	cli := healthpb.NewHealthClient(conn)

	// Use WaitForReady to wait until the connection is ready. The health check
	// may return Unimplemented if the server does not expose the health endpoint,
	// or FailedPrecondition if the leader has not yet applied the configuration
	// change that enables it. In both cases, we can still treat the connection as
	// healthy enough to proceed.
	//
	// Use withMax to disable retrying on Unimplemented, so that we can
	// return the original error immediately.
	_, err := cli.Check(ctx, &healthpb.HealthCheckRequest{}, grpc.WaitForReady(true), withMax(0))
	if err == nil {
		return nil
	}
	if cerr := ctx.Err(); cerr != nil {
		if serr, ok := status.FromError(err); ok && serr.Message() != "" {
			return fmt.Errorf("etcdclient: failed to connect to the etcd server: %s: %w", serr.Message(), cerr)
		}
		return fmt.Errorf("etcdclient: failed to connect to the etcd server: %w", cerr)
	}

	serr, ok := status.FromError(err)
	if ok {
		switch serr.Code() {
		case codes.Unimplemented, codes.FailedPrecondition:
			return nil
		}
	}
	return fmt.Errorf("etcdclient: failed to dial by invoking health endpoint: %w", err)
}

func authority(endpoint string) string {
	spl := strings.SplitN(endpoint, "://", 2)
	if len(spl) < 2 {
		if strings.HasPrefix(endpoint, "unix:") {
			return endpoint[len("unix:"):]
		}
		if strings.HasPrefix(endpoint, "unixs:") {
			return endpoint[len("unixs:"):]
		}