Nexus/pkg/internal/directory/service_rpc.go

119 lines
3.3 KiB
Go
Raw Normal View History

2024-10-19 14:36:33 +00:00
package directory
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2024-10-19 14:36:33 +00:00
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
)
type ServiceRpcServer struct {
2024-10-21 14:07:36 +00:00
proto.UnimplementedDirectoryServiceServer
2024-10-19 14:36:33 +00:00
}
func instantiationService(in *ServiceInstance) *proto.ServiceInfo {
2024-10-19 14:36:33 +00:00
if in == nil {
return nil
}
return &proto.ServiceInfo{
Id: in.ID,
Type: in.Type,
Label: in.Label,
GrpcAddr: in.GrpcAddr,
HttpAddr: in.HttpAddr,
}
}
func (v *ServiceRpcServer) GetService(ctx context.Context, request *proto.GetServiceRequest) (*proto.GetServiceResponse, error) {
2024-10-19 14:36:33 +00:00
if request.Id != nil {
out := GetServiceInstance(request.GetId())
return &proto.GetServiceResponse{
Data: instantiationService(out),
2024-10-19 14:36:33 +00:00
}, nil
}
if request.Type != nil {
out := GetServiceInstanceByType(request.GetType())
return &proto.GetServiceResponse{
Data: instantiationService(out),
2024-10-19 14:36:33 +00:00
}, nil
}
return nil, fmt.Errorf("no filter condition is provided")
}
func (v *ServiceRpcServer) ListService(ctx context.Context, request *proto.ListServiceRequest) (*proto.ListServiceResponse, error) {
2024-10-19 14:36:33 +00:00
var out []*ServiceInstance
if request.Type != nil {
out = ListServiceInstanceByType(request.GetType())
} else {
out = ListServiceInstance()
}
return &proto.ListServiceResponse{
Data: lo.Map(out, func(item *ServiceInstance, index int) *proto.ServiceInfo {
return instantiationService(item)
2024-10-19 14:36:33 +00:00
}),
}, nil
}
func (v *ServiceRpcServer) AddService(ctx context.Context, info *proto.ServiceInfo) (*proto.AddServiceResponse, error) {
clientId, err := GetClientId(ctx)
if err != nil {
return nil, err
}
if info.GetId() != clientId {
return nil, status.Errorf(codes.InvalidArgument, "client_id mismatch in metadata")
2024-10-24 14:40:14 +00:00
} else if len(clientId) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "client_id must not be blank")
}
2024-10-19 14:36:33 +00:00
in := &ServiceInstance{
ID: clientId,
2024-10-19 14:36:33 +00:00
Type: info.GetType(),
Label: info.GetLabel(),
GrpcAddr: info.GetGrpcAddr(),
HttpAddr: info.HttpAddr,
}
err = AddServiceInstance(in)
if err == nil {
log.Info().Str("id", clientId).Str("label", info.GetLabel()).Msg("New service registered")
} else {
log.Error().Str("id", clientId).Str("label", info.GetLabel()).Err(err).Msg("Unable to register a service")
}
2024-10-19 14:36:33 +00:00
return &proto.AddServiceResponse{
IsSuccess: err == nil,
2024-10-19 14:36:33 +00:00
}, nil
}
func (v *ServiceRpcServer) RemoveService(ctx context.Context, request *proto.RemoveServiceRequest) (*proto.RemoveServiceResponse, error) {
err := RemoveServiceInstance(request.GetId())
if err == nil {
log.Info().Str("id", request.GetId()).Msg("A service removed")
} else {
log.Error().Str("id", request.GetId()).Err(err).Msg("Unable to remove a service")
}
2024-10-19 14:36:33 +00:00
return &proto.RemoveServiceResponse{
IsSuccess: err == nil,
2024-10-19 14:36:33 +00:00
}, nil
}
func (v *ServiceRpcServer) BroadcastEvent(ctx context.Context, in *proto.EventInfo) (*proto.EventResponse, error) {
services := ListServiceInstance()
log.Debug().
Int("destinations", len(services)).
Str("event", in.GetEvent()).
Msg("Broadcasting event...")
for _, service := range services {
conn, err := service.GetGrpcConn()
if err != nil {
continue
}
_, _ = proto.NewDirectoryServiceClient(conn).BroadcastEvent(ctx, in)
}
return &proto.EventResponse{}, nil
}