-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
285 lines (237 loc) · 7.48 KB
/
server.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
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package brpc
import (
"context"
"encoding/binary"
"fmt"
"log"
"net"
"reflect"
"sync"
"github.com/bytedance/gopkg/util/gopool"
"github.com/cloudwego/netpoll"
"github.com/cloudwego/netpoll/mux"
"google.golang.org/protobuf/proto"
"github.com/xxpbb/brpc-go/brpcpb"
)
type methodHandler func(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error)
// MethodDesc represents an RPC service's method specification.
type MethodDesc struct {
MethodName string
Handler methodHandler
}
// ServiceDesc represents an RPC service's specification.
type ServiceDesc struct {
ServiceName string
// The pointer to the service interface. Used to check whether the user
// provided implementation satisfies the interface requirements.
HandlerType interface{}
Methods []MethodDesc
}
// serviceInfo wraps information about a service. It is very similar to
// ServiceDesc and is constructed from it for internal purposes.
type serviceInfo struct {
// Contains the implementation for the methods in this service.
serviceImpl interface{}
methods map[string]*MethodDesc
}
// Server is a bRPC server to serve RPC requests.
type Server struct {
opts serverOptions
mu sync.Mutex // guards following
serve bool
eventLoop netpoll.EventLoop
cv *sync.Cond // signaled when connections close for GracefulStop
services map[string]*serviceInfo // service name -> service info
}
type serverOptions struct {
}
var defaultServerOptions = serverOptions{}
// A ServerOption sets options such as credentials, codec and keepalive parameters, etc.
type ServerOption interface {
apply(*serverOptions)
}
// funcServerOption wraps a function that modifies serverOptions into an
// implementation of the ServerOption interface.
// type funcServerOption struct {
// f func(*serverOptions)
// }
// func (fdo *funcServerOption) apply(do *serverOptions) {
// fdo.f(do)
// }
// func newFuncServerOption(f func(*serverOptions)) *funcServerOption {
// return &funcServerOption{
// f: f,
// }
// }
// NewServer creates a bRPC server which has no service registered and has not
// started to accept requests yet.
func NewServer(opt ...ServerOption) *Server {
opts := defaultServerOptions
for _, o := range opt {
o.apply(&opts)
}
s := &Server{
opts: opts,
services: make(map[string]*serviceInfo),
}
s.cv = sync.NewCond(&s.mu)
return s
}
// ServiceRegistrar wraps a single method that supports service registration. It
// enables users to pass concrete types other than brpc.Server to the service
// registration methods exported by the IDL generated code.
type ServiceRegistrar interface {
// RegisterService registers a service and its implementation to the
// concrete type implementing this interface. It may not be called
// once the server has started serving.
// desc describes the service and its methods and handlers. impl is the
// service implementation which is passed to the method handlers.
RegisterService(desc *ServiceDesc, impl interface{})
}
// RegisterService registers a service and its implementation to the bRPC
// server. It is called from the IDL generated code. This must be called before
// invoking Serve. If ss is non-nil (for legacy code), its type is checked to
// ensure it implements sd.HandlerType.
func (s *Server) RegisterService(sd *ServiceDesc, ss interface{}) {
if ss != nil {
ht := reflect.TypeOf(sd.HandlerType).Elem()
st := reflect.TypeOf(ss)
if !st.Implements(ht) {
log.Fatalf("brpc: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht)
}
}
s.register(sd, ss)
}
func (s *Server) register(sd *ServiceDesc, ss interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
if s.serve {
log.Fatalf("brpc: Server.RegisterService after Server.Serve for %q", sd.ServiceName)
}
if _, ok := s.services[sd.ServiceName]; ok {
log.Fatalf("brpc: Server.RegisterService found duplicate service registration for %q", sd.ServiceName)
}
info := &serviceInfo{
serviceImpl: ss,
methods: make(map[string]*MethodDesc),
}
for i := range sd.Methods {
d := &sd.Methods[i]
info.methods[d.MethodName] = d
}
s.services[sd.ServiceName] = info
}
func (s *Server) Stop() {
s.GracefulStop()
}
func (s *Server) GracefulStop() {
s.mu.Lock()
defer s.mu.Unlock()
s.eventLoop.Shutdown(context.Background())
s.serve = false
}
// Serve accepts incoming connections on the listener lis, creating a new
// ServerTransport and service goroutine for each. The service goroutines
// read bRPC requests and then call the registered handlers to reply to them.
// Serve returns when lis.Accept fails with fatal errors. lis will be closed when
// this method returns.
// Serve will return a non-nil error unless Stop or GracefulStop is called.
func (s *Server) Serve(lis net.Listener) error {
s.serve = true
eventLoop, err := netpoll.NewEventLoop(s.connRequestCb, netpoll.WithOnPrepare(s.connPrepareCb))
if err != nil {
return err
}
s.eventLoop = eventLoop
return eventLoop.Serve(lis)
}
type wqContextKey struct{}
var gWQContextKey = &wqContextKey{}
func (s *Server) connPrepareCb(conn netpoll.Connection) context.Context {
wq := mux.NewShardQueue(mux.ShardSize, conn)
conn.AddCloseCallback(func(c netpoll.Connection) error {
log.Printf("conn [%s] is closed\n", conn.RemoteAddr())
return wq.Close()
})
return context.WithValue(context.Background(), gWQContextKey, wq)
}
func (s *Server) connRequestCb(ctx context.Context, conn netpoll.Connection) error {
wq, ok := ctx.Value(gWQContextKey).(*mux.ShardQueue)
if !ok {
return fmt.Errorf("wq not found")
}
reader := conn.Reader()
headerB, err := reader.Peek(8)
if err != nil {
return err
}
bodyL := int(binary.BigEndian.Uint32(headerB[4:8]))
r, err := reader.Slice(12 + bodyL)
if err != nil {
return err
}
gopool.Go(func() { s.handleMsg(r, wq) })
return nil
}
func (s *Server) handleMsg(reader netpoll.Reader, wq *mux.ShardQueue) error {
// decode request
m := NewMessage()
err := m.Decode(reader)
if err != nil {
return err
}
serviceName := m.meta.GetRequest().GetServiceName()
methodName := m.meta.GetRequest().GetMethodName()
id := m.meta.GetCorrelationId()
// handle
cntl := &Controller{
requestAttachment: m.attachment,
}
srvInfo := s.services[serviceName]
out, err := srvInfo.methods[methodName].Handler(srvInfo.serviceImpl, newContextWithController(cntl), func(in interface{}) error {
return proto.Unmarshal(m.data, in.(proto.Message))
})
if err != nil {
return err
}
// encode response
data, err := proto.Marshal(out.(proto.Message))
if err != nil {
return err
}
m = &Message{
meta: &brpcpb.RpcMeta{
Response: &brpcpb.RpcResponseMeta{
ErrorCode: proto.Int32(0),
},
CorrelationId: proto.Int64(id),
AttachmentSize: proto.Int32(int32(len(cntl.responseAttachment))),
},
data: data,
attachment: cntl.responseAttachment,
}
w, err := m.Encode()
if err != nil {
return err
}
// send
wq.Add(func() (netpoll.Writer, bool) {
return w, false
})
return nil
}