-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathservice.go
executable file
·149 lines (117 loc) · 3.56 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
package gorpc
import (
"context"
"errors"
"fmt"
"github.com/lubanproj/gorpc/codec"
"github.com/lubanproj/gorpc/codes"
"github.com/lubanproj/gorpc/interceptor"
"github.com/lubanproj/gorpc/log"
"github.com/lubanproj/gorpc/metadata"
"github.com/lubanproj/gorpc/protocol"
"github.com/lubanproj/gorpc/transport"
"github.com/lubanproj/gorpc/utils"
"github.com/golang/protobuf/proto"
)
// Service defines a generic implementation interface for a specific Service
type Service interface {
Register(string, Handler)
Serve(*ServerOptions)
Close()
Name() string
}
type service struct {
svr interface{} // server
ctx context.Context // Each service is managed in one context
cancel context.CancelFunc // controller of context
serviceName string // service name
handlers map[string]Handler
opts *ServerOptions // parameter options
closing bool // whether the service is closing
}
// 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(context.Context, interface{}, 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) {
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.WithSerializationType(s.opts.serializationType),
transport.WithProtocol(s.opts.protocol),
}
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.closing = true
if s.cancel != nil {
s.cancel()
}
fmt.Println("service closing ...")
}
func (s *service) Name() string {
return s.serviceName
}
func (s *service) Handle(ctx context.Context, reqbuf []byte) ([]byte, error) {
// parse protocol header
request := &protocol.Request{}
if err := proto.Unmarshal(reqbuf, request); err != nil {
return nil, err
}
ctx = metadata.WithServerMetadata(ctx, request.Metadata)
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(ctx, s.svr, dec, s.opts.interceptors)
if err != nil {
return nil, err
}
rspbuf, err := serverSerialization.Marshal(rsp)
if err != nil {
return nil, err
}
return rspbuf, nil
}