2021-03-12 12:33:32 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-03-09 09:56:02 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2021-03-12 12:33:32 +00:00
|
|
|
|
2021-11-16 17:30:55 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/grpc"
|
2021-03-12 12:33:32 +00:00
|
|
|
grpcstd "google.golang.org/grpc"
|
2021-05-20 15:51:28 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2022-09-17 06:41:31 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2021-03-12 12:33:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) createGRPCClient() (err error) {
|
|
|
|
c.gRPCClientOnce.Do(func() {
|
|
|
|
if err = c.openGRPCConn(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-24 10:18:57 +00:00
|
|
|
c.gRPCClient = grpc.New(
|
|
|
|
grpc.WithClientConnection(c.conn),
|
|
|
|
grpc.WithRWTimeout(c.rwTimeout),
|
|
|
|
)
|
2021-03-12 12:33:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var errInvalidEndpoint = errors.New("invalid endpoint options")
|
|
|
|
|
|
|
|
func (c *Client) openGRPCConn() error {
|
|
|
|
if c.conn != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.addr == "" {
|
|
|
|
return errInvalidEndpoint
|
|
|
|
}
|
|
|
|
|
2022-09-17 06:41:31 +00:00
|
|
|
var creds credentials.TransportCredentials
|
2021-05-20 15:51:28 +00:00
|
|
|
|
|
|
|
if c.tlsCfg != nil {
|
2022-09-17 06:41:31 +00:00
|
|
|
creds = credentials.NewTLS(c.tlsCfg)
|
2021-05-20 15:51:28 +00:00
|
|
|
} else {
|
2022-09-17 06:41:31 +00:00
|
|
|
creds = insecure.NewCredentials()
|
2021-05-20 15:51:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:33:32 +00:00
|
|
|
dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout)
|
2022-09-17 06:41:31 +00:00
|
|
|
var err error
|
|
|
|
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, grpcstd.WithTransportCredentials(creds))
|
2021-03-12 12:33:32 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-03-09 09:56:02 +00:00
|
|
|
|
|
|
|
// ParseURI parses s as address and returns a host and a flag
|
|
|
|
// indicating that TLS is enabled. If multi-address is provided
|
|
|
|
// the argument is returned unchanged.
|
|
|
|
func ParseURI(s string) (string, bool, error) {
|
|
|
|
uri, err := url.ParseRequestURI(s)
|
|
|
|
if err != nil {
|
|
|
|
return s, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if passed string was parsed correctly
|
|
|
|
// URIs that do not start with a slash after the scheme are interpreted as:
|
|
|
|
// `scheme:opaque` => if `opaque` is not empty, then it is supposed that URI
|
|
|
|
// is in `host:port` format
|
|
|
|
if uri.Host == "" {
|
|
|
|
uri.Host = uri.Scheme
|
|
|
|
uri.Scheme = grpcScheme // assume GRPC by default
|
|
|
|
if uri.Opaque != "" {
|
|
|
|
uri.Host = net.JoinHostPort(uri.Host, uri.Opaque)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch uri.Scheme {
|
|
|
|
case grpcTLSScheme, grpcScheme:
|
|
|
|
default:
|
|
|
|
return "", false, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri.Host, uri.Scheme == grpcTLSScheme, nil
|
|
|
|
}
|