/
rpcv2.go
80 lines (70 loc) · 1.46 KB
/
rpcv2.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
package akRpc
// server no block rpc, enter queue task call back for order.
// add by stefan 20190704 19:24
import (
. "github.com/Peakchen/xgameCommon/RedisConn"
"context"
"fmt"
"log"
"sync"
)
type TAorpcV2 struct {
c *TAokoRedis
callfuns map[string]reflect.Value
acts []*TModelActV2
}
var GAorpcV2obj *TAorpcV2
func CreateAorpcV2(ctx context.Context, wg *sync.WaitGroup, c *TAokoRedis) {
GAorpcV2obj = &TAorpcV2{
c: c,
}
wg.Add(1)
go loop(ctx, wg)
}
func Exit(wg *sync.WaitGroup) {
wg.Wait()
}
func loop(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case <-ctx.Done():
Exit(wg)
return
default:
doRpcTasks()
}
}
}
func doRpcTasks() {
for _, act := range GAorpcV2obj.acts {
act.modf.Call(act.params)
}
}
func CallV2(cn string, in []interface{}) (err error) {
f, ok := GAorpcV2obj.callfuns[cn]
if !ok {
return fmt.Errorf("can not find call func: ", cn)
}
if GAorpcV2obj.acts == nil {
GAorpcV2obj.acts = []*TModelActV2{}
}
if len(GAorpcV2obj.acts) >= ActChanMaxSize {
return fmt.Errorf("rpc acts queue has arive max.")
}
var params []reflect.Value = []reflect.Value{}
for _, param := range in {
params = append(params, reflect.Value(param))
}
GAorpcV2obj.acts = append(GAorpcV2obj.acts, &TModelActV2{
modf: f,
params: params,
})
return nil
}
func AorpcV2Register(cn string, cf interface{}) {
GAorpcV2obj.callfuns[cn] = reflect.ValueOf(cf)
}
func init() {
GAorpcV2obj = nil
}