Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit 3ad2369

Browse files
committed
fix a deadlock and some other bugs
1 parent 8591ee6 commit 3ad2369

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

app/distortioner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ func main() {
344344
priorityChatsStr := strings.Split(os.Getenv("DISTORTIONER_PRIORITY_CHATS"), ",")
345345
priorityChats := make([]int64, len(priorityChatsStr))
346346
for i, s := range priorityChatsStr {
347+
if s == "" {
348+
continue
349+
}
347350
priorityChats[i], err = strconv.ParseInt(s, 10, 64)
348351
if err != nil {
349352
logger.Fatal(err)

app/handler_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (d DistorterBot) SendMessage(c tb.Context, toSend interface{}, method Metho
137137
b.Reply(message, NotEnoughRights)
138138
case strings.Contains(err.Error(), "bot was blocked by the user (403)"):
139139
d.videoWorker.BanUser(message.Chat.ID)
140+
return nil, nil
140141
case strings.Contains(err.Error(), "telegram: Bad Request: message to be replied not found (400)"):
141142
return d.SendMessage(c, toSend, Send)
142143
}

app/queue/honest_priority_queue.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,11 @@ func (hjq *HonestJobQueue) Pop() *Job {
7676

7777
job := heap.Pop(&hjq.queue).(*Job)
7878

79-
if _, ok := hjq.banned[job.userID]; ok {
80-
allBannedJobsExhausted := true
81-
for _, queued := range hjq.queue {
82-
if queued.userID == job.userID {
83-
allBannedJobsExhausted = false
84-
break
85-
}
79+
for _, ok := hjq.banned[job.userID]; ok; {
80+
if hjq.queue.Len() == 0 {
81+
return nil
8682
}
87-
88-
if allBannedJobsExhausted {
89-
delete(hjq.banned, job.userID)
90-
}
91-
92-
return hjq.Pop()
83+
job = heap.Pop(&hjq.queue).(*Job)
9384
}
9485

9586
hjq.users[job.userID]--
@@ -134,6 +125,11 @@ func (hjq *HonestJobQueue) Push(userID int64, runnable func()) error {
134125
return errors.New("You're distorting videos too often, wait until the previous ones have been processed")
135126
}
136127

128+
// if a user sent us a message then we're clearly unbanned
129+
if _, ok := hjq.banned[userID]; ok {
130+
delete(hjq.banned, userID)
131+
}
132+
137133
hjq.users[userID] = priority + 1
138134

139135
job := newJob(userID, priority, runnable)

app/tools/video_worker.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func (vw *VideoWorker) BanUser(userID int64) {
2929

3030
func (vw *VideoWorker) run() {
3131
for range vw.messenger {
32-
vw.queue.Pop().Run()
32+
job := vw.queue.Pop()
33+
if job != nil {
34+
job.Run()
35+
}
3336
}
3437
}
3538

0 commit comments

Comments
 (0)