88 "strings"
99 "sync"
1010 "time"
11-
11+
1212 "github.com/yincongcyincong/MuseBot/admin/conf"
1313 "github.com/yincongcyincong/MuseBot/admin/db"
1414 "github.com/yincongcyincong/MuseBot/admin/utils"
@@ -32,17 +32,17 @@ type BotStatus struct {
3232}
3333
3434func InitStatusCheck () {
35- if * conf .RegisterConfInfo .Type != "" {
35+ if conf .RegisterConfInfo .Type != "" {
3636 go func () {
3737 InitRegister ()
3838 }()
39-
39+
4040 } else {
4141 go func () {
4242 ManualCheckPoint ()
4343 }()
4444 }
45-
45+
4646}
4747
4848func ManualCheckPoint () {
@@ -52,9 +52,9 @@ func ManualCheckPoint() {
5252 }
5353 }()
5454 ScheduleBotChecks ()
55- ticker := time .NewTicker (time .Duration (* conf .BaseConfInfo .CheckBotSec ) * time .Second )
55+ ticker := time .NewTicker (time .Duration (conf .BaseConfInfo .CheckBotSec ) * time .Second )
5656 defer ticker .Stop ()
57-
57+
5858 for {
5959 select {
6060 case <- ticker .C :
@@ -72,12 +72,12 @@ func checkBotStatus(bot *db.Bot) string {
7272 return "offline" // 请求失败
7373 }
7474 defer resp .Body .Close ()
75-
75+
7676 if resp .StatusCode != http .StatusOK {
7777 logger .Warn ("checkpoint request fail" , "resp" , resp , "address" , bot .Address )
7878 return OfflineStatus
7979 }
80-
80+
8181 return OnlineStatus
8282}
8383
@@ -87,16 +87,16 @@ func ScheduleBotChecks() {
8787 logger .Error ("ScheduleBotChecks panic" , "err" , err , "stack" , string (debug .Stack ()))
8888 }
8989 }()
90-
90+
9191 bots , _ , err := db .ListBots (0 , 10000 , "" )
9292 if err != nil {
9393 logger .Error ("ScheduleBotChecks list bots fail" , "err" , err )
9494 return
9595 }
96-
97- batchCount := * conf .BaseConfInfo .CheckBotSec
96+
97+ batchCount := conf .BaseConfInfo .CheckBotSec
9898 batchSize := (len (bots ) + batchCount - 1 ) / batchCount
99-
99+
100100 var wg sync.WaitGroup
101101 for i := 0 ; i < batchCount ; i ++ {
102102 start := i * batchSize
@@ -107,10 +107,10 @@ func ScheduleBotChecks() {
107107 if start >= len (bots ) {
108108 break
109109 }
110-
110+
111111 batch := bots [start :end ]
112112 batchIndex := i
113-
113+
114114 wg .Add (1 )
115115 go func (batch []* db.Bot , batchIndex int ) {
116116 defer func () {
@@ -119,10 +119,10 @@ func ScheduleBotChecks() {
119119 }
120120 wg .Done ()
121121 }()
122-
122+
123123 timer := time .NewTimer (time .Duration (batchIndex ) * time .Second )
124124 <- timer .C
125-
125+
126126 for _ , b := range batch {
127127 status := checkBotStatus (b )
128128 BotMap .Store (b .ID , & BotStatus {
@@ -135,12 +135,12 @@ func ScheduleBotChecks() {
135135 }
136136 }(batch , batchIndex )
137137 }
138-
138+
139139 wg .Wait ()
140140}
141141
142142func InitRegister () {
143- switch * conf .RegisterConfInfo .Type {
143+ switch conf .RegisterConfInfo .Type {
144144 case "etcd" :
145145 InitEtcdRegister ()
146146 }
@@ -150,34 +150,34 @@ func InitEtcdRegister() {
150150 if len (conf .RegisterConfInfo .EtcdURLs ) == 0 {
151151 return
152152 }
153-
153+
154154 cli , err := clientv3 .New (clientv3.Config {
155155 Endpoints : conf .RegisterConfInfo .EtcdURLs ,
156156 DialTimeout : 5 * time .Second ,
157- Username : * conf .RegisterConfInfo .EtcdUsername ,
158- Password : * conf .RegisterConfInfo .EtcdPassword ,
157+ Username : conf .RegisterConfInfo .EtcdUsername ,
158+ Password : conf .RegisterConfInfo .EtcdPassword ,
159159 })
160160 if err != nil {
161161 logger .Error ("register init failed: " , err )
162162 return
163163 }
164164 defer cli .Close ()
165-
165+
166166 ctx := context .Background ()
167167 prefix := "/services/musebot/"
168-
168+
169169 resp , err := cli .Get (ctx , prefix , clientv3 .WithPrefix ())
170170 if err != nil {
171171 logger .Error ("register get failed: " , err )
172172 return
173173 }
174-
174+
175175 for _ , kv := range resp .Kvs {
176176 parts := strings .Split (string (kv .Key ), "/" )
177177 name := parts [len (parts )- 1 ]
178-
178+
179179 id := utils .NormalizeAddress (string (kv .Value ))
180-
180+
181181 BotMap .Store (name , & BotStatus {
182182 Id : id ,
183183 Name : name ,
@@ -186,19 +186,19 @@ func InitEtcdRegister() {
186186 LastCheck : time .Now (),
187187 })
188188 }
189-
189+
190190 rch := cli .Watch (ctx , prefix , clientv3 .WithPrefix (), clientv3 .WithPrevKV ())
191-
191+
192192 for wresp := range rch {
193193 for _ , ev := range wresp .Events {
194194 key := string (ev .Kv .Key )
195195 val := string (ev .Kv .Value )
196-
196+
197197 switch ev .Type {
198198 case clientv3 .EventTypePut :
199199 parts := strings .Split (key , "/" )
200200 name := parts [len (parts )- 1 ]
201-
201+
202202 id := utils .NormalizeAddress (val )
203203 BotMap .Store (name , & BotStatus {
204204 Id : id ,
0 commit comments