forked from liangdas/mqant
/
redis_server.go
165 lines (150 loc) · 3.85 KB
/
redis_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
// 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 (
"github.com/garyburd/redigo/redis"
"github.com/golang/protobuf/proto"
"github.com/liangdas/mqant/conf"
"github.com/liangdas/mqant/log"
"github.com/liangdas/mqant/rpc"
"github.com/liangdas/mqant/rpc/pb"
"github.com/liangdas/mqant/utils"
"runtime"
)
type RedisServer struct {
call_chan chan mqrpc.CallInfo
url string
info *conf.Redis
queueName string
done chan error
pool redis.Conn
closed bool
}
func NewRedisServer(info *conf.Redis, call_chan chan mqrpc.CallInfo) (*RedisServer, error) {
var queueName = info.Queue
var url = info.Uri
server := new(RedisServer)
server.call_chan = call_chan
server.url = url
server.info = info
server.queueName = queueName
server.done = make(chan error)
server.closed = false
go server.on_request_handle(server.done)
return server, nil
//log.Printf("shutting down")
//
//if err := c.Shutdown(); err != nil {
// log.Fatalf("error during shutdown: %s", err)
//}
}
/**
停止接收请求
*/
func (s *RedisServer) StopConsume() error {
s.closed = true
if s.pool != nil {
return s.pool.Close()
}
return nil
}
/**
注销消息队列
*/
func (s *RedisServer) Shutdown() error {
s.closed = true
if s.pool != nil {
return s.pool.Close()
}
return nil
}
func (s *RedisServer) Callback(callinfo mqrpc.CallInfo) error {
body, _ := s.MarshalResult(callinfo.Result)
return s.response(callinfo.Props, body)
}
/**
消息应答
*/
func (s *RedisServer) response(props map[string]interface{}, body []byte) error {
pool := utils.GetRedisFactory().GetPool(s.info.Uri).Get()
defer pool.Close()
var err error
_, err = pool.Do("lpush", props["reply_to"].(string), body)
if err != nil {
log.Warning("Publish: %s", err)
return err
}
return nil
}
/**
接收请求信息
*/
func (s *RedisServer) on_request_handle(done chan 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)
}
}()
for !s.closed {
s.pool = utils.GetRedisFactory().GetPool(s.info.Uri).Get()
result, err := s.pool.Do("brpop", s.queueName, 0)
s.pool.Close()
if err == nil && result != nil {
rpcInfo, err := s.Unmarshal(result.([]interface{})[1].([]byte))
if err == nil {
callInfo := &mqrpc.CallInfo{
RpcInfo: *rpcInfo,
}
callInfo.Props = map[string]interface{}{
"reply_to": callInfo.RpcInfo.ReplyTo,
}
callInfo.Agent = s //设置代理为AMQPServer
s.call_chan <- *callInfo
} else {
log.Error("error ", err)
}
} else if err != nil {
log.Warning("error %s", err.Error())
}
}
log.Debug("finish on_request_handle")
}
func (s *RedisServer) 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 *RedisServer) MarshalResult(resultInfo rpcpb.ResultInfo) ([]byte, error) {
//log.Error("",map2)
b, err := proto.Marshal(&resultInfo)
return b, err
}