-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsynapse.go
196 lines (184 loc) · 4.9 KB
/
synapse.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 synapse
import (
"github.com/streadway/amqp"
"time"
"math/rand"
"runtime"
"reflect"
"github.com/bitly/go-simplejson"
"fmt"
)
type Server struct {
Debug bool
DisableRpcClient bool
DisableEventClient bool
AppName string
AppId string
SysName string
MqHost string
MqPort string
MqUser string
MqPass string
MqVHost string
EventProcessNum int
RpcProcessNum int
EventCallback map[string]func(*simplejson.Json, amqp.Delivery) bool
RpcCallback map[string]func(*simplejson.Json, amqp.Delivery) map[string]interface{}
RpcTimeout time.Duration
conn *amqp.Connection
eventClientChannel *amqp.Channel
rpcClientChannel *amqp.Channel
cli <-chan amqp.Delivery
cliResMap map[string]chan *simplejson.Json
}
const LogInfo = "Info"
const LogDebug = "Debug"
const LogWarn = "Warn"
const LogError = "Error"
var err error
/**
创建一个新的Synapse
*/
func New() *Server {
return &Server{}
}
/**
启动 Synapse 组件, 开始监听RPC请求和事件
*/
func (s *Server) Serve() {
if s.AppName == "" || s.SysName == "" {
Log("Must Set SysName and AppName system", LogError)
return;
} else {
Log(fmt.Sprintf("System Name: %s", s.SysName), LogInfo)
Log(fmt.Sprintf("App Name: %s", s.AppName), LogInfo)
}
if s.AppId == "" {
s.AppId = s.randomString(20)
}
Log(fmt.Sprintf("App ID: %s", s.AppId), LogInfo)
if s.Debug {
Log("App Run Mode: Debug", LogDebug)
} else {
Log("App Run Mode: Production", LogInfo)
}
if s.EventProcessNum == 0 {
s.EventProcessNum = 20
}
if s.RpcProcessNum == 0 {
s.RpcProcessNum = 20
}
goto START
START:
s.createConnection()
defer s.conn.Close()
time.Sleep(time.Second * 2)
s.checkAndCreateExchange()
if s.EventCallback != nil {
eventChannel := s.eventQueue()
go s.eventServer(eventChannel)
for k, v := range s.EventCallback {
Log(fmt.Sprintf("*EVT: %s -> %s", k, runtime.FuncForPC(reflect.ValueOf(v).Pointer()).Name()), LogInfo)
}
} else {
Log("Event Server Disabled: EventCallback not set", LogWarn)
}
if s.RpcCallback != nil {
serverChannel := s.serverQueue()
go s.rpcServer(serverChannel)
for k, v := range s.RpcCallback {
Log(fmt.Sprintf("*RPC: %s -> %s", k, runtime.FuncForPC(reflect.ValueOf(v).Pointer()).Name()), LogInfo)
}
} else {
Log("Rpc Server Disabled: RpcCallback not set", LogWarn)
}
if s.DisableEventClient {
Log("Event Client Disabled: DisableEventClient set true", LogWarn)
} else {
s.eventClient()
Log("Event Client Ready", LogInfo)
}
if !s.DisableRpcClient {
s.cliResMap = make(map[string]chan *simplejson.Json)
s.clientQueue()
go s.rpcCallbackQueueListen()
if s.RpcTimeout == 0 {
s.RpcTimeout = 3
}
} else {
Log("Rpc Client Disabled: DisableRpcClient set true", LogWarn)
}
var closedConnChannel = s.conn.NotifyClose(make(chan *amqp.Error))
Log(fmt.Sprintf("Connection Error: %s , reconnect after 5 sec", <-closedConnChannel), LogError)
time.Sleep(5 * time.Second)
goto START
}
/**
创建到 Rabbit MQ的链接
*/
func (s *Server) createConnection() {
s.conn, err = amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%s/%s", s.MqUser, s.MqPass, s.MqHost, s.MqPort, s.MqVHost))
if err != nil {
Log(fmt.Sprintf("Failed to connect to RabbitMQ: %s", err), LogError)
}
Log("Rabbit MQ Connection Created.", LogInfo)
}
/**
创建到 Rabbit MQ 的通道
*/
func (s *Server) CreateChannel(processNum int, desc string) *amqp.Channel {
channel, err := s.conn.Channel()
if err != nil {
Log(fmt.Sprintf("Failed to open a channel: %s", err), LogError)
}
Log(fmt.Sprintf("%s Channel Created.", desc), LogInfo)
if processNum > 0 {
channel.Qos(
processNum, // prefetch count
0, // prefetch size
false, // global
)
Log(fmt.Sprintf("%s MaxProcessNum: %d", desc, processNum), LogInfo)
}
return channel
}
/**
注册通讯用的 MQ Exchange
*/
func (s *Server) checkAndCreateExchange() {
channel := s.CreateChannel(0, "Exchange")
err := channel.ExchangeDeclare(
s.SysName, // name
amqp.ExchangeTopic, // type
true, // durable
true, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
if err != nil {
Log(fmt.Sprintf("Failed to declare Exchange: %s", err), LogError)
}
Log("Register Exchange Successed.", LogInfo)
channel.Close()
Log("Exchange Channel Closed", LogInfo)
}
/**
生成随机字符串
*/
func (s *Server) randomString(l int) string {
str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
/**
控制台日志
*/
func Log(desc string, level string) {
fmt.Printf("[%s][Synapse %s] %s \n", time.Now().Format("2006-01-02 15:04:05"), level, desc)
}