Skip to content

Commit

Permalink
perf: 删除无订阅者的topic,移除无意义的随机数种子设置
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer committed Dec 23, 2021
1 parent cf6a3f9 commit b186b2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 1 addition & 2 deletions client/qq.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func (q *QQ) Init() {

// 发送消息
func (q *QQ) SendMessage(userID int64, message string, isGroup bool) {
rand.Seed(time.Now().Unix()) //设置随机数种子
time.Sleep(time.Duration(rand.Int63n(30)) * time.Second) //尝试随机sleep,或可降低风控风险
time.Sleep(time.Duration(rand.Int63n(30)) * time.Second) //尝试随机sleep[0,30)秒,或可降低风控风险
if isGroup {
q.bot.SendGroupMessage(userID, message)
} else {
Expand Down
13 changes: 12 additions & 1 deletion message/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func (m *Queue) overwrite(topic string, receiverList []chan string) {
m.lock.Unlock()
}

func (m *Queue) delete(topic string) {
m.lock.Lock()
delete(m.mq, topic)
m.lock.Unlock()
}

// 内部的消息广播实现
func (m *Queue) broadcast(message string, receiverList []chan string) {
alert := func(receiver chan string) {
Expand Down Expand Up @@ -71,7 +77,12 @@ func (m *Queue) Unsubscribe(topic string, channel chan string) error {
for index := range receiverList {
receiverChan := receiverList[index]
if receiverChan == channel {
m.overwrite(topic, append(receiverList[:index], receiverList[index+1:]...))
newList := append(receiverList[:index], receiverList[index+1:]...)
if len(newList) > 0 {
m.overwrite(topic, newList)
} else { // 某topic无订阅者则直接删除topic
m.delete(topic)
}
}
}
return nil
Expand Down

0 comments on commit b186b2e

Please sign in to comment.