This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
service.go
290 lines (242 loc) · 6.44 KB
/
service.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package grpc
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"path"
"sync"
"time"
"github.com/spiral/php-grpc/parser"
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service/env"
"github.com/spiral/roadrunner/service/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/keepalive"
)
// ID sets public GRPC service ID for roadrunner.Container.
const ID = "grpc"
var errCouldNotAppendPemError = errors.New("could not append Certs from PEM")
// Service manages set of GPRC services, options and connections.
type Service struct {
cfg *Config
env env.Environment
list []func(event int, ctx interface{})
opts []grpc.ServerOption
services []func(server *grpc.Server)
mu sync.Mutex
rr *roadrunner.Server
cr roadrunner.Controller
grpc *grpc.Server
}
// Attach attaches cr. Currently only one cr is supported.
func (svc *Service) Attach(ctr roadrunner.Controller) {
svc.cr = ctr
}
// AddListener attaches grpc event watcher.
func (svc *Service) AddListener(l func(event int, ctx interface{})) {
svc.list = append(svc.list, l)
}
// AddService would be invoked after GRPC service creation.
func (svc *Service) AddService(r func(server *grpc.Server)) error {
svc.services = append(svc.services, r)
return nil
}
// AddOption adds new GRPC server option. Codec and TLS options are controlled by service internally.
func (svc *Service) AddOption(opt grpc.ServerOption) {
svc.opts = append(svc.opts, opt)
}
// Init service.
func (svc *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (ok bool, err error) {
svc.cfg = cfg
svc.env = e
if r != nil {
if err := r.Register(ID, &rpcServer{svc}); err != nil {
return false, err
}
}
if svc.cfg.Workers.Command != "" {
svc.rr = roadrunner.NewServer(svc.cfg.Workers)
}
// register the Codec
encoding.RegisterCodec(&Codec{
Base: encoding.GetCodec(CodecName),
})
return true, nil
}
// Serve GRPC grpc.
func (svc *Service) Serve() (err error) {
svc.mu.Lock()
if svc.grpc, err = svc.createGPRCServer(); err != nil {
svc.mu.Unlock()
return err
}
ls, err := svc.cfg.Listener()
if err != nil {
svc.mu.Unlock()
return err
}
defer ls.Close()
if svc.rr != nil {
if svc.env != nil {
if err := svc.env.Copy(svc.cfg.Workers); err != nil {
svc.mu.Unlock()
return err
}
}
svc.cfg.Workers.SetEnv("RR_GRPC", "true")
svc.rr.Listen(svc.throw)
if svc.cr != nil {
svc.rr.Attach(svc.cr)
}
if err := svc.rr.Start(); err != nil {
svc.mu.Unlock()
return err
}
defer svc.rr.Stop()
}
svc.mu.Unlock()
return svc.grpc.Serve(ls)
}
// Stop the service.
func (svc *Service) Stop() {
svc.mu.Lock()
defer svc.mu.Unlock()
if svc.grpc == nil {
return
}
go svc.grpc.GracefulStop()
}
// Server returns associated rr server (if any).
func (svc *Service) Server() *roadrunner.Server {
svc.mu.Lock()
defer svc.mu.Unlock()
return svc.rr
}
// call info
func (svc *Service) interceptor(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (resp interface{}, err error) {
start := time.Now()
resp, err = handler(ctx, req)
svc.throw(EventUnaryCall, &UnaryCallEvent{
Info: info,
Context: ctx,
Error: err,
start: start,
elapsed: time.Since(start),
})
return resp, err
}
// throw handles service, grpc and pool events.
func (svc *Service) throw(event int, ctx interface{}) {
for _, l := range svc.list {
l(event, ctx)
}
if event == roadrunner.EventServerFailure {
// underlying rr grpc is dead
svc.Stop()
}
}
// new configured GRPC server
func (svc *Service) createGPRCServer() (*grpc.Server, error) {
opts, err := svc.serverOptions()
if err != nil {
return nil, err
}
server := grpc.NewServer(opts...)
if len(svc.cfg.Proto) > 0 && svc.rr != nil {
for _, proto := range svc.cfg.Proto {
// php proxy services
services, err := parser.File(proto, path.Dir(proto))
if err != nil {
return nil, err
}
for _, service := range services {
p := NewProxy(fmt.Sprintf("%s.%s", service.Package, service.Name), proto, svc.rr)
for _, m := range service.Methods {
p.RegisterMethod(m.Name)
}
server.RegisterService(p.ServiceDesc(), p)
}
}
}
// external and native services
for _, r := range svc.services {
r(server)
}
return server, nil
}
// server options
func (svc *Service) serverOptions() ([]grpc.ServerOption, error) {
var tcreds credentials.TransportCredentials
var opts []grpc.ServerOption
var cert tls.Certificate
var certPool *x509.CertPool
var rca []byte
var err error
if svc.cfg.EnableTLS() {
// if client CA is not empty we combine it with Cert and Key
if svc.cfg.TLS.RootCA != "" {
cert, err = tls.LoadX509KeyPair(svc.cfg.TLS.Cert, svc.cfg.TLS.Key)
if err != nil {
return nil, err
}
certPool, err = x509.SystemCertPool()
if err != nil {
return nil, err
}
if certPool == nil {
certPool = x509.NewCertPool()
}
rca, err = ioutil.ReadFile(svc.cfg.TLS.RootCA)
if err != nil {
return nil, err
}
if ok := certPool.AppendCertsFromPEM(rca); !ok {
return nil, errCouldNotAppendPemError
}
tcreds = credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{cert},
ClientCAs: certPool,
RootCAs: certPool,
})
} else {
var err error
tcreds, err = credentials.NewServerTLSFromFile(svc.cfg.TLS.Cert, svc.cfg.TLS.Key)
if err != nil {
return nil, err
}
}
serverOptions := []grpc.ServerOption{
grpc.MaxSendMsgSize(int(svc.cfg.MaxSendMsgSize)),
grpc.MaxRecvMsgSize(int(svc.cfg.MaxRecvMsgSize)),
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: svc.cfg.MaxConnectionIdle,
MaxConnectionAge: svc.cfg.MaxConnectionAge,
MaxConnectionAgeGrace: svc.cfg.MaxConnectionAge,
Time: svc.cfg.PingTime,
Timeout: svc.cfg.Timeout,
}),
grpc.MaxConcurrentStreams(uint32(svc.cfg.MaxConcurrentStreams)),
}
opts = append(opts, grpc.Creds(tcreds))
opts = append(opts, serverOptions...)
}
opts = append(opts, svc.opts...)
// custom Codec is required to bypass protobuf, common interceptor used for debug and stats
// custom Codec is required to bypass protobuf, common interceptor used for debug and stats
return append(
opts,
grpc.UnaryInterceptor(svc.interceptor),
), nil
}