forked from JIUYEKEJI/gorpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
executable file
·170 lines (135 loc) · 3.98 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
package gorpc
import (
"context"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/lubanproj/gorpc/codec"
"github.com/lubanproj/gorpc/codes"
"github.com/lubanproj/gorpc/interceptor"
"github.com/lubanproj/gorpc/log"
"github.com/lubanproj/gorpc/protocol"
"github.com/lubanproj/gorpc/stream"
"github.com/lubanproj/gorpc/transport"
"github.com/lubanproj/gorpc/utils"
)
// Service 定义了某个具体服务的通用实现接口
type Service interface {
Register(string, Handler)
Serve(*ServerOptions)
Close()
}
type service struct{
svr interface{} // server
ctx context.Context // 每一个 service 一个上下文进行管理
cancel context.CancelFunc // context 的控制器
serviceName string // 服务名
handlers map[string]Handler
opts *ServerOptions // 参数选项
}
// ServiceDesc is a detailed description of a service
type ServiceDesc struct {
Svr interface{}
ServiceName string
Methods []*MethodDesc
HandlerType interface{}
}
// MethodDesc is a detailed description of a method
type MethodDesc struct {
MethodName string
Handler Handler
}
// Handler is the handler of a method
type Handler func (interface{}, context.Context, func(interface{}) error, []interceptor.ServerInterceptor) (interface{}, error)
func (s *service) Register(handlerName string, handler Handler) {
if s.handlers == nil {
s.handlers = make(map[string]Handler)
}
s.handlers[handlerName] = handler
}
func (s *service) Serve(opts *ServerOptions) {
// TODO 思考下除了 Server 和 Service 的 Options 如何处理
s.opts = opts
transportOpts := []transport.ServerTransportOption {
transport.WithServerAddress(s.opts.address),
transport.WithServerNetwork(s.opts.network),
transport.WithHandler(s),
transport.WithServerTimeout(s.opts.timeout),
transport.WithSerialization(s.opts.serializationType),
}
serverTransport := transport.GetServerTransport(s.opts.protocol)
s.ctx, s.cancel = context.WithCancel(context.Background())
if err := serverTransport.ListenAndServe(s.ctx, transportOpts ...); err != nil {
log.Errorf("%s serve error, %v", s.opts.network, err)
return
}
fmt.Printf("%s service serving at %s ... \n",s.opts.protocol, s.opts.address)
<- s.ctx.Done()
}
func (s *service) Close() {
s.cancel()
fmt.Println("service Closing ...")
}
func (s *service) Handle (ctx context.Context, frame []byte) ([]byte, error) {
if len(frame) == 0 {
return nil, errors.New("req is nil")
}
// 将 reqbuf 解析成 req interface {}
serverCodec := codec.GetCodec(s.opts.protocol)
reqbuf, err := serverCodec.Decode(frame)
if err != nil {
return nil, err
}
// parse protocol header
request := &protocol.Request{}
if err = proto.Unmarshal(reqbuf, request); err != nil {
return nil, err
}
serverSerialization := codec.GetSerialization(s.opts.serializationType)
dec := func(req interface {}) error {
if err := serverSerialization.Unmarshal(request.Payload, req); err != nil {
return err
}
return nil
}
if s.opts.timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, s.opts.timeout)
defer cancel()
}
_, method , err := utils.ParseServicePath(string(request.ServicePath))
if err != nil {
return nil, codes.New(codes.ClientMsgErrorCode, "method is invalid")
}
handler := s.handlers[method]
if handler == nil {
return nil, errors.New("handlers is nil")
}
rsp, err := handler(s.svr, ctx, dec, s.opts.interceptors)
if err != nil {
return nil, err
}
rspbuf, err := serverSerialization.Marshal(rsp)
if err != nil {
return nil, err
}
response := addRspHeader(ctx, rspbuf)
rspPb, err := proto.Marshal(response)
if err != nil {
return nil, err
}
rspbody, err := serverCodec.Encode(rspPb)
if err != nil {
return nil, err
}
return rspbody, nil
}
func addRspHeader(ctx context.Context, payload []byte) *protocol.Response {
serverStream := stream.GetServerStream(ctx)
response := &protocol.Response{
Payload: payload,
RetCode: serverStream.RetCode,
RetMsg: serverStream.RetMsg,
}
return response
}