-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontendgrpc.go
151 lines (133 loc) · 4.95 KB
/
frontendgrpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package minimatch
import (
"context"
"errors"
"github.com/rs/xid"
"github.com/sethvargo/go-retry"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
pb "github.com/castaneai/minimatch/gen/openmatch"
"github.com/castaneai/minimatch/pkg/statestore"
)
type FrontendGPRCService struct {
pb.UnimplementedFrontendServiceServer
store statestore.FrontendStore
options *frontendOptions
}
func NewFrontendGPRCService(store statestore.FrontendStore, opts ...FrontendOption) pb.FrontendServiceServer {
options := defaultFrontendOptions()
for _, opt := range opts {
opt.apply(options)
}
return &FrontendGPRCService{store: store, options: options}
}
func (s *FrontendGPRCService) CreateTicket(ctx context.Context, req *pb.CreateTicketRequest) (*pb.Ticket, error) {
ticket, ok := proto.Clone(req.Ticket).(*pb.Ticket)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to clone input ticket proto")
}
ticket.Id = xid.New().String()
ticket.CreateTime = timestamppb.Now()
ttlVal, err := anypb.New(wrapperspb.Int64(s.options.ticketTTL.Nanoseconds()))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create ttl value")
}
ticket.PersistentField = map[string]*anypb.Any{
persistentFieldKeyTicketTTL: ttlVal,
}
if err := s.store.CreateTicket(ctx, ticket, s.options.ticketTTL); err != nil {
return nil, err
}
return ticket, nil
}
func (s *FrontendGPRCService) DeleteTicket(ctx context.Context, req *pb.DeleteTicketRequest) (*emptypb.Empty, error) {
if req.TicketId == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid ticket_id")
}
if err := s.store.DeleteTicket(ctx, req.TicketId); err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}
func (s *FrontendGPRCService) GetTicket(ctx context.Context, req *pb.GetTicketRequest) (*pb.Ticket, error) {
if req.TicketId == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid ticket_id")
}
ticket, err := s.store.GetTicket(ctx, req.TicketId)
if err != nil {
if errors.Is(err, statestore.ErrTicketNotFound) {
return nil, status.Errorf(codes.NotFound, "ticket id: %s not found", req.TicketId)
}
return nil, err
}
assignment, err := s.store.GetAssignment(ctx, req.TicketId)
if err != nil && !errors.Is(err, statestore.ErrAssignmentNotFound) {
return nil, err
}
if assignment != nil {
ticket.Assignment = assignment
}
return ticket, nil
}
func (s *FrontendGPRCService) WatchAssignments(req *pb.WatchAssignmentsRequest, stream grpc.ServerStreamingServer[pb.WatchAssignmentsResponse]) error {
if req.TicketId == "" {
return status.Errorf(codes.InvalidArgument, "invalid ticket_id")
}
onAssignmentChanged := func(as *pb.Assignment) error {
if err := stream.Send(&pb.WatchAssignmentsResponse{Assignment: as}); err != nil {
return err
}
return nil
}
var prev *pb.Assignment
backoff := newWatchAssignmentBackoff()
if err := retry.Do(stream.Context(), backoff, func(ctx context.Context) error {
assignment, err := s.store.GetAssignment(ctx, req.TicketId)
if err != nil {
if errors.Is(err, statestore.ErrAssignmentNotFound) {
return retry.RetryableError(err)
}
return err
}
if (prev == nil && assignment != nil) || !proto.Equal(prev, assignment) {
prev = assignment
if err := onAssignmentChanged(assignment); err != nil {
return err
}
}
return retry.RetryableError(errors.New("assignment unchanged"))
}); err != nil {
return err
}
return nil
}
func (s *FrontendGPRCService) AcknowledgeBackfill(ctx context.Context, req *pb.AcknowledgeBackfillRequest) (*pb.AcknowledgeBackfillResponse, error) {
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (s *FrontendGPRCService) CreateBackfill(ctx context.Context, req *pb.CreateBackfillRequest) (*pb.Backfill, error) {
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (s *FrontendGPRCService) DeleteBackfill(ctx context.Context, req *pb.DeleteBackfillRequest) (*emptypb.Empty, error) {
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (s *FrontendGPRCService) GetBackfill(ctx context.Context, req *pb.GetBackfillRequest) (*pb.Backfill, error) {
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (s *FrontendGPRCService) UpdateBackfill(ctx context.Context, req *pb.UpdateBackfillRequest) (*pb.Backfill, error) {
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (s *FrontendGPRCService) DeindexTicket(ctx context.Context, req *pb.DeindexTicketRequest) (*pb.DeindexTicketResponse, error) {
if req.TicketId == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid ticket_id")
}
if err := s.store.DeindexTicket(ctx, req.TicketId); err != nil {
return nil, err
}
return &pb.DeindexTicketResponse{}, nil
}