-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpush_stream.go
62 lines (51 loc) · 1.24 KB
/
push_stream.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
package input
import (
"context"
"io"
"github.com/ab180/lrmr/job"
"github.com/ab180/lrmr/lrdd"
"github.com/ab180/lrmr/lrmrpb"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type PushStream struct {
stream lrmrpb.Node_PushDataServer
reader *Reader
reqCache *lrmrpb.PushDataRequest
}
func NewPushStream(r *Reader, stream lrmrpb.Node_PushDataServer) *PushStream {
return &PushStream{
stream: stream,
reader: r,
reqCache: &lrmrpb.PushDataRequest{},
}
}
func (p *PushStream) Dispatch() error {
p.reader.Add()
defer p.reader.Done()
for {
err := p.stream.RecvMsg(p.reqCache)
if err != nil {
if status.Code(err) == codes.Canceled || errors.Cause(err) == context.Canceled || err == io.EOF {
return nil
}
return errors.Wrap(err, "stream dispatch")
}
rows := make([]lrdd.Row, len(p.reqCache.Data))
for i, row := range p.reqCache.Data {
value := lrdd.GetValue(p.reader.RowType())
_, err := value.UnmarshalMsg(row.Value)
if err != nil {
return err
}
rows[i].Key = row.Key
rows[i].Value = value
}
p.reader.Write(rows)
p.reqCache.RemainCapicityReset()
}
}
func (p *PushStream) CloseWithStatus(st job.Status) error {
return p.stream.SendMsg(st)
}