Skip to content

Commit 8b1a8f5

Browse files
committed
🐛 添加锁
1 parent 68a075e commit 8b1a8f5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

plugin/chatcount/model.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package chatcount
22

33
import (
44
"os"
5+
"sync"
56
"time"
67

78
"github.com/jinzhu/gorm"
@@ -14,8 +15,10 @@ const (
1415
var (
1516
// ctdb 聊天时长数据库全局变量
1617
ctdb *chattimedb
17-
// 水群提醒时间提醒段,单位分钟
18+
// levelArray 水群提醒时间提醒段,单位分钟
1819
levelArray = [...]int{15, 30, 60, 120, 240}
20+
// chatmu 读写添加锁
21+
chatmu sync.Mutex
1922
)
2023

2124
// chattimedb 聊天时长数据库结构体
@@ -61,8 +64,10 @@ func (ChatTime) TableName() string {
6164
return "chat_time"
6265
}
6366

64-
// sleep 更新发言时间,todayTime的单位是分钟
67+
// updateChatTime 更新发言时间,todayTime的单位是分钟
6568
func (ctdb *chattimedb) updateChatTime(gid, uid int64) (todayTime int64, remindFlag bool) {
69+
chatmu.Lock()
70+
defer chatmu.Unlock()
6671
db := (*gorm.DB)(ctdb)
6772
now := time.Now()
6873
st := ChatTime{
@@ -108,6 +113,8 @@ func (ctdb *chattimedb) updateChatTime(gid, uid int64) (todayTime int64, remindF
108113

109114
// getChatTime 获得用户聊天时长,todayTime,totalTime的单位是分钟
110115
func (ctdb *chattimedb) getChatTime(gid, uid int64) (todayTime int64, totalTime int64) {
116+
chatmu.Lock()
117+
defer chatmu.Unlock()
111118
db := (*gorm.DB)(ctdb)
112119
st := ChatTime{}
113120
db.Model(&ChatTime{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st)
@@ -118,6 +125,8 @@ func (ctdb *chattimedb) getChatTime(gid, uid int64) (todayTime int64, totalTime
118125

119126
// getChatRank 获得水群排名
120127
func (ctdb *chattimedb) getChatRank(gid int64) (chatTimeList []ChatTime) {
128+
chatmu.Lock()
129+
defer chatmu.Unlock()
121130
db := (*gorm.DB)(ctdb)
122131
db.Model(&ChatTime{}).Where("group_id = ?", gid).Order("today_time DESC").Find(&chatTimeList)
123132
return

0 commit comments

Comments
 (0)