-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrouter.go
157 lines (134 loc) · 3.59 KB
/
router.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
package main
import (
"galopush/internal/logs"
"galopush/internal/nsq"
"galopush/internal/rds"
// "galopush/internal/redisstore"
"galopush/internal/rpc"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
"github.com/widuu/goini"
)
type Router struct {
//rpc服务
routerRpcAddr string
maxRpcInFight int
rpcServer *rpc.RpcServer
//nsq服务
topics []string
discover *nsq.TopicDiscoverer
nsqlookupAddr []string
nsqdTcpAddr string
producer *nsq.Producer //nsq生产者
upMsgTopic string
//负载路由
httpBindAddr string
cometExit chan string //cometId exit channel
pool *Pool
//离线消息
//store *redisstore.Storager
store *rds.Storager
//系统控制
exit chan struct{}
wg sync.WaitGroup
}
func (p *Router) Init() {
conf := goini.SetConfig("./config.ini")
logs.Logger.Debug("--------OnInit--------")
//RPC
{
p.routerRpcAddr = conf.GetValue("router", "rpcAddr")
s := conf.GetValue("router", "rpcServerCache")
p.maxRpcInFight, _ = strconv.Atoi(s)
logs.Logger.Debug("----router rpc addr=", p.routerRpcAddr, " cache=", p.maxRpcInFight)
}
//NSQ
{
s := conf.GetValue("nsq", "nsqlookupAddr")
p.nsqlookupAddr = strings.Split(s, ",")
s = conf.GetValue("nsq", "topics")
p.topics = strings.Split(s, ",")
p.nsqdTcpAddr = conf.GetValue("nsq", "tcpAddr")
p.upMsgTopic = conf.GetValue("nsq", "upMsgTopic")
logs.Logger.Debug("----nsqd nsqlookup addr=", p.nsqlookupAddr, " topics=", p.topics, " upMsgTopic=", p.upMsgTopic)
}
//HTTP
{
p.httpBindAddr = conf.GetValue("http", "bindAddr")
logs.Logger.Debug("----http addr=", p.httpBindAddr, " cache=", p.maxRpcInFight)
}
p.cometExit = make(chan string)
p.pool = new(Pool)
p.pool.comets = make(map[string]*comet)
// p.pool.sessions = make(map[string]*session)
//REDIS
{
dbconn := conf.GetValue("redis", "conn")
password := conf.GetValue("redis", "password")
password = strings.TrimSpace(password)
databaseS := conf.GetValue("redis", "database")
database, err := strconv.Atoi(databaseS)
if err != nil {
database = 0
}
p.store = rds.NewStorager(dbconn, password, database)
logs.Logger.Debug("----redis addr=", dbconn, " password:", password, " database:", database)
}
//开启统计输出
go p.stat()
logs.Logger.Debug("--------Init success--------")
}
func (p *Router) Start() {
defer func() {
if r := recover(); r != nil {
logs.Logger.Critical("Start.recover:", r)
go p.Start()
}
}()
p.rpcServer = rpc.NewRpcServer(p.routerRpcAddr, p.maxRpcInFight, p.RpcSyncHandle, p.RpcAsyncHandle)
//处理comet异常中断 清除comet以及comet上注册的用户
go func() {
for {
select {
case id := <-p.cometExit:
p.pool.deleteComet(id)
p.store.OfflineComet(id)
//p.pool.deleteSessionsWithCometId(id)
}
}
}()
p.discover = nsq.NewTopicDiscoverer(p.topics, p.maxRpcInFight, p.nsqlookupAddr, p.NsqHandler)
producer, err := nsq.NewProducer(p.nsqdTcpAddr)
if err != nil {
panic(err)
}
p.producer = producer
p.startHttpServer()
logs.Logger.Debug("--------Start Router success--------")
}
func (p *Router) Stop() error {
debug.PrintStack()
close(p.exit)
return nil
}
//newRpcClient 返回一个RPC客户端
func (p *Router) NewRpcClient(name, addr string, ch chan int) (*rpc.RpcClient, error) {
c, err := rpc.NewRpcClient(name, addr, ch)
if err != nil {
logs.Logger.Error("NewRpcClient ", err)
return c, err
}
return c, err
}
func (p *Router) stat() {
t := time.NewTicker(time.Second * 60)
for {
select {
case <-t.C:
logs.Logger.Debug("Registered ", len(p.pool.comets), " comets with ", p.store.SessionCount(), " sessions")
}
}
}