Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix #154

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ type StringToInt64MappingSeq struct {
mapping map[string]int64
}

// Int64Stack 用于存储 int64 的栈
type Int64Stack struct {
mu sync.Mutex
stack []int64
}

// 定义一个全局的 Int64Stack 实例
var globalInt64Stack = &Int64Stack{
stack: make([]int64, 0),
}

var globalEchoMapping = &EchoMapping{
msgTypeMapping: make(map[string]string),
msgIDMapping: make(map[string]string),
Expand Down Expand Up @@ -120,3 +131,28 @@ func GetMappingFileTimeLimit(key string) int64 {
defer globalStringToInt64MappingSeq.mu.Unlock()
return globalStringToInt64MappingSeq.mapping[key]
}

// Add 添加一个新的 int64 到全局栈中
func AddFileTimeLimit(value int64) {
globalInt64Stack.mu.Lock()
defer globalInt64Stack.mu.Unlock()

// 添加新元素到栈顶
globalInt64Stack.stack = append(globalInt64Stack.stack, value)

// 如果栈的大小超过 10,移除最早添加的元素
if len(globalInt64Stack.stack) > 10 {
globalInt64Stack.stack = globalInt64Stack.stack[1:]
}
}

// Get 获取全局栈顶的元素
func GetFileTimeLimit() int64 {
globalInt64Stack.mu.Lock()
defer globalInt64Stack.mu.Unlock()

if len(globalInt64Stack.stack) == 0 {
return 0 // 当栈为空时返回 0
}
return globalInt64Stack.stack[len(globalInt64Stack.stack)-1]
}
22 changes: 22 additions & 0 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
echo.AddMappingSeq(messageID, msgseq+1)
//时间限制
lastSendTimestamp := echo.GetMappingFileTimeLimit(messageID)
if lastSendTimestamp == 0 {
lastSendTimestamp = echo.GetFileTimeLimit()
}
now := time.Now()
millis := now.UnixMilli()
diff := millis - lastSendTimestamp
Expand All @@ -153,6 +156,7 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
mylog.Println("延迟完成")
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessageCopy)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
Expand All @@ -177,8 +181,26 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
} else {
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, err.Error(), msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送文本报错信息失败: %v", err)
}
}
}
}
//发送成功回执
Expand Down
23 changes: 23 additions & 0 deletions handlers/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
echo.AddMappingSeq(messageID, msgseq+1)
//时间限制
lastSendTimestamp := echo.GetMappingFileTimeLimit(messageID)
if lastSendTimestamp == 0 {
lastSendTimestamp = echo.GetFileTimeLimit()
}
now := time.Now()
millis := now.UnixMilli()
diff := millis - lastSendTimestamp
Expand All @@ -153,6 +156,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
mylog.Println("延迟完成")
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessageCopy)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
Expand All @@ -177,9 +181,28 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
} else {
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, err.Error(), msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送文本报错信息失败: %v", err)
}
}
}

}
//发送成功回执
SendResponse(client, err, &message)
Expand Down
5 changes: 5 additions & 0 deletions handlers/send_private_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
echo.AddMappingSeq(messageID, msgseq+1)
//时间限制
lastSendTimestamp := echo.GetMappingFileTimeLimit(messageID)
if lastSendTimestamp == 0 {
lastSendTimestamp = echo.GetFileTimeLimit()
}
now := time.Now()
millis := now.UnixMilli()
diff := millis - lastSendTimestamp
Expand All @@ -158,13 +161,15 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
mylog.Println("延迟完成")
_, err := apiv2.PostC2CMessage(context.TODO(), UserID, richMediaMessageCopy)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 私聊信息失败: %v", key, err)
}
})
} else { // 发送消息
_, err := apiv2.PostC2CMessage(context.TODO(), UserID, richMediaMessage)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 私聊信息失败: %v", key, err)
}
Expand Down
Loading