-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontend.go
188 lines (163 loc) · 6.31 KB
/
frontend.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package minimatch
import (
"context"
"errors"
"fmt"
"time"
"connectrpc.com/connect"
"github.com/rs/xid"
"github.com/sethvargo/go-retry"
"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"
)
const (
watchAssignmentInterval = 100 * time.Millisecond
defaultTicketTTL = 10 * time.Minute
persistentFieldKeyTicketTTL = "ttl"
)
type FrontendOption interface {
apply(options *frontendOptions)
}
type FrontendOptionFunc func(options *frontendOptions)
func (f FrontendOptionFunc) apply(options *frontendOptions) {
f(options)
}
type frontendOptions struct {
ticketTTL time.Duration
}
func defaultFrontendOptions() *frontendOptions {
return &frontendOptions{
ticketTTL: defaultTicketTTL,
}
}
func WithTicketTTL(ticketTTL time.Duration) FrontendOption {
return FrontendOptionFunc(func(options *frontendOptions) {
options.ticketTTL = ticketTTL
})
}
type FrontendService struct {
store statestore.FrontendStore
options *frontendOptions
}
func NewFrontendService(store statestore.FrontendStore, opts ...FrontendOption) *FrontendService {
options := defaultFrontendOptions()
for _, opt := range opts {
opt.apply(options)
}
return &FrontendService{
store: store,
options: options,
}
}
func (s *FrontendService) CreateTicket(ctx context.Context, req *connect.Request[pb.CreateTicketRequest]) (*connect.Response[pb.Ticket], error) {
ticket, ok := proto.Clone(req.Msg.Ticket).(*pb.Ticket)
if !ok {
return nil, connect.NewError(connect.CodeInternal, errors.New("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, connect.NewError(connect.CodeInternal, errors.New("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 connect.NewResponse(ticket), nil
}
func (s *FrontendService) DeleteTicket(ctx context.Context, req *connect.Request[pb.DeleteTicketRequest]) (*connect.Response[emptypb.Empty], error) {
if req.Msg.TicketId == "" {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("invalid ticket_id"))
}
if err := s.store.DeleteTicket(ctx, req.Msg.TicketId); err != nil {
return nil, err
}
return connect.NewResponse(&emptypb.Empty{}), nil
}
func (s *FrontendService) GetTicket(ctx context.Context, req *connect.Request[pb.GetTicketRequest]) (*connect.Response[pb.Ticket], error) {
if req.Msg.TicketId == "" {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("invalid ticket_id"))
}
ticket, err := s.store.GetTicket(ctx, req.Msg.TicketId)
if err != nil {
if errors.Is(err, statestore.ErrTicketNotFound) {
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("ticket id: %s not found", req.Msg.TicketId))
}
return nil, err
}
assignment, err := s.store.GetAssignment(ctx, req.Msg.TicketId)
if err != nil && !errors.Is(err, statestore.ErrAssignmentNotFound) {
return nil, err
}
if assignment != nil {
ticket.Assignment = assignment
}
return connect.NewResponse(ticket), nil
}
func (s *FrontendService) WatchAssignments(ctx context.Context, req *connect.Request[pb.WatchAssignmentsRequest], stream *connect.ServerStream[pb.WatchAssignmentsResponse]) error {
if req.Msg.TicketId == "" {
return connect.NewError(connect.CodeInvalidArgument, errors.New("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(ctx, backoff, func(ctx context.Context) error {
assignment, err := s.store.GetAssignment(ctx, req.Msg.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 *FrontendService) AcknowledgeBackfill(ctx context.Context, request *connect.Request[pb.AcknowledgeBackfillRequest]) (*connect.Response[pb.AcknowledgeBackfillResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("not implemented"))
}
func (s *FrontendService) CreateBackfill(ctx context.Context, request *connect.Request[pb.CreateBackfillRequest]) (*connect.Response[pb.Backfill], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("not implemented"))
}
func (s *FrontendService) DeleteBackfill(ctx context.Context, request *connect.Request[pb.DeleteBackfillRequest]) (*connect.Response[emptypb.Empty], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("not implemented"))
}
func (s *FrontendService) GetBackfill(ctx context.Context, request *connect.Request[pb.GetBackfillRequest]) (*connect.Response[pb.Backfill], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("not implemented"))
}
func (s *FrontendService) UpdateBackfill(ctx context.Context, request *connect.Request[pb.UpdateBackfillRequest]) (*connect.Response[pb.Backfill], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("not implemented"))
}
func (s *FrontendService) DeindexTicket(ctx context.Context, req *connect.Request[pb.DeindexTicketRequest]) (*connect.Response[pb.DeindexTicketResponse], error) {
if req.Msg.TicketId == "" {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("invalid ticket_id"))
}
if err := s.store.DeindexTicket(ctx, req.Msg.TicketId); err != nil {
return nil, err
}
return connect.NewResponse(&pb.DeindexTicketResponse{}), nil
}
func newWatchAssignmentBackoff() retry.Backoff {
return retry.NewConstant(watchAssignmentInterval)
}