netbird/signal/server/signal.go

174 lines
6.0 KiB
Go
Raw Permalink Normal View History

package server
2021-05-01 12:45:37 +02:00
import (
"context"
"fmt"
2024-06-13 01:20:46 +02:00
"io"
"time"
"github.com/netbirdio/signal-dispatcher/dispatcher"
2021-05-01 12:45:37 +02:00
log "github.com/sirupsen/logrus"
2024-06-13 01:20:46 +02:00
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
2021-05-01 12:45:37 +02:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
2024-06-13 01:20:46 +02:00
"github.com/netbirdio/netbird/signal/metrics"
"github.com/netbirdio/netbird/signal/peer"
"github.com/netbirdio/netbird/signal/proto"
)
const (
labelType = "type"
labelTypeError = "error"
labelTypeNotConnected = "not_connected"
labelTypeNotRegistered = "not_registered"
2024-07-16 10:14:30 +02:00
labelTypeStream = "stream"
labelTypeMessage = "message"
2024-06-13 01:20:46 +02:00
labelError = "error"
labelErrorMissingId = "missing_id"
labelErrorMissingMeta = "missing_meta"
labelErrorFailedHeader = "failed_header"
2024-07-16 16:55:57 +02:00
labelRegistrationStatus = "status"
labelRegistrationFound = "found"
labelRegistrationNotFound = "not_found"
2021-05-01 12:45:37 +02:00
)
// Server an instance of a Signal server
type Server struct {
2021-05-01 12:45:37 +02:00
registry *peer.Registry
proto.UnimplementedSignalExchangeServer
2024-08-30 15:44:07 +02:00
dispatcher *dispatcher.Dispatcher
metrics *metrics.AppMetrics
2021-05-01 12:45:37 +02:00
}
// NewServer creates a new Signal server
func NewServer(ctx context.Context, meter metric.Meter) (*Server, error) {
2024-06-13 01:20:46 +02:00
appMetrics, err := metrics.NewAppMetrics(meter)
if err != nil {
return nil, fmt.Errorf("creating app metrics: %v", err)
}
2024-09-29 20:23:34 +02:00
dispatcher, err := dispatcher.NewDispatcher(ctx, meter)
2024-08-30 15:44:07 +02:00
if err != nil {
return nil, fmt.Errorf("creating dispatcher: %v", err)
}
2024-06-13 01:20:46 +02:00
s := &Server{
2024-08-30 15:44:07 +02:00
dispatcher: dispatcher,
registry: peer.NewRegistry(appMetrics),
metrics: appMetrics,
2021-05-01 12:45:37 +02:00
}
2024-06-13 01:20:46 +02:00
return s, nil
2021-05-01 12:45:37 +02:00
}
// Send forwards a message to the signal peer
func (s *Server) Send(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
2024-08-30 15:44:07 +02:00
log.Debugf("received a new message to send from peer [%s] to peer [%s]", msg.Key, msg.RemoteKey)
2024-06-13 01:20:46 +02:00
2024-08-30 15:44:07 +02:00
if _, found := s.registry.Get(msg.RemoteKey); found {
s.forwardMessageToPeer(ctx, msg)
return &proto.EncryptedMessage{}, nil
2021-05-01 12:45:37 +02:00
}
2024-08-30 15:44:07 +02:00
return s.dispatcher.SendMessage(context.Background(), msg)
2021-05-01 12:45:37 +02:00
}
// ConnectStream connects to the exchange stream
func (s *Server) ConnectStream(stream proto.SignalExchange_ConnectStreamServer) error {
2024-08-30 15:44:07 +02:00
p, err := s.RegisterPeer(stream)
2021-05-01 12:45:37 +02:00
if err != nil {
return err
}
2024-08-30 15:44:07 +02:00
defer s.DeregisterPeer(p)
2024-06-13 01:20:46 +02:00
2024-08-30 15:44:07 +02:00
// needed to confirm that the peer has been registered so that the client can proceed
header := metadata.Pairs(proto.HeaderRegistered, "1")
err = stream.SendHeader(header)
if err != nil {
2024-06-13 01:20:46 +02:00
s.metrics.RegistrationFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelError, labelErrorFailedHeader)))
return err
}
2024-08-30 15:44:07 +02:00
log.Debugf("peer connected [%s] [streamID %d] ", p.Id, p.StreamID)
2021-05-01 12:45:37 +02:00
for {
2024-08-30 15:44:07 +02:00
// read incoming messages
2021-05-01 12:45:37 +02:00
msg, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
2024-06-13 01:20:46 +02:00
2024-08-30 15:44:07 +02:00
log.Debugf("Received a response from peer [%s] to peer [%s]", msg.Key, msg.RemoteKey)
_, err = s.dispatcher.SendMessage(stream.Context(), msg)
if err != nil {
log.Debugf("error while sending message from peer [%s] to peer [%s] %v", msg.Key, msg.RemoteKey, err)
2021-05-01 12:45:37 +02:00
}
}
2024-08-30 15:44:07 +02:00
2021-05-01 12:45:37 +02:00
<-stream.Context().Done()
return stream.Context().Err()
}
2024-08-30 15:44:07 +02:00
func (s *Server) RegisterPeer(stream proto.SignalExchange_ConnectStreamServer) (*peer.Peer, error) {
log.Debugf("registering new peer")
2021-05-01 12:45:37 +02:00
if meta, hasMeta := metadata.FromIncomingContext(stream.Context()); hasMeta {
if id, found := meta[proto.HeaderId]; found {
p := peer.NewPeer(id[0], stream)
2024-06-13 01:20:46 +02:00
2021-05-01 12:45:37 +02:00
s.registry.Register(p)
2024-08-30 15:44:07 +02:00
s.dispatcher.ListenForMessages(stream.Context(), p.Id, s.forwardMessageToPeer)
2021-05-01 12:45:37 +02:00
return p, nil
} else {
2024-06-13 01:20:46 +02:00
s.metrics.RegistrationFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelError, labelErrorMissingId)))
2021-05-01 12:45:37 +02:00
return nil, status.Errorf(codes.FailedPrecondition, "missing connection header: "+proto.HeaderId)
}
} else {
2024-06-13 01:20:46 +02:00
s.metrics.RegistrationFailures.Add(stream.Context(), 1, metric.WithAttributes(attribute.String(labelError, labelErrorMissingMeta)))
2021-05-01 12:45:37 +02:00
return nil, status.Errorf(codes.FailedPrecondition, "missing connection stream meta")
}
}
2024-08-30 15:44:07 +02:00
func (s *Server) DeregisterPeer(p *peer.Peer) {
log.Debugf("peer disconnected [%s] [streamID %d] ", p.Id, p.StreamID)
s.registry.Deregister(p)
s.metrics.PeerConnectionDuration.Record(p.Stream.Context(), int64(time.Since(p.RegisteredAt).Seconds()))
}
func (s *Server) forwardMessageToPeer(ctx context.Context, msg *proto.EncryptedMessage) {
log.Debugf("forwarding a new message from peer [%s] to peer [%s]", msg.Key, msg.RemoteKey)
getRegistrationStart := time.Now()
// lookup the target peer where the message is going to
if dstPeer, found := s.registry.Get(msg.RemoteKey); found {
s.metrics.GetRegistrationDelay.Record(ctx, float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream), attribute.String(labelRegistrationStatus, labelRegistrationFound)))
start := time.Now()
// forward the message to the target peer
if err := dstPeer.Stream.Send(msg); err != nil {
log.Warnf("error while forwarding message from peer [%s] to peer [%s] %v", msg.Key, msg.RemoteKey, err)
// todo respond to the sender?
s.metrics.MessageForwardFailures.Add(ctx, 1, metric.WithAttributes(attribute.String(labelType, labelTypeError)))
} else {
// in milliseconds
s.metrics.MessageForwardLatency.Record(ctx, float64(time.Since(start).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream)))
s.metrics.MessagesForwarded.Add(ctx, 1)
}
} else {
s.metrics.GetRegistrationDelay.Record(ctx, float64(time.Since(getRegistrationStart).Nanoseconds())/1e6, metric.WithAttributes(attribute.String(labelType, labelTypeStream), attribute.String(labelRegistrationStatus, labelRegistrationNotFound)))
s.metrics.MessageForwardFailures.Add(ctx, 1, metric.WithAttributes(attribute.String(labelType, labelTypeNotConnected)))
log.Debugf("message from peer [%s] can't be forwarded to peer [%s] because destination peer is not connected", msg.Key, msg.RemoteKey)
// todo respond to the sender?
}
}