-
Notifications
You must be signed in to change notification settings - Fork 16
/
server_source.go
197 lines (174 loc) · 4.71 KB
/
server_source.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
189
190
191
192
193
194
195
196
197
package proximo
import (
"context"
"io"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/uw-labs/proximo/internal/id"
"github.com/uw-labs/proximo/proto"
"github.com/uw-labs/substrate"
"github.com/uw-labs/sync/gogroup"
)
var (
errStartedTwice = status.Error(codes.InvalidArgument, "consumption already started")
errInvalidConfirm = status.Error(codes.InvalidArgument, "invalid confirmation")
errNotConnected = status.Error(codes.InvalidArgument, "not connected to a topic")
errInvalidRequest = status.Error(codes.InvalidArgument, "invalid consumer request - this is possibly a bug in your client library")
)
type SourceServer struct {
SourceFactory AsyncSourceFactory
SkipDiscard bool
}
func (s *SourceServer) Consume(stream proto.MessageSource_ConsumeServer) error {
sCtx := stream.Context()
g, ctx := gogroup.New(sCtx)
confirmations := make(chan string)
startRequest := make(chan *proto.StartConsumeRequest)
toAck := make(chan *ackMsg)
acks := make(chan substrate.Message)
messages := make(chan substrate.Message)
g.Go(func() error {
return s.receiveConfirmations(ctx, stream, startRequest, confirmations)
})
g.Go(func() error {
return s.handleAcks(ctx, confirmations, acks, toAck)
})
g.Go(func() error {
return s.sendMessages(ctx, stream, messages, toAck)
})
g.Go(func() error {
var req *proto.StartConsumeRequest
select {
case req = <-startRequest:
case <-ctx.Done():
return nil
}
source, err := s.SourceFactory.NewAsyncSource(ctx, req)
if err != nil {
return err
}
defer source.Close()
return source.ConsumeMessages(ctx, messages, acks)
})
if err := g.Wait(); err != nil {
return err
}
return sCtx.Err()
}
// receiveSourceStream is a subset of proto.MessageSource_ConsumeServer that only exposes the receive method
type receiveSourceStream interface {
Recv() (*proto.ConsumerRequest, error)
}
// receiveConfirmations receives confirmations from the client
func (s *SourceServer) receiveConfirmations(ctx context.Context, stream receiveSourceStream, startRequest chan<- *proto.StartConsumeRequest, confirmations chan<- string) error {
started := false
for {
msg, err := stream.Recv()
if err != nil {
if err == io.EOF {
return nil
}
if strings.HasSuffix(err.Error(), "context canceled") {
return nil
}
return err
}
switch {
case msg.GetStartRequest() != nil:
if started {
return errStartedTwice
}
started = true
select {
case startRequest <- msg.GetStartRequest():
case <-ctx.Done():
return nil
}
case msg.GetConfirmation() != nil:
if !started {
return errInvalidConfirm
}
select {
case confirmations <- msg.GetConfirmation().GetMsgID():
case <-ctx.Done():
return nil
}
default:
return errInvalidRequest
}
}
}
// sendSourceStream is a subset of proto.MessageSource_ConsumeServer that only exposes the send method
type sendSourceStream interface {
Send(*proto.Message) error
}
// sendMessages sends messages to the client
func (s *SourceServer) sendMessages(ctx context.Context, stream sendSourceStream, messages <-chan substrate.Message, toAck chan<- *ackMsg) error {
for {
select {
case <-ctx.Done():
return nil
case msg := <-messages:
aMsg := &ackMsg{
id: id.Generate(),
msg: msg,
}
pMsg := &proto.Message{
Id: aMsg.id,
// Read the data now so that we can safely discard the payload
// once the message is passed to the ack handler.
Data: msg.Data(),
}
if km, ok := msg.(substrate.KeyedMessage); ok {
pMsg.Key = km.Key()
}
select {
case <-ctx.Done():
return nil
case toAck <- aMsg:
}
err := stream.Send(pMsg)
if err != nil {
if strings.HasSuffix(err.Error(), "context canceled") {
return nil
}
return err
}
}
}
}
func (s *SourceServer) handleAcks(ctx context.Context, confirmations <-chan string, acks chan<- substrate.Message, toAck <-chan *ackMsg) error {
ackMap := make(map[string]substrate.Message)
for {
select {
case <-ctx.Done():
return nil
case aMsg := <-toAck:
if !s.SkipDiscard {
if dMsg, ok := aMsg.msg.(substrate.DiscardableMessage); ok {
dMsg.DiscardPayload() // Discard payload to save space
}
}
ackMap[aMsg.id] = aMsg.msg
case msgID := <-confirmations:
sMsg, ok := ackMap[msgID]
if !ok {
return status.Errorf(codes.InvalidArgument, "no message to confirm with id: %s", msgID)
}
// Substrate backend should always be ready to accept
// an acknowledgement, so this shouldn't block
select {
case <-ctx.Done():
return nil
case acks <- sMsg:
}
delete(ackMap, msgID)
}
}
}
// ackMsg is a mapping from substrate message to proximo message id
type ackMsg struct {
id string
msg substrate.Message
}