github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/rpc/client/connect.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net"
     8  	"net/url"
     9  
    10  	grpcstd "google.golang.org/grpc"
    11  )
    12  
    13  var errInvalidEndpoint = errors.New("invalid endpoint options")
    14  
    15  func (c *Client) openGRPCConn(ctx context.Context) error {
    16  	if c.conn != nil {
    17  		return nil
    18  	}
    19  
    20  	if c.addr == "" {
    21  		return errInvalidEndpoint
    22  	}
    23  
    24  	dialCtx, cancel := context.WithTimeout(ctx, c.dialTimeout)
    25  	var err error
    26  
    27  	c.conn, err = grpcstd.DialContext(dialCtx, c.addr, c.grpcDialOpts...)
    28  
    29  	cancel()
    30  
    31  	if err != nil {
    32  		return fmt.Errorf("gRPC dial: %w", err)
    33  	}
    34  
    35  	return nil
    36  }
    37  
    38  // ParseURI parses s as address and returns a host and a flag
    39  // indicating that TLS is enabled. If multi-address is provided
    40  // the argument is returned unchanged.
    41  func ParseURI(s string) (string, bool, error) {
    42  	uri, err := url.ParseRequestURI(s)
    43  	if err != nil {
    44  		return s, false, nil
    45  	}
    46  
    47  	// check if passed string was parsed correctly
    48  	// URIs that do not start with a slash after the scheme are interpreted as:
    49  	// `scheme:opaque` => if `opaque` is not empty, then it is supposed that URI
    50  	// is in `host:port` format
    51  	if uri.Host == "" {
    52  		uri.Host = uri.Scheme
    53  		uri.Scheme = grpcScheme // assume GRPC by default
    54  		if uri.Opaque != "" {
    55  			uri.Host = net.JoinHostPort(uri.Host, uri.Opaque)
    56  		}
    57  	}
    58  
    59  	switch uri.Scheme {
    60  	case grpcTLSScheme, grpcScheme:
    61  	default:
    62  		return "", false, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
    63  	}
    64  
    65  	return uri.Host, uri.Scheme == grpcTLSScheme, nil
    66  }