forked from skynetservices/skynet-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservicerpc.go
182 lines (145 loc) · 3.68 KB
/
servicerpc.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
package service
import (
"code.google.com/p/gonicetrace/nicetrace"
"errors"
"fmt"
"github.com/bketelsen/skynet"
"launchpad.net/mgo/v2/bson"
"os"
"reflect"
"time"
)
var (
RequestInfoPtrType = reflect.TypeOf(&skynet.RequestInfo{})
anError error
ErrorType = reflect.TypeOf(&anError).Elem()
)
type ServiceRPC struct {
log skynet.Logger
delegate ServiceDelegate
methods map[string]reflect.Value
MethodNames []string
}
var reservedMethodNames = map[string]bool{}
func init() {
var sd ServiceDelegate
sdvalue := reflect.ValueOf(&sd).Elem().Type()
for i := 0; i < sdvalue.NumMethod(); i++ {
m := sdvalue.Method(i)
reservedMethodNames[m.Name] = true
}
}
func NewServiceRPC(sd ServiceDelegate, log skynet.Logger) (srpc *ServiceRPC) {
srpc = &ServiceRPC{
log: log,
delegate: sd,
methods: make(map[string]reflect.Value),
}
// scan through methods looking for a method (RequestInfo, something, something) error
typ := reflect.TypeOf(srpc.delegate)
for i := 0; i < typ.NumMethod(); i++ {
m := typ.Method(i)
if reservedMethodNames[m.Name] {
continue
}
// this is the check to see if something is exported
if m.PkgPath != "" {
continue
}
f := m.Func
ftyp := f.Type()
// must have four parameters: (receiver, RequestInfo, somethingIn, somethingOut)
if ftyp.NumIn() != 4 {
goto problem
}
// don't have to check for the receiver
// check the second parameter
if ftyp.In(1) != RequestInfoPtrType {
goto problem
}
// the somethingIn can be anything
// somethingOut must be a pointer or a map
switch ftyp.In(3).Kind() {
case reflect.Ptr, reflect.Map:
default:
goto problem
}
// must have one return value that is an error
if ftyp.NumOut() != 1 {
goto problem
}
if ftyp.Out(0) != ErrorType {
goto problem
}
// we've got a method!
srpc.methods[m.Name] = f
srpc.MethodNames = append(srpc.MethodNames, m.Name)
continue
problem:
fmt.Println("trying to panic")
fmt.Printf("Bad RPC method for %T: %q %v\n", sd, m.Name, f)
nicetrace.WriteStacktrace(os.Stdout)
panic(fmt.Sprintf("Bad RPC method for %T: %q %v\n", sd, m.Name, f))
}
return
}
// ServiceRPC.Forward is the entry point for RPC calls. It wraps actual RPC calls
// and provides a slot for the RequestInfo. The parameters to the actual RPC
// calls are transmitted in a []byte, and are then marshalled/unmarshalled on
// either end.
func (srpc *ServiceRPC) Forward(in ServiceRPCIn, out *ServiceRPCOut) (err error) {
m, ok := srpc.methods[in.Method]
if !ok {
err = errors.New(fmt.Sprintf("No such method %q", in.Method))
return
}
inValuePtr := reflect.New(m.Type().In(2))
err = bson.Unmarshal(in.In, inValuePtr.Interface())
if err != nil {
return
}
// Allocate the out parameter of the RPC call.
outType := m.Type().In(3)
var outValue reflect.Value
switch outType.Kind() {
case reflect.Ptr:
outValue = reflect.New(m.Type().In(3).Elem())
case reflect.Map:
outValue = reflect.MakeMap(outType)
default:
panic("illegal out param type")
}
startTime := time.Now().UnixNano()
params := []reflect.Value{
reflect.ValueOf(srpc.delegate),
reflect.ValueOf(in.RequestInfo),
inValuePtr.Elem(),
outValue,
}
returns := m.Call(params)
duration := time.Now().UnixNano() - startTime
mc := MethodCall{
MethodName: in.Method,
RequestInfo: in.RequestInfo,
Duration: duration,
}
if srpc.log != nil {
srpc.log.Item(mc)
}
out.Out, err = bson.Marshal(outValue.Interface())
if err != nil {
return
}
erri := returns[0].Interface()
out.Err, _ = erri.(error)
return
}
type ServiceRPCIn struct {
Method string
RequestInfo *skynet.RequestInfo
In []byte
}
type ServiceRPCOut struct {
Out []byte
Err error
}