-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
102 lines (91 loc) · 2.38 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
package gorpc
import (
"go/ast"
"reflect"
"sync/atomic"
"github.com/sirupsen/logrus"
)
type MethodType struct {
method reflect.Method
ArgType reflect.Type
ReplyType reflect.Type
numCalls uint64
}
func (mt *MethodType) NumCalls() uint64 {
return atomic.LoadUint64(&mt.numCalls)
}
func (mt *MethodType) newArgv() reflect.Value {
var v reflect.Value
if mt.ArgType.Kind() == reflect.Ptr {
v = reflect.New(mt.ArgType.Elem())
} else {
v = reflect.New(mt.ArgType).Elem()
}
return v
}
func (mt *MethodType) newReplyv() reflect.Value {
v := reflect.New(mt.ReplyType.Elem())
if mt.ReplyType.Elem().Kind() == reflect.Map {
v.Elem().Set(reflect.MakeMap(mt.ReplyType.Elem()))
} else if mt.ReplyType.Elem().Kind() == reflect.Slice {
v.Elem().Set(reflect.MakeSlice(mt.ReplyType.Elem(), 0, 0))
}
return v
}
type service struct {
name string
serviceType reflect.Type
serviceStruct reflect.Value
method map[string]*MethodType
}
func NewService(serviceStruct interface{}) *service {
s := new(service)
s.serviceStruct = reflect.ValueOf(serviceStruct)
s.serviceType = reflect.TypeOf(serviceStruct)
s.name = reflect.Indirect(s.serviceStruct).Type().Name()
s.method = make(map[string]*MethodType)
if !ast.IsExported(s.name) {
logrus.Fatal("invalid service: service can't be exported")
}
s.registerMethods()
return s
}
func (s *service) registerMethods() {
for i := 0; i < s.serviceType.NumMethod(); i++ {
method := s.serviceType.Method(i)
methodType := method.Type
if methodType.NumIn() != 3 || methodType.NumOut() != 1 {
continue
}
name := method.Name
if !ast.IsExported(name) {
continue
}
if methodType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
continue
}
argType := methodType.In(1)
replyType := methodType.In(2)
if !isBuildInOrExported(argType) || !isBuildInOrExported(replyType) {
continue
}
s.method[name] = &MethodType{
method: method,
ArgType: argType,
ReplyType: replyType,
}
logrus.Info("register method: ", name)
}
}
func isBuildInOrExported(tp reflect.Type) bool {
return ast.IsExported(tp.Name()) || tp.PkgPath() == ""
}
func (s *service) Call(mt *MethodType, arg, reply reflect.Value) error {
atomic.AddUint64(&mt.numCalls, 1)
fun := mt.method.Func
value := fun.Call([]reflect.Value{s.serviceStruct, arg, reply})
if value[0].Interface() != nil {
return value[0].Interface().(error)
}
return nil
}