-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (141 loc) · 3.28 KB
/
main.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
package main
import (
"context"
"github.com/bufbuild/protovalidate-go"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"io"
"log/slog"
"net"
"os"
"os/signal"
"sync"
"syscall"
"github.com/dmitrorezn/proto-validation-example/gen/apis/processor"
)
type Cfg struct {
Port string
}
func NewCfg() *Cfg {
return &Cfg{
Port: "8080",
}
}
func (c Cfg) WithPort(p string) Cfg {
c.Port = p
return c
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
cfg := NewCfg()
service, err := newProcessorSvc(ctx)
if err != nil {
panic(err)
}
lis, err := net.Listen("tcp", net.JoinHostPort("", cfg.Port))
if err != nil {
panic(err)
}
gr, ctx := errgroup.WithContext(ctx)
defer func() {
if err = gr.Wait(); err != nil {
panic(err)
}
}()
server := grpc.NewServer()
defer server.GracefulStop()
processor.RegisterProcessorServiceServer(server, service)
gr.Go(func() error {
return server.Serve(lis)
})
slog.Info("STARTED")
defer slog.Info("STOPPED")
<-ctx.Done()
}
var _ processor.ProcessorServiceServer = (*processorSvc)(nil)
type processorSvc struct {
processor.UnimplementedProcessorServiceServer
ctx context.Context
mu sync.RWMutex
cache *safehmap[uuid.UUID, string]
validation *protovalidate.Validator
replicator *replicator[uuid.UUID]
}
func (p *processorSvc) Consume(_ *processor.ConsumeRequest, server processor.ProcessorService_ConsumeServer) error {
for {
select {
case id := <-p.replicator.consume():
if err := server.Send(&processor.ConsumeResponse{
Id: id.String(),
}); err != nil {
return err
}
case <-p.ctx.Done():
return p.ctx.Err()
}
}
}
func newProcessorSvc(ctx context.Context) (*processorSvc, error) {
validator, err := protovalidate.New(
protovalidate.WithMessages( // to warm up validator
new(processor.ProcessRequest),
),
)
if err != nil {
return nil, err
}
return &processorSvc{
ctx: ctx,
replicator: newReplicator[uuid.UUID](ctx),
cache: newSafe[uuid.UUID, string](),
validation: validator,
}, nil
}
func (p *processorSvc) ProcessStream(server processor.ProcessorService_ProcessStreamServer) error {
ctx := server.Context()
var response *processor.ProcessResponse
for {
request, err := server.Recv()
if err != nil {
if err == io.EOF {
return nil
}
slog.Error("ProcessStream -> Recv", err)
continue
}
if response, err = p.Process(ctx, request); err != nil {
return err
}
if err = server.Send(response); err != nil {
return err
}
}
}
func (p *processorSvc) putName(ctx context.Context, name string) (uuid.UUID, error) {
id := uuid.New()
p.cache.Put(id, name)
select {
case <-ctx.Done():
return id, ctx.Err()
case <-p.replicator.produce(id):
}
return id, nil
}
func (p *processorSvc) Process(ctx context.Context, request *processor.ProcessRequest) (response *processor.ProcessResponse, err error) {
if err = p.validation.Validate(request); err != nil {
return nil, err
}
id, err := p.putName(ctx, request.Name)
if err != nil {
return response, err
}
response = &processor.ProcessResponse{
Id: id.String(),
}
if err = p.validation.Validate(response); err != nil {
return response, err
}
return response, err
}