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"
|
2019-03-22 17:09:37 +00:00
|
|
|
"time"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
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"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2018-06-29 19:28:06 +01: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
|
2019-02-08 09:25:13 +00:00
|
|
|
WithObservers(obs ...Observer) *Transport
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:28:06 +01:00
|
|
|
// Transport interface structure
|
|
|
|
type Transport struct {
|
2019-03-22 17:09:37 +00:00
|
|
|
tlsOpts *tlsopts.Options
|
|
|
|
observers []Observer
|
|
|
|
requestTimeout time.Duration
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
// NewClient returns a transport client with a default timeout for requests
|
2019-02-11 11:17:32 +00:00
|
|
|
func NewClient(tlsOpts *tlsopts.Options, obs ...Observer) Client {
|
2019-03-22 17:09:37 +00:00
|
|
|
return NewClientWithTimeout(tlsOpts, defaultRequestTimeout, obs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientWithTimeout returns a transport client with a specified timeout for requests
|
|
|
|
func NewClientWithTimeout(tlsOpts *tlsopts.Options, requestTimeout time.Duration, obs ...Observer) Client {
|
2018-12-22 04:51:42 +00:00
|
|
|
return &Transport{
|
2019-03-22 17:09:37 +00:00
|
|
|
tlsOpts: tlsOpts,
|
|
|
|
requestTimeout: requestTimeout,
|
|
|
|
observers: obs,
|
2018-12-22 04:51:42 +00:00
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:34:08 +00:00
|
|
|
// DialNode returns a grpc connection with tls to a node.
|
|
|
|
//
|
|
|
|
// Use this method for communicating with nodes as it is more secure than
|
|
|
|
// DialAddress. The connection will be established successfully only if the
|
|
|
|
// target node has the private key for the requested node ID.
|
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
|
|
|
|
2019-03-04 20:03:33 +00:00
|
|
|
dialOption, err := transport.tlsOpts.DialOption(node.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
options := append([]grpc.DialOption{
|
2019-03-04 20:03:33 +00:00
|
|
|
dialOption,
|
2019-02-26 18:35:16 +00:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.FailOnNonTempDialError(true),
|
2019-03-22 17:09:37 +00:00
|
|
|
grpc.WithUnaryInterceptor(InvokeTimeout{transport.requestTimeout}.Intercept),
|
2019-02-26 18:35:16 +00:00
|
|
|
}, opts...)
|
2018-11-20 16:54:52 +00:00
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, defaultDialTimeout)
|
2019-03-20 10:11:07 +00:00
|
|
|
defer cancel()
|
2018-12-22 04:51:42 +00:00
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
conn, err = grpc.DialContext(timedCtx, node.GetAddress().Address, options...)
|
2018-12-22 04:51:42 +00:00
|
|
|
if err != nil {
|
2019-02-06 13:19:14 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-19 17:37:26 +00:00
|
|
|
alertFail(timedCtx, transport.observers, node, err)
|
2018-12-22 04:51:42 +00:00
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
alertSuccess(timedCtx, transport.observers, node)
|
2018-12-22 04:51:42 +00:00
|
|
|
|
2019-01-08 08:22:54 +00:00
|
|
|
return conn, nil
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:34:08 +00:00
|
|
|
// DialAddress returns a grpc connection with tls to an IP address.
|
|
|
|
//
|
|
|
|
// Do not use this method unless having a good reason. In most cases DialNode
|
|
|
|
// should be used for communicating with nodes as it is more secure than
|
|
|
|
// DialAddress.
|
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)
|
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
options := append([]grpc.DialOption{
|
2019-03-04 20:03:33 +00:00
|
|
|
transport.tlsOpts.DialUnverifiedIDOption(),
|
2019-02-26 18:35:16 +00:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.FailOnNonTempDialError(true),
|
2019-03-22 17:09:37 +00:00
|
|
|
grpc.WithUnaryInterceptor(InvokeTimeout{transport.requestTimeout}.Intercept),
|
2019-02-26 18:35:16 +00:00
|
|
|
}, opts...)
|
2019-02-11 11:17:32 +00:00
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, defaultDialTimeout)
|
2019-03-20 10:11:07 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err = grpc.DialContext(timedCtx, address, options...)
|
2019-02-06 13:19:14 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-08 08:22:54 +00:00
|
|
|
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 {
|
2019-02-11 11:17:32 +00:00
|
|
|
return transport.tlsOpts.Ident
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
// WithObservers returns a new transport including the listed observers.
|
|
|
|
func (transport *Transport) WithObservers(obs ...Observer) *Transport {
|
2019-03-22 17:09:37 +00:00
|
|
|
tr := &Transport{tlsOpts: transport.tlsOpts, requestTimeout: transport.requestTimeout}
|
2019-02-08 09:25:13 +00:00
|
|
|
tr.observers = append(tr.observers, transport.observers...)
|
|
|
|
tr.observers = append(tr.observers, obs...)
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|