2019-09-04 19:29:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package contact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-06 17:14:03 +01:00
|
|
|
"sync"
|
2019-09-04 19:29:34 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc/rpcpeer"
|
|
|
|
"storj.io/storj/pkg/rpc/rpcstatus"
|
2019-09-04 19:29:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Endpoint implements the contact service Endpoints
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-09-04 19:29:34 +01:00
|
|
|
type Endpoint struct {
|
2019-09-06 17:14:03 +01:00
|
|
|
log *zap.Logger
|
|
|
|
pingStats *PingStats
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:31:39 +01:00
|
|
|
// PingStats contains information regarding when the node was last pinged
|
2019-09-06 17:14:03 +01:00
|
|
|
type PingStats struct {
|
2019-10-03 19:31:39 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
lastPinged time.Time
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpoint returns a new contact service endpoint
|
2019-09-06 17:14:03 +01:00
|
|
|
func NewEndpoint(log *zap.Logger, pingStats *PingStats) *Endpoint {
|
2019-09-04 19:29:34 +01:00
|
|
|
return &Endpoint{
|
2019-09-06 17:14:03 +01:00
|
|
|
log: log,
|
|
|
|
pingStats: pingStats,
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:14:03 +01:00
|
|
|
// PingNode provides an easy way to verify a node is online and accepting requests
|
|
|
|
func (endpoint *Endpoint) PingNode(ctx context.Context, req *pb.ContactPingRequest) (_ *pb.ContactPingResponse, err error) {
|
2019-09-04 19:29:34 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-09-19 05:46:39 +01:00
|
|
|
peer, err := rpcpeer.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
peerID, err := identity.PeerIdentityFromPeer(peer)
|
2019-09-04 19:29:34 +01:00
|
|
|
if err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, err.Error())
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
endpoint.log.Debug("pinged", zap.Stringer("by", peerID.ID), zap.Stringer("srcAddr", peer.Addr))
|
2019-10-03 19:31:39 +01:00
|
|
|
endpoint.pingStats.WasPinged(time.Now())
|
2019-09-04 19:29:34 +01:00
|
|
|
return &pb.ContactPingResponse{}, nil
|
|
|
|
}
|
2019-09-06 17:14:03 +01:00
|
|
|
|
|
|
|
// WhenLastPinged returns last time someone pinged this node.
|
2019-10-03 19:31:39 +01:00
|
|
|
func (stats *PingStats) WhenLastPinged() (when time.Time) {
|
2019-09-06 17:14:03 +01:00
|
|
|
stats.mu.Lock()
|
|
|
|
defer stats.mu.Unlock()
|
2019-10-03 19:31:39 +01:00
|
|
|
return stats.lastPinged
|
2019-09-06 17:14:03 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 21:41:43 +01:00
|
|
|
// WasPinged notifies the service it has been remotely pinged.
|
2019-10-03 19:31:39 +01:00
|
|
|
func (stats *PingStats) WasPinged(when time.Time) {
|
2019-09-06 17:14:03 +01:00
|
|
|
stats.mu.Lock()
|
|
|
|
defer stats.mu.Unlock()
|
|
|
|
stats.lastPinged = when
|
|
|
|
}
|