2018-06-29 19:28:06 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// 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"
|
2018-11-06 17:49:17 +00:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
Identity() *provider.FullIdentity
|
|
|
|
}
|
|
|
|
|
2018-06-29 19:28:06 +01:00
|
|
|
// Transport interface structure
|
|
|
|
type Transport struct {
|
2018-08-24 05:01:03 +01:00
|
|
|
identity *provider.FullIdentity
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
// NewClient returns a newly instantiated Transport Client
|
2018-11-06 17:49:17 +00:00
|
|
|
func NewClient(identity *provider.FullIdentity) Client {
|
2018-08-24 05:01:03 +01:00
|
|
|
return &Transport{identity: identity}
|
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)
|
|
|
|
|
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-19 14:40:01 +00:00
|
|
|
dialOpt, err := transport.identity.DialOption(node.GetId())
|
2018-11-09 22:08:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-19 14:40:01 +00:00
|
|
|
|
|
|
|
options := append([]grpc.DialOption{dialOpt}, opts...)
|
2018-11-20 16:54:52 +00:00
|
|
|
|
|
|
|
ctx, cf := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cf()
|
|
|
|
return grpc.DialContext(ctx, node.GetAddress().Address, options...)
|
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-19 14:40:01 +00:00
|
|
|
dialOpt, err := transport.identity.DialOption("")
|
2018-08-24 05:01:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-19 14:40:01 +00:00
|
|
|
|
|
|
|
options := append([]grpc.DialOption{dialOpt}, opts...)
|
|
|
|
return grpc.Dial(address, options...)
|
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
|
2018-11-19 14:40:01 +00:00
|
|
|
func (transport *Transport) Identity() *provider.FullIdentity {
|
|
|
|
return transport.identity
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements io.closer, closing the transport connection(s)
|