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"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/rpc/rpcpeer"
|
|
|
|
"storj.io/common/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
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PingNode provides an easy way to verify a node is online and accepting requests.
|
2019-09-06 17:14:03 +01:00
|
|
|
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
|
|
|
|
}
|