-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxrpc_client.go
202 lines (172 loc) · 3.89 KB
/
xrpc_client.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
package xrpc
import (
"errors"
"sync"
"time"
gobcodec "github.com/yc90s/xrpc/codec/gob"
xrpcpb "github.com/yc90s/xrpc/pb"
"github.com/golang/glog"
"github.com/google/uuid"
"google.golang.org/protobuf/proto"
)
// 用于外部封装的接口
type IRPCClient interface {
Call(subj string, methodName string, reply any, args ...any) error
Cast(subj string, methodName string, args ...any) error
}
// RPCClient is a rpc client, it must implement the MQCallback interface
type RPCClient struct {
opts Options
calls sync.Map
isValid bool
mu sync.Mutex
}
func NewRPCClient(opts ...Option) *RPCClient {
rpc_client := new(RPCClient)
rpc_client.calls = sync.Map{}
// default timeout 3s
rpc_client.opts.timeout = time.Second * 3
for _, o := range opts {
o(&rpc_client.opts)
}
if rpc_client.opts.codec == nil {
rpc_client.opts.codec = gobcodec.NewCodec()
}
rpc_client.isValid = true
err := rpc_client.opts.mq.Subscribe(rpc_client.opts.subj, rpc_client)
if err != nil {
rpc_client.isValid = false
}
return rpc_client
}
// Call is a method to call a rpc method with reply
// goroutine safe
func (c *RPCClient) Call(subj string, methodName string, reply any, args ...any) error {
if !c.isValid {
err := c.retry()
if err != nil {
return err
}
}
return c._call(subj, methodName, reply, args...)
}
// Cast is a method to call a rpc method without reply
// goroutine safe
func (c *RPCClient) Cast(subj string, methodName string, args ...any) error {
if !c.isValid {
err := c.retry()
if err != nil {
return err
}
}
return c._cast(subj, methodName, args...)
}
func (c *RPCClient) _cast(subj string, methodName string, args ...any) error {
var argsData [][]byte
for _, arg := range args {
data, err := c.opts.codec.Marshal(arg)
if err != nil {
return err
}
argsData = append(argsData, data)
}
request := &xrpcpb.Request{
Method: methodName,
Params: argsData,
}
requestData, err := proto.Marshal(request)
if err != nil {
return err
}
return c.opts.mq.Publish(subj, requestData)
}
func (c *RPCClient) _call(subj string, methodName string, reply any, args ...any) error {
var argsData [][]byte
for _, arg := range args {
data, err := c.opts.codec.Marshal(arg)
if err != nil {
return err
}
argsData = append(argsData, data)
}
randCid, err := uuid.NewRandom()
if err != nil {
return err
}
cid := randCid.String()
request := &xrpcpb.Request{
Cid: cid,
ReplyTo: c.opts.subj,
Method: methodName,
Params: argsData,
}
requestData, err := proto.Marshal(request)
if err != nil {
return err
}
doneChan := make(chan *xrpcpb.Response, 1)
c.calls.Store(cid, doneChan)
defer func() {
c.calls.Delete(cid)
}()
err = c.opts.mq.Publish(subj, requestData)
if err != nil {
return err
}
timeout := time.After(c.opts.timeout)
select {
case <-timeout:
return errors.New("timeout")
case response := <-doneChan:
if len(response.Error) > 0 {
return errors.New(response.Error)
}
return c.opts.codec.Unmarshal(response.Result, reply)
}
}
// must goroutine safe
func (c *RPCClient) Callback(data []byte, mqerr error) {
if mqerr != nil {
glog.Error(mqerr)
// some error happend, should close the client
c.Close()
return
}
var response xrpcpb.Response
err := proto.Unmarshal(data, &response)
if err != nil {
glog.Error(err)
return
}
if doneChan, ok := c.calls.Load(response.Cid); !ok {
glog.Error("cid not found: ", response.Cid)
} else {
doneChan.(chan *xrpcpb.Response) <- &response
}
}
func (c *RPCClient) Close() {
c.mu.Lock()
defer c.mu.Unlock()
if c.isValid {
c.opts.mq.UnSubscribe()
c.isValid = false
}
}
func (c *RPCClient) retry() error {
if c.isValid {
return nil
}
c.mu.Lock()
defer c.mu.Unlock()
// need double check
if c.isValid {
return nil
}
err := c.opts.mq.Subscribe(c.opts.subj, c)
if err != nil {
return err
}
glog.Info("retry success", c.opts.subj)
c.isValid = true
return nil
}