/
pusher.go
196 lines (172 loc) · 4.55 KB
/
pusher.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
package plugins
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/nats-io/nats.go"
"reflect"
"time"
)
type PusherInterface interface {
GetChannel() string
Push(string, interface{}) error
SetChannel(string) PusherInterface
Subscribe(string, interface{}) (*nats.Subscription, error)
Close()
}
type MsgRpc interface {
Close()
GetTimeOut() time.Duration
SetTimeOut(duration time.Duration) MsgRpc
Worker(handler interface{}) (*nats.Subscription, error)
RpcCall(method string, data interface{}, responsePtr interface{}, timeOut ...time.Duration) error
RpcService(method string, handler func(string, []byte) (interface{}, error)) (*nats.Subscription, error)
}
type MapperDecoder interface {
M(...func(beego.M)) beego.M
}
type Pusher struct {
Client *NatsPlugin
channel string
conn *nats.EncodedConn
Timeout time.Duration
}
func GetPusher() PusherInterface {
var instance = new(Pusher)
instance.Init()
return instance
}
func GetMsgRpc() MsgRpc {
var instance = new(Pusher)
instance.Init()
return instance
}
func (this *Pusher) Init() {
this.Client = GetNatsPlugin()
}
func (this *Pusher) Push(channel string, data interface{}) error {
return this.client().Publish(channel, data)
}
func (this *Pusher) SetChannel(channel string) PusherInterface {
if this.channel == "" {
this.channel = channel
}
return this
}
func (this *Pusher) Send(data []byte) error {
return this.Push(this.GetChannel(), data)
}
func (this *Pusher) GetChannel() string {
if this.channel == "" {
return "default"
}
return this.channel
}
// 订阅
func (this *Pusher) Subscribe(channel string, handler interface{}) (*nats.Subscription, error) {
return this.client().Subscribe(channel, handler)
}
// 队列订阅
func (this *Pusher) QueueSubscribe(channel, queue string, cb interface{}) (*nats.Subscription, error) {
return this.client().QueueSubscribe(channel, queue, cb)
}
// 队列订阅
func (this *Pusher) Queue(channel, queue string, cb interface{}) (*nats.Subscription, error) {
return this.client().BindRecvQueueChan(channel, queue, cb)
}
// worker
func (this *Pusher) Worker(handler interface{}) (*nats.Subscription, error) {
return this.Subscribe(this.GetChannel(), handler)
}
// 调用远程服务
func (this *Pusher) RpcService(method string, handler func(string, []byte) (interface{}, error)) (*nats.Subscription, error) {
return this.Subscribe(method, func(msg *nats.Msg) error {
var data, err = handler(msg.Subject, msg.Data)
if err == nil {
return respond(msg, data)
}
return respond(msg, err.Error())
})
}
// 远程调用
func (this *Pusher) RpcCall(method string, data interface{}, responsePtr interface{}, timeOut ...time.Duration) error {
if len(timeOut) == 0 {
timeOut = append(timeOut, this.GetTimeOut())
}
return this.client().Request(method, data, responsePtr, timeOut[0])
}
func (this *Pusher) Close() {
this.client().Close()
this.Client = nil
}
func (this *Pusher) GetTimeOut() time.Duration {
if this.Timeout != 0 {
return this.Timeout
}
return 2 * time.Second
}
func (this *Pusher) SetTimeOut(duration time.Duration) MsgRpc {
if duration <= 0 {
return this
}
this.Timeout = duration
return this
}
func (this *Pusher) client() *nats.EncodedConn {
if this.conn != nil {
return this.conn
}
this.conn, _ = nats.NewEncodedConn(this.Client.GetConn(), nats.JSON_ENCODER)
return this.conn
}
func respond(msg *nats.Msg, v interface{}) error {
switch v.(type) {
case string:
return msg.Respond([]byte(v.(string)))
case []byte:
return msg.Respond(v.([]byte))
}
var (
getType = reflect.TypeOf(v)
getValue = reflect.ValueOf(v)
)
// func
if getType.Kind() == reflect.Func {
var (
params = []reflect.Value{
reflect.ValueOf(msg), reflect.ValueOf(v),
}
values = getValue.Call(params)
)
if e, ok := values[0].Interface().(error); ok {
return e
}
return nil
}
// struct
if getType.Kind() == reflect.Struct {
var data, _ = json.Marshal(v)
return msg.Respond(data)
}
// mapper
if getType.Kind() == reflect.Map {
var data, _ = json.Marshal(v)
return msg.Respond(data)
}
if decoder, ok := v.(MapperDecoder); ok {
var data, _ = json.Marshal(decoder.M())
return msg.Respond(data)
}
return msg.Respond(NewErrorResponse())
}
func NewErrorResponse() []byte {
var data, _ = json.Marshal(beego.M{
"msg": "service error",
"code": 5000,
"error": beego.M{
"errno": 5000,
"errmsg": "服务异常",
},
"data": nil,
})
return data
}