Skip to content

Commit 7fdb5ce

Browse files
committed
修复一个长期导致队列堵塞的bug
其他细节优化
1 parent a266a99 commit 7fdb5ce

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

db/queue.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"github.com/google/uuid"
7+
"gopkg.in/rroy233/logger.v2"
78
"sync"
89
"time"
910
)
@@ -45,7 +46,7 @@ var queue *QStruct
4546
var maxQueueSize int
4647
var QueueTimeout int64
4748

48-
const queueCleanerInterval = 30 * time.Second
49+
const queueCleanerInterval = 10 * time.Second
4950

5051
func initQueue(maxSize int) {
5152
if maxSize == 0 {
@@ -79,6 +80,13 @@ func queueCleaner() {
7980
}
8081
}
8182
queue.lock.Unlock()
83+
84+
p := queue.head
85+
for queue.data[p] != nil && queue.data[p].abort == true {
86+
queue.pop()
87+
p = (p + 1) % maxQueueSize
88+
}
89+
8290
time.Sleep(queueCleanerInterval)
8391
}
8492
}
@@ -180,22 +188,33 @@ func (q *QStruct) pop() *QItem {
180188
q.data[q.head] = nil
181189
q.size--
182190
q.head = (q.head + 1) % maxQueueSize
191+
if q.size == 0 {
192+
q.head = 0
193+
q.tail = 0
194+
}
183195
return item
184196
}
185197

186-
func (q *QStruct) print() {
198+
func (q *QStruct) debugPrint() {
187199
q.lock.Lock()
188200
defer q.lock.Unlock()
189-
fmt.Printf("[Size=%d,head=%d,tail=%d]->[\n", q.size, q.head, q.tail)
201+
text := ""
202+
text += fmt.Sprintf("[Size=%d,head=%d,tail=%d]->[\n", q.size, q.head, q.tail)
190203
for i := 0; i < maxQueueSize; i++ {
191-
fmt.Printf("\t")
204+
text += fmt.Sprintf("\t")
192205
if q.data[i] == nil {
193-
fmt.Printf("<nil>")
206+
text += fmt.Sprintf("<nil>")
194207
} else {
195-
fmt.Printf("UUID=%s\tUID=%d\tabort=%v", q.data[i].UUID, q.data[i].uid, q.data[i].abort)
208+
text += fmt.Sprintf("UUID=%s\tUID=%d\tabort=%v\tadd_time=%d\tqueueIndex=%d",
209+
q.data[i].UUID, q.data[i].uid, q.data[i].abort, q.data[i].addTime, q.data[i].queueIndex,
210+
)
196211
}
197-
fmt.Printf("\n")
212+
text += "\n"
213+
}
214+
if logger.Debug == nil {
215+
logger.New(&logger.Config{StdOutput: true})
198216
}
217+
logger.Debug.Println(text)
199218
return
200219
}
201220

handler/AddStickerUrlMessage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func AddStickerUrlMessage(update tgbotapi.Update) {
3232

3333
//len equal to 0
3434
if len(stickerSet.Stickers) == 0 {
35-
logger.Error.Println(userInfo+"len(stickerSet.Stickers) == 0", utils.JsonEncode(stickerSet))
35+
logger.Info.Println(userInfo+"len(stickerSet.Stickers) == 0", utils.JsonEncode(stickerSet))
3636
utils.SendPlainText(&update, languages.Get(&update).BotMsg.ErrFailedToDownload)
3737
return
3838
}

handler/queue_op.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handler
22

33
import (
4+
"errors"
45
"fmt"
56
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
67
"github.com/rroy233/StickerDownloader/db"
@@ -25,7 +26,8 @@ func enqueue(update *tgbotapi.Update, queueEditMsg *tgbotapi.Message) (*db.QItem
2526
needRecover := false
2627
qItem, err := db.EnQueue(utils.GetUID(update))
2728
if err != nil {
28-
if err == db.ErrorQueueFull {
29+
if errors.Is(err, db.ErrorQueueFull) {
30+
logger.Warn.Printf("[handler.enqueue]Queue is FULL! chatID:%d MsgID:%d", queueEditMsg.Chat.ID, queueEditMsg.MessageID)
2931
utils.EditMsgText(queueEditMsg.Chat.ID, queueEditMsg.MessageID, languages.Get(update).BotMsg.ErrSysBusy)
3032
return nil, true
3133
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/rroy233/StickerDownloader/statistics"
1212
"github.com/rroy233/StickerDownloader/utils"
1313
"gopkg.in/rroy233/logger.v2"
14+
"log"
1415
"os"
1516
"os/signal"
1617
"syscall"
@@ -25,6 +26,7 @@ var cancelCh chan int
2526
func main() {
2627
//config
2728
config.Init()
29+
log.Println("[main]config=" + utils.JsonEncode(config.Get()))
2830

2931
//logger
3032
logger.New(

0 commit comments

Comments
 (0)