2019-09-04 19:29:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package contact
|
|
|
|
|
|
|
|
import (
|
2019-09-19 20:56:34 +01:00
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2019-09-10 17:05:07 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-09-04 19:29:34 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc"
|
2019-09-04 19:29:34 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
2019-09-12 17:33:04 +01:00
|
|
|
// Error is the default error class for contact package.
|
2019-09-10 17:05:07 +01:00
|
|
|
var Error = errs.Class("contact")
|
|
|
|
|
2019-09-04 19:29:34 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
// Config contains configurable values for contact service
|
|
|
|
type Config struct {
|
|
|
|
ExternalAddress string `user:"true" help:"the public address of the node, useful for nodes behind NAT" default:""`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conn represents a connection
|
|
|
|
type Conn struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
conn *rpc.Conn
|
|
|
|
client rpc.NodesClient
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 17:33:04 +01:00
|
|
|
// Service is the contact service between storage nodes and satellites.
|
|
|
|
// It is responsible for updating general node information like address, capacity, and uptime.
|
|
|
|
// It is also responsible for updating peer identity information for verifying signatures from that node.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-09-04 19:29:34 +01:00
|
|
|
type Service struct {
|
2019-09-19 20:56:34 +01:00
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
self *overlay.NodeDossier
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
overlay *overlay.Service
|
|
|
|
peerIDs overlay.PeerIdentities
|
|
|
|
dialer rpc.Dialer
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 17:33:04 +01:00
|
|
|
// NewService creates a new contact service.
|
2019-09-19 05:46:39 +01:00
|
|
|
func NewService(log *zap.Logger, self *overlay.NodeDossier, overlay *overlay.Service, peerIDs overlay.PeerIdentities, dialer rpc.Dialer) *Service {
|
2019-09-04 19:29:34 +01:00
|
|
|
return &Service{
|
2019-09-19 05:46:39 +01:00
|
|
|
log: log,
|
|
|
|
self: self,
|
|
|
|
overlay: overlay,
|
|
|
|
peerIDs: peerIDs,
|
|
|
|
dialer: dialer,
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 20:56:34 +01:00
|
|
|
|
|
|
|
// Local returns the satellite node dossier
|
|
|
|
func (service *Service) Local() overlay.NodeDossier {
|
|
|
|
service.mutex.Lock()
|
|
|
|
defer service.mutex.Unlock()
|
|
|
|
return *service.self
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchInfo connects to a node and returns its node info.
|
|
|
|
func (service *Service) FetchInfo(ctx context.Context, target pb.Node) (_ *pb.InfoResponse, err error) {
|
|
|
|
conn, err := service.dialNode(ctx, target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
defer func() { err = errs.Combine(err, conn.Close()) }()
|
2019-09-19 20:56:34 +01:00
|
|
|
|
|
|
|
resp, err := conn.client.RequestInfo(ctx, &pb.InfoRequest{})
|
2019-09-19 05:46:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-19 20:56:34 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
return resp, nil
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// dialNode dials the specified node.
|
|
|
|
func (service *Service) dialNode(ctx context.Context, target pb.Node) (_ *Conn, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-09-19 05:46:39 +01:00
|
|
|
|
|
|
|
conn, err := service.dialer.DialNode(ctx, &target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
return &Conn{
|
2019-09-19 05:46:39 +01:00
|
|
|
conn: conn,
|
|
|
|
client: conn.NodesClient(),
|
2019-09-19 20:56:34 +01:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
// Close disconnects this connection.
|
|
|
|
func (conn *Conn) Close() error {
|
2019-09-19 20:56:34 +01:00
|
|
|
return conn.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes resources
|
|
|
|
func (service *Service) Close() error { return nil }
|