2021-03-12 12:33:32 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
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"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2021-05-20 15:51:28 +00:00
|
|
|
var credOpt grpcstd.DialOption
|
|
|
|
|
|
|
|
if c.tlsCfg != nil {
|
|
|
|
creds := credentials.NewTLS(c.tlsCfg)
|
|
|
|
credOpt = grpcstd.WithTransportCredentials(creds)
|
|
|
|
} else {
|
|
|
|
credOpt = grpcstd.WithInsecure()
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:33:32 +00:00
|
|
|
dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout)
|
2021-05-20 15:51:28 +00:00
|
|
|
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, credOpt)
|
2021-03-12 12:33:32 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|