Refactor identifier handling: replace Provider and Subject fields with a single Key field in Identifier struct, update related message structures, and adjust parsing logic
This commit is contained in:
@@ -18,9 +18,7 @@ type GRPCControlServer struct {
|
||||
}
|
||||
|
||||
func NewGRPCControlServer(m *manager.Manager) *GRPCControlServer {
|
||||
return &GRPCControlServer{
|
||||
manager: m,
|
||||
}
|
||||
return &GRPCControlServer{manager: m}
|
||||
}
|
||||
|
||||
func (s *GRPCControlServer) StartStream(_ context.Context, _ *pb.StartStreamRequest) (*pb.StartStreamResponse, error) {
|
||||
@@ -28,7 +26,6 @@ func (s *GRPCControlServer) StartStream(_ context.Context, _ *pb.StartStreamRequ
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to start stream: %w", err)
|
||||
}
|
||||
|
||||
return &pb.StartStreamResponse{StreamUuid: streamID.String()}, nil
|
||||
}
|
||||
|
||||
@@ -38,19 +35,18 @@ func (s *GRPCControlServer) ConfigureStream(_ context.Context, req *pb.Configure
|
||||
return nil, status.Errorf(codes.InvalidArgument, "invalid stream_uuid %q: %v", req.StreamUuid, err)
|
||||
}
|
||||
|
||||
// Transform identifiers from protobuf to domain format
|
||||
var ids []domain.Identifier
|
||||
for _, i := range req.Identifiers {
|
||||
ids = append(ids, domain.Identifier{
|
||||
Provider: i.Provider,
|
||||
Subject: i.Subject,
|
||||
})
|
||||
for _, in := range req.Identifiers {
|
||||
id, e := domain.ParseIdentifier(in.Key)
|
||||
if e != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "invalid identifier %q: %v", in.Key, e)
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
|
||||
if err := s.manager.ConfigureStream(streamID, ids); err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "configure failed: %v", err)
|
||||
}
|
||||
|
||||
return &pb.ConfigureStreamResponse{}, nil
|
||||
}
|
||||
|
||||
@@ -59,11 +55,8 @@ func (s *GRPCControlServer) StopStream(_ context.Context, req *pb.StopStreamRequ
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "invalid stream_uuid %q: %v", req.StreamUuid, err)
|
||||
}
|
||||
|
||||
err = s.manager.StopStream(streamID) // Should only error if the stream doesn't exist
|
||||
if err != nil {
|
||||
if err := s.manager.StopStream(streamID); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to stop stream: %v", err)
|
||||
}
|
||||
|
||||
return &pb.StopStreamResponse{}, nil
|
||||
}
|
||||
|
||||
@@ -14,9 +14,7 @@ type GRPCStreamingServer struct {
|
||||
}
|
||||
|
||||
func NewGRPCStreamingServer(m *manager.Manager) *GRPCStreamingServer {
|
||||
return &GRPCStreamingServer{
|
||||
manager: m,
|
||||
}
|
||||
return &GRPCStreamingServer{manager: m}
|
||||
}
|
||||
|
||||
func (s *GRPCStreamingServer) ConnectStream(req *pb.ConnectStreamRequest, stream pb.DataServiceStreaming_ConnectStreamServer) error {
|
||||
@@ -39,17 +37,11 @@ func (s *GRPCStreamingServer) ConnectStream(req *pb.ConnectStreamRequest, stream
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := stream.Send(&pb.Message{
|
||||
Identifier: &pb.Identifier{
|
||||
Provider: msg.Identifier.Provider,
|
||||
Subject: msg.Identifier.Subject,
|
||||
},
|
||||
Payload: msg.Payload,
|
||||
Encoding: string(msg.Encoding),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if err := stream.Send(&pb.Message{
|
||||
Identifier: &pb.Identifier{Key: msg.Identifier.Key()},
|
||||
Payload: msg.Payload,
|
||||
Encoding: string(msg.Encoding),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
pb "gitlab.michelsen.id/phillmichelsen/tessera/pkg/pb/data_service"
|
||||
@@ -22,7 +23,6 @@ func NewSocketStreamingServer(m *manager.Manager) *SocketStreamingServer {
|
||||
return &SocketStreamingServer{manager: m}
|
||||
}
|
||||
|
||||
// Accepts connections and hands each off to handleConnection.
|
||||
func (s *SocketStreamingServer) Serve(lis net.Listener) error {
|
||||
for {
|
||||
conn, err := lis.Accept()
|
||||
@@ -43,16 +43,16 @@ func (s *SocketStreamingServer) handleConnection(conn net.Conn) {
|
||||
}
|
||||
}()
|
||||
|
||||
// Low-latency socket hints (best-effort).
|
||||
if tc, ok := conn.(*net.TCPConn); ok {
|
||||
_ = tc.SetNoDelay(true)
|
||||
_ = tc.SetWriteBuffer(512 * 1024)
|
||||
_ = tc.SetReadBuffer(512 * 1024)
|
||||
_ = tc.SetKeepAlive(true)
|
||||
_ = tc.SetKeepAlivePeriod(30 * time.Second)
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(conn)
|
||||
|
||||
// Protocol header: first line is the stream UUID.
|
||||
raw, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
fmt.Printf("read stream UUID error: %v\n", err)
|
||||
@@ -74,9 +74,8 @@ func (s *SocketStreamingServer) handleConnection(conn net.Conn) {
|
||||
defer s.manager.DisconnectStream(streamUUID)
|
||||
|
||||
writer := bufio.NewWriterSize(conn, 256*1024)
|
||||
defer func(writer *bufio.Writer) {
|
||||
err := writer.Flush()
|
||||
if err != nil {
|
||||
defer func(w *bufio.Writer) {
|
||||
if err := w.Flush(); err != nil {
|
||||
fmt.Printf("final flush error: %v\n", err)
|
||||
}
|
||||
}(writer)
|
||||
@@ -85,27 +84,20 @@ func (s *SocketStreamingServer) handleConnection(conn net.Conn) {
|
||||
batch := 0
|
||||
|
||||
for msg := range outCh {
|
||||
// Build protobuf payload.
|
||||
message := pb.Message{
|
||||
Identifier: &pb.Identifier{
|
||||
Provider: msg.Identifier.Provider,
|
||||
Subject: msg.Identifier.Subject,
|
||||
},
|
||||
Payload: msg.Payload, // []byte
|
||||
Encoding: string(msg.Encoding), // e.g., "application/json"
|
||||
m := pb.Message{
|
||||
Identifier: &pb.Identifier{Key: msg.Identifier.Key()},
|
||||
Payload: msg.Payload,
|
||||
Encoding: string(msg.Encoding),
|
||||
}
|
||||
|
||||
// Marshal protobuf.
|
||||
// Use MarshalAppend to reuse capacity and avoid an extra alloc.
|
||||
size := proto.Size(&message)
|
||||
size := proto.Size(&m)
|
||||
buf := make([]byte, 0, size)
|
||||
b, err := proto.MarshalOptions{}.MarshalAppend(buf, &message)
|
||||
b, err := proto.MarshalOptions{}.MarshalAppend(buf, &m)
|
||||
if err != nil {
|
||||
fmt.Printf("proto marshal error: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Fixed 4-byte big-endian length prefix.
|
||||
var hdr [4]byte
|
||||
if len(b) > int(^uint32(0)) {
|
||||
fmt.Printf("message too large: %d bytes\n", len(b))
|
||||
@@ -113,7 +105,6 @@ func (s *SocketStreamingServer) handleConnection(conn net.Conn) {
|
||||
}
|
||||
binary.BigEndian.PutUint32(hdr[:], uint32(len(b)))
|
||||
|
||||
// Write frame: [len][bytes].
|
||||
if _, err := writer.Write(hdr[:]); err != nil {
|
||||
if err == io.EOF {
|
||||
return
|
||||
@@ -139,7 +130,6 @@ func (s *SocketStreamingServer) handleConnection(conn net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
// Final flush when channel closes.
|
||||
if err := writer.Flush(); err != nil {
|
||||
fmt.Printf("final flush error: %v\n", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user