main.go 3.89 KB
Newer Older
hujiebin's avatar
hujiebin committed
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
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis/v8"
	"hilo-micCenter/common/dingding"
	"hilo-micCenter/common/mylogrus"
	"hilo-micCenter/common/redisCli"
	"hilo-micCenter/common/tencentyun"
	"time"
)

const SEND_WORKER = 2000   // 消费端协程数量
const MONITOR_LENGTH = 100 // 队列告警数量

var sendChan chan GroupSystemMsg

func main() {
	//if !config.IsMaster() {
	//	return
	//}
	mylogrus.MyLog.Infof("cron micChangeSys start")
	// 8核 n send + 4 blpop
	sendChan = make(chan GroupSystemMsg, SEND_WORKER)
	for i := 0; i < 4; i++ {
		go func() {
			deal()
		}()
	}
	for i := 0; i < SEND_WORKER; i++ {
		go func() {
			send()
		}()
	}
	go check()
	select {}
}

func check() {
	tick := time.NewTicker(time.Second * 3)
	defer tick.Stop()
	for {
		select {
		case <-tick.C:
JiebinHu's avatar
JiebinHu committed
47
			l, err := redisCli.GetRedisCluster().LLen(context.Background(), micInfoChange).Result()
hujiebin's avatar
hujiebin committed
48
			if err != nil {
hujiebin's avatar
hujiebin committed
49
				mylogrus.MyLog.Errorf("cron micChangeSys msg error,left %v-%v", l, err)
hujiebin's avatar
hujiebin committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
			}
			if l > MONITOR_LENGTH {
				go func() {
					if sErr := dingding.SendDingRobot(dingding.ROBOTWEBHOOK, fmt.Sprintf("麦位变化通知延迟,队列%s长度:%d", micInfoChange, l), true); sErr != nil {
						mylogrus.MyLog.Errorf("dingding msg fail:%v", sErr)
					}
				}()
			}
			if l > 0 {
				mylogrus.MyLog.Infof("cron micChangeSys msg,left %v", l)
			}
		}
	}
}

func deal() {
	for true {
		//不需要加锁,注意,阻塞。
JiebinHu's avatar
JiebinHu committed
68
		strs, err := redisCli.GetRedisCluster().BLPop(context.Background(), time.Second, micInfoChange).Result()
hujiebin's avatar
hujiebin committed
69 70 71 72 73 74 75
		if err != nil {
			if err != redis.Nil {
				mylogrus.MyLog.Errorf("cron micChangeSys redisCli.GetRedis().BLPop err:+%v", err)
			}
		}
		if len(strs) >= 2 {
			content := strs[1]
hujiebin's avatar
hujiebin committed
76
			//mylogrus.MyLog.Infof("cron micChangeSys content:%v", content)
hujiebin's avatar
hujiebin committed
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
			micSystemMsg := MicSystemMsg{}
			if err := json.Unmarshal([]byte(content), &micSystemMsg); err != nil {
				mylogrus.MyLog.Errorf("cron micChangeSys Unmarshal err:%+v, content:%v", err, content)
			}
			sendChan <- GroupSystemMsg{
				MsgGroupUid: micSystemMsg.GroupUid,
				MsgId:       micSystemMsg.MsgId,
				Source:      micSystemMsg.Source,
				Target:      micSystemMsg.Target,
				Content:     micSystemMsg.Content,
			}
		}
	}
}

//var limiter = rate.NewLimiter(2000, 2000)

func send() {
	for msg := range sendChan {
		//if err := limiter.Wait(context.Background()); err == nil {
		SendSignalMsg(msg.MsgGroupUid, msg)
		//}
	}
}

const micInfoChange = "mic_info_change"

type MicSystemMsg struct {
	GroupUid string //房间ID
	Source   string
	Target   string
	MsgId    uint8
	Content  string //要发送的内容
}

type GroupSystemMsg struct {
	MsgGroupUid string `json:"-"`
	MsgId       uint8  `json:"msgId"`
	Source      string `json:"source"`
	Target      string `json:"target"`
	Content     string `json:"content"`
}

// SendSignalMsg 发送群信令。入参是内部imGroupId,这里做转换
func SendSignalMsg(groupId string, msg GroupSystemMsg) {
	buffer, err := json.Marshal(msg)
	if err == nil {
		str := string(buffer)
hujiebin's avatar
hujiebin committed
125
		//mylogrus.MyLog.Infof("cron micChangeSys SendSignalMsg: %s", str)
hujiebin's avatar
hujiebin committed
126 127 128 129
		logger := mylogrus.MyLog.WithField("msgId", msg.MsgId)

		if err = tencentyun.SendSystemMsg(logger, groupId, []string{}, str); err != nil {
			mylogrus.MyLog.Errorf("cron micChangeSys SendSignalMsg sync failed for %s, msgId = %d, content:%v, err:%+v", groupId, msg.MsgId, str, err)
JiebinHu's avatar
JiebinHu committed
130
			content := fmt.Sprintf("腾讯云推送失败,group:%v,msgId:%v,err:%v", groupId, msg.MsgId, err.Error())
hujiebin's avatar
hujiebin committed
131 132 133 134
			if err := dingding.SendDingRobot(dingding.ROBOTWEBHOOK, content, true); err != nil {
				mylogrus.MyLog.Errorf("cron micChangeSys SendSignalMsg send dingding fail%s", err.Error())
			}
		} else {
hujiebin's avatar
hujiebin committed
135
			//mylogrus.MyLog.Infof("cron micChangeSys SendSignalMsg sync success for %s, msgId = %d, content:%v", groupId, msg.MsgId, str)
hujiebin's avatar
hujiebin committed
136 137 138 139 140
		}
	} else {
		mylogrus.MyLog.Errorf("cron micChangeSys SendSignalMsg failure, msgId = %d : %s , msg:%+v", msg.MsgId, err.Error(), msg)
	}
}