forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chanrpc.go
392 lines (336 loc) · 7.21 KB
/
chanrpc.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package chanrpc
import (
"errors"
"fmt"
"github.com/mikukawai/leaf/conf"
"github.com/mikukawai/leaf/log"
"runtime"
)
// one server per goroutine (goroutine not safe)
// one client per goroutine (goroutine not safe)
type Server struct {
// id -> function
//
// function:
// func(args []interface{})
// func(args []interface{}) interface{}
// func(args []interface{}) []interface{}
functions map[interface{}]interface{}
ChanCall chan *CallInfo
}
type CallInfo struct {
f interface{}
args []interface{}
chanRet chan *RetInfo
cb interface{}
}
type RetInfo struct {
// nil
// interface{}
// []interface{}
ret interface{}
err error
// callback:
// func(err error)
// func(ret interface{}, err error)
// func(ret []interface{}, err error)
cb interface{}
}
type Client struct {
s *Server
chanSyncRet chan *RetInfo
ChanAsynRet chan *RetInfo
pendingAsynCall int
}
func NewServer(l int) *Server {
s := new(Server)
s.functions = make(map[interface{}]interface{})
s.ChanCall = make(chan *CallInfo, l)
return s
}
func assert(i interface{}) []interface{} {
if i == nil {
return nil
} else {
return i.([]interface{})
}
}
// you must call the function before calling Open and Go
func (s *Server) Register(id interface{}, f interface{}) {
switch f.(type) {
case func([]interface{}):
case func([]interface{}) interface{}:
case func([]interface{}) []interface{}:
default:
panic(fmt.Sprintf("function id %v: definition of function is invalid", id))
}
if _, ok := s.functions[id]; ok {
panic(fmt.Sprintf("function id %v: already registered", id))
}
s.functions[id] = f
}
func (s *Server) ret(ci *CallInfo, ri *RetInfo) (err error) {
if ci.chanRet == nil {
return
}
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
ri.cb = ci.cb
ci.chanRet <- ri
return
}
func (s *Server) exec(ci *CallInfo) (err error) {
defer func() {
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
err = fmt.Errorf("%v: %s", r, buf[:l])
} else {
err = fmt.Errorf("%v", r)
}
s.ret(ci, &RetInfo{err: fmt.Errorf("%v", r)})
}
}()
// execute
switch ci.f.(type) {
case func([]interface{}):
ci.f.(func([]interface{}))(ci.args)
return s.ret(ci, &RetInfo{})
case func([]interface{}) interface{}:
ret := ci.f.(func([]interface{}) interface{})(ci.args)
return s.ret(ci, &RetInfo{ret: ret})
case func([]interface{}) []interface{}:
ret := ci.f.(func([]interface{}) []interface{})(ci.args)
return s.ret(ci, &RetInfo{ret: ret})
}
panic("bug")
}
func (s *Server) Exec(ci *CallInfo) {
err := s.exec(ci)
if err != nil {
log.Error("%v", err)
}
}
// goroutine safe
func (s *Server) Go(id interface{}, args ...interface{}) {
f := s.functions[id]
if f == nil {
return
}
defer func() {
recover()
}()
s.ChanCall <- &CallInfo{
f: f,
args: args,
}
}
// goroutine safe
func (s *Server) Call0(id interface{}, args ...interface{}) error {
return s.Open(0).Call0(id, args...)
}
// goroutine safe
func (s *Server) Call1(id interface{}, args ...interface{}) (interface{}, error) {
return s.Open(0).Call1(id, args...)
}
// goroutine safe
func (s *Server) CallN(id interface{}, args ...interface{}) ([]interface{}, error) {
return s.Open(0).CallN(id, args...)
}
func (s *Server) Close() {
close(s.ChanCall)
for ci := range s.ChanCall {
s.ret(ci, &RetInfo{
err: errors.New("chanrpc server closed"),
})
}
}
// goroutine safe
func (s *Server) Open(l int) *Client {
c := NewClient(l)
c.Attach(s)
return c
}
func NewClient(l int) *Client {
c := new(Client)
c.chanSyncRet = make(chan *RetInfo, 1)
c.ChanAsynRet = make(chan *RetInfo, l)
return c
}
func (c *Client) Attach(s *Server) {
c.s = s
}
func (c *Client) call(ci *CallInfo, block bool) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
if block {
c.s.ChanCall <- ci
} else {
select {
case c.s.ChanCall <- ci:
default:
err = errors.New("chanrpc channel full")
}
}
return
}
func (c *Client) f(id interface{}, n int) (f interface{}, err error) {
if c.s == nil {
err = errors.New("server not attached")
return
}
f = c.s.functions[id]
if f == nil {
err = fmt.Errorf("function id %v: function not registered", id)
return
}
var ok bool
switch n {
case 0:
_, ok = f.(func([]interface{}))
case 1:
_, ok = f.(func([]interface{}) interface{})
case 2:
_, ok = f.(func([]interface{}) []interface{})
default:
panic("bug")
}
if !ok {
err = fmt.Errorf("function id %v: return type mismatch", id)
}
return
}
func (c *Client) Call0(id interface{}, args ...interface{}) error {
f, err := c.f(id, 0)
if err != nil {
return err
}
err = c.call(&CallInfo{
f: f,
args: args,
chanRet: c.chanSyncRet,
}, true)
if err != nil {
return err
}
ri := <-c.chanSyncRet
return ri.err
}
func (c *Client) Call1(id interface{}, args ...interface{}) (interface{}, error) {
f, err := c.f(id, 1)
if err != nil {
return nil, err
}
err = c.call(&CallInfo{
f: f,
args: args,
chanRet: c.chanSyncRet,
}, true)
if err != nil {
return nil, err
}
ri := <-c.chanSyncRet
return ri.ret, ri.err
}
func (c *Client) CallN(id interface{}, args ...interface{}) ([]interface{}, error) {
f, err := c.f(id, 2)
if err != nil {
return nil, err
}
err = c.call(&CallInfo{
f: f,
args: args,
chanRet: c.chanSyncRet,
}, true)
if err != nil {
return nil, err
}
ri := <-c.chanSyncRet
return assert(ri.ret), ri.err
}
func (c *Client) asynCall(id interface{}, args []interface{}, cb interface{}, n int) {
f, err := c.f(id, n)
if err != nil {
c.ChanAsynRet <- &RetInfo{err: err, cb: cb}
return
}
err = c.call(&CallInfo{
f: f,
args: args,
chanRet: c.ChanAsynRet,
cb: cb,
}, false)
if err != nil {
c.ChanAsynRet <- &RetInfo{err: err, cb: cb}
return
}
}
func (c *Client) AsynCall(id interface{}, _args ...interface{}) {
if len(_args) < 1 {
panic("callback function not found")
}
args := _args[:len(_args)-1]
cb := _args[len(_args)-1]
var n int
switch cb.(type) {
case func(error):
n = 0
case func(interface{}, error):
n = 1
case func([]interface{}, error):
n = 2
default:
panic("definition of callback function is invalid")
}
// too many calls
if c.pendingAsynCall >= cap(c.ChanAsynRet) {
execCb(&RetInfo{err: errors.New("too many calls"), cb: cb})
return
}
c.asynCall(id, args, cb, n)
c.pendingAsynCall++
}
func execCb(ri *RetInfo) {
defer func() {
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
// execute
switch ri.cb.(type) {
case func(error):
ri.cb.(func(error))(ri.err)
case func(interface{}, error):
ri.cb.(func(interface{}, error))(ri.ret, ri.err)
case func([]interface{}, error):
ri.cb.(func([]interface{}, error))(assert(ri.ret), ri.err)
default:
panic("bug")
}
return
}
func (c *Client) Cb(ri *RetInfo) {
c.pendingAsynCall--
execCb(ri)
}
func (c *Client) Close() {
for c.pendingAsynCall > 0 {
c.Cb(<-c.ChanAsynRet)
}
}
func (c *Client) Idle() bool {
return c.pendingAsynCall == 0
}