2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-06-29 19:28:06 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-20 16:54:52 +00:00
|
|
|
"time"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-06-29 19:28:06 +01:00
|
|
|
"google.golang.org/grpc"
|
2019-01-08 08:22:54 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
2019-01-30 20:47:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-06-29 19:28:06 +01:00
|
|
|
)
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
//Error is the errs class of standard Transport Client errors
|
|
|
|
Error = errs.Class("transport error")
|
2018-11-20 16:54:52 +00:00
|
|
|
// default time to wait for a connection to be established
|
|
|
|
timeout = 20 * time.Second
|
2018-11-06 17:49:17 +00:00
|
|
|
)
|
|
|
|
|
2018-12-22 04:51:42 +00:00
|
|
|
// Observer implements the ConnSuccess and ConnFailure methods
|
|
|
|
// for Discovery and other services to use
|
|
|
|
type Observer interface {
|
|
|
|
ConnSuccess(ctx context.Context, node *pb.Node)
|
|
|
|
ConnFailure(ctx context.Context, node *pb.Node, err error)
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
// Client defines the interface to an transport client.
|
|
|
|
type Client interface {
|
|
|
|
DialNode(ctx context.Context, node *pb.Node, opts ...grpc.DialOption) (*grpc.ClientConn, error)
|
|
|
|
DialAddress(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
|
2019-01-30 20:47:21 +00:00
|
|
|
Identity() *identity.FullIdentity
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:28:06 +01:00
|
|
|
// Transport interface structure
|
|
|
|
type Transport struct {
|
2019-01-30 20:47:21 +00:00
|
|
|
identity *identity.FullIdentity
|
2018-12-22 04:51:42 +00:00
|
|
|
observers []Observer
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
// NewClient returns a newly instantiated Transport Client
|
2019-01-30 20:47:21 +00:00
|
|
|
func NewClient(identity *identity.FullIdentity, obs ...Observer) Client {
|
2018-12-22 04:51:42 +00:00
|
|
|
return &Transport{
|
|
|
|
identity: identity,
|
|
|
|
observers: obs,
|
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
// DialNode returns a grpc connection with tls to a node
|
2018-11-19 14:40:01 +00:00
|
|
|
func (transport *Transport) DialNode(ctx context.Context, node *pb.Node, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
|
2018-06-29 19:28:06 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-02 18:47:34 +00:00
|
|
|
if node != nil {
|
|
|
|
node.Type.DPanicOnInvalid("transport dial node")
|
|
|
|
}
|
2018-08-24 05:01:03 +01:00
|
|
|
if node.Address == nil || node.Address.Address == "" {
|
2018-06-29 19:28:06 +01:00
|
|
|
return nil, Error.New("no address")
|
|
|
|
}
|
2018-11-09 22:08:33 +00:00
|
|
|
|
|
|
|
// add ID of node we are wanting to connect to
|
2018-11-29 18:39:27 +00:00
|
|
|
dialOpt, err := transport.identity.DialOption(node.Id)
|
2018-11-09 22:08:33 +00:00
|
|
|
if err != nil {
|
2019-01-08 08:22:54 +00:00
|
|
|
return nil, Error.Wrap(err)
|
2018-11-09 22:08:33 +00:00
|
|
|
}
|
2018-11-19 14:40:01 +00:00
|
|
|
|
2019-02-01 17:40:35 +00:00
|
|
|
options := append([]grpc.DialOption{dialOpt, grpc.WithBlock()}, opts...)
|
2018-11-20 16:54:52 +00:00
|
|
|
|
|
|
|
ctx, cf := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cf()
|
2018-12-22 04:51:42 +00:00
|
|
|
|
|
|
|
conn, err = grpc.DialContext(ctx, node.GetAddress().Address, options...)
|
|
|
|
if err != nil {
|
|
|
|
alertFail(ctx, transport.observers, node, err)
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alertSuccess(ctx, transport.observers, node)
|
|
|
|
|
2019-01-08 08:22:54 +00:00
|
|
|
return conn, nil
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialAddress returns a grpc connection with tls to an IP address
|
2018-11-19 14:40:01 +00:00
|
|
|
func (transport *Transport) DialAddress(ctx context.Context, address string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
|
2018-11-06 17:49:17 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
dialOpt, err := transport.identity.DialOption(storj.NodeID{})
|
2018-08-24 05:01:03 +01:00
|
|
|
if err != nil {
|
2019-01-08 08:22:54 +00:00
|
|
|
return nil, Error.Wrap(err)
|
2018-08-24 05:01:03 +01:00
|
|
|
}
|
2018-11-19 14:40:01 +00:00
|
|
|
|
2019-02-01 17:40:35 +00:00
|
|
|
options := append([]grpc.DialOption{dialOpt, grpc.WithBlock()}, opts...)
|
2019-01-08 08:22:54 +00:00
|
|
|
conn, err = grpc.Dial(address, options...)
|
|
|
|
return conn, Error.Wrap(err)
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
|
|
|
|
// Identity is a getter for the transport's identity
|
2019-01-30 20:47:21 +00:00
|
|
|
func (transport *Transport) Identity() *identity.FullIdentity {
|
2018-11-19 14:40:01 +00:00
|
|
|
return transport.identity
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 04:51:42 +00:00
|
|
|
func alertFail(ctx context.Context, obs []Observer, node *pb.Node, err error) {
|
|
|
|
for _, o := range obs {
|
|
|
|
o.ConnFailure(ctx, node, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func alertSuccess(ctx context.Context, obs []Observer, node *pb.Node) {
|
|
|
|
for _, o := range obs {
|
|
|
|
o.ConnSuccess(ctx, node)
|
|
|
|
}
|
|
|
|
}
|