/
nats_server.go
185 lines (168 loc) · 3.94 KB
/
nats_server.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
// Copyright 2014 mqant Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package defaultrpc
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/liangdas/mqant/log"
"github.com/liangdas/mqant/module"
"github.com/liangdas/mqant/rpc"
"github.com/liangdas/mqant/rpc/pb"
"github.com/nats-io/nats.go"
"runtime"
"strings"
"time"
)
type NatsServer struct {
call_chan chan mqrpc.CallInfo
addr string
app module.App
server *RPCServer
done chan bool
stopeds chan bool
isClose bool
}
func setAddrs(addrs []string) []string {
var cAddrs []string
for _, addr := range addrs {
if len(addr) == 0 {
continue
}
if !strings.HasPrefix(addr, "nats://") {
addr = "nats://" + addr
}
cAddrs = append(cAddrs, addr)
}
if len(cAddrs) == 0 {
cAddrs = []string{nats.DefaultURL}
}
return cAddrs
}
func NewNatsServer(app module.App, s *RPCServer) (*NatsServer, error) {
server := new(NatsServer)
server.server = s
server.done = make(chan bool)
server.stopeds = make(chan bool)
server.isClose = false
server.app = app
server.addr = nats.NewInbox()
go func() {
server.on_request_handle()
safeClose(server.stopeds)
}()
return server, nil
}
func (s *NatsServer) Addr() string {
return s.addr
}
func safeClose(ch chan bool) {
defer func() {
if recover() != nil {
// close(ch) panic occur
}
}()
close(ch) // panic if ch is closed
}
/**
注销消息队列
*/
func (s *NatsServer) Shutdown() (err error) {
safeClose(s.done)
s.isClose = true
select {
case <-s.stopeds:
//等待nats注销完成
}
return
}
func (s *NatsServer) Callback(callinfo mqrpc.CallInfo) error {
body, _ := s.MarshalResult(callinfo.Result)
reply_to := callinfo.Props["reply_to"].(string)
return s.app.Transport().Publish(reply_to, body)
}
/**
接收请求信息
*/
func (s *NatsServer) on_request_handle() error {
defer func() {
if r := recover(); r != nil {
var rn = ""
switch r.(type) {
case string:
rn = r.(string)
case error:
rn = r.(error).Error()
}
buf := make([]byte, 1024)
l := runtime.Stack(buf, false)
errstr := string(buf[:l])
log.Error("%s\n ----Stack----\n%s", rn, errstr)
fmt.Println(errstr)
}
}()
subs, err := s.app.Transport().SubscribeSync(s.addr)
if err != nil {
return err
}
go func() {
select {
case <-s.done:
//服务关闭
}
subs.Unsubscribe()
}()
for !s.isClose {
m, err := subs.NextMsg(time.Minute)
if err != nil && err == nats.ErrTimeout {
//fmt.Println(err.Error())
//log.Warning("NatsServer error with '%v'",err)
continue
} else if err != nil {
log.Warning("NatsServer error with '%v'", err)
continue
}
rpcInfo, err := s.Unmarshal(m.Data)
if err == nil {
callInfo := &mqrpc.CallInfo{
RPCInfo: *rpcInfo,
}
callInfo.Props = map[string]interface{}{
"reply_to": rpcInfo.ReplyTo,
}
callInfo.Agent = s //设置代理为NatsServer
s.server.Call(*callInfo)
} else {
fmt.Println("error ", err)
}
}
return nil
}
func (s *NatsServer) Unmarshal(data []byte) (*rpcpb.RPCInfo, error) {
//fmt.Println(msg)
//保存解码后的数据,Value可以为任意数据类型
var rpcInfo rpcpb.RPCInfo
err := proto.Unmarshal(data, &rpcInfo)
if err != nil {
return nil, err
} else {
return &rpcInfo, err
}
panic("bug")
}
// goroutine safe
func (s *NatsServer) MarshalResult(resultInfo rpcpb.ResultInfo) ([]byte, error) {
//log.Error("",map2)
b, err := proto.Marshal(&resultInfo)
return b, err
}