storj/storagenode/contact/endpoint.go
paul cannon adfa16188b pkg/contact: bare-bones service and endpoint (#2941)
* pkg/contact: bare-bones service and endpoint

* split contact package into satellite and node

* use new contact protobuf types
2019-09-04 11:29:34 -07:00

48 lines
1.3 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package contact
import (
"context"
"time"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/pb"
)
// Endpoint implements the contact service Endpoints
type Endpoint struct {
log *zap.Logger
service *Service
}
// NewEndpoint returns a new contact service endpoint
func NewEndpoint(log *zap.Logger, service *Service) *Endpoint {
return &Endpoint{
log: log,
service: service,
}
}
// Ping provides an easy way to verify a node is online and accepting requests
func (endpoint *Endpoint) Ping(ctx context.Context, req *pb.ContactPingRequest) (_ *pb.ContactPingResponse, err error) {
defer mon.Task()(&ctx)(&err)
p, ok := peer.FromContext(ctx)
if !ok {
return nil, status.Error(codes.Internal, "unable to get grpc peer from context")
}
peerID, err := identity.PeerIdentityFromPeer(p)
if err != nil {
return nil, status.Error(codes.Unauthenticated, err.Error())
}
endpoint.log.Debug("pinged", zap.Stringer("by", peerID.ID), zap.Stringer("srcAddr", p.Addr))
endpoint.service.wasPinged(time.Now(), peerID.ID, p.Addr.String())
return &pb.ContactPingResponse{}, nil
}