forked from open-falcon/falcon-plus
/
im_sender.go
45 lines (39 loc) · 919 Bytes
/
im_sender.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
package cron
import (
log "github.com/Sirupsen/logrus"
"github.com/open-falcon/falcon-plus/modules/alarm/g"
"github.com/open-falcon/falcon-plus/modules/alarm/model"
"github.com/open-falcon/falcon-plus/modules/alarm/redi"
"github.com/toolkits/net/httplib"
"time"
)
func ConsumeIM() {
for {
L := redi.PopAllIM()
if len(L) == 0 {
time.Sleep(time.Millisecond * 200)
continue
}
SendIMList(L)
}
}
func SendIMList(L []*model.IM) {
for _, im := range L {
IMWorkerChan <- 1
go SendIM(im)
}
}
func SendIM(im *model.IM) {
defer func() {
<-IMWorkerChan
}()
url := g.Config().Api.IM
r := httplib.Post(url).SetTimeout(5*time.Second, 30*time.Second)
r.Param("tos", im.Tos)
r.Param("content", im.Content)
resp, err := r.String()
if err != nil {
log.Errorf("send im fail, tos:%s, cotent:%s, error:%v", im.Tos, im.Content, err)
}
log.Debugf("send im:%v, resp:%v, url:%s", im, resp, url)
}