Skip to content

Commit

Permalink
fix a deadlock and some other bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
graynk committed Jul 21, 2024
1 parent 8591ee6 commit 3ad2369
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/distortioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ func main() {
priorityChatsStr := strings.Split(os.Getenv("DISTORTIONER_PRIORITY_CHATS"), ",")
priorityChats := make([]int64, len(priorityChatsStr))
for i, s := range priorityChatsStr {
if s == "" {
continue
}
priorityChats[i], err = strconv.ParseInt(s, 10, 64)
if err != nil {
logger.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions app/handler_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (d DistorterBot) SendMessage(c tb.Context, toSend interface{}, method Metho
b.Reply(message, NotEnoughRights)
case strings.Contains(err.Error(), "bot was blocked by the user (403)"):
d.videoWorker.BanUser(message.Chat.ID)
return nil, nil
case strings.Contains(err.Error(), "telegram: Bad Request: message to be replied not found (400)"):
return d.SendMessage(c, toSend, Send)
}
Expand Down
22 changes: 9 additions & 13 deletions app/queue/honest_priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,11 @@ func (hjq *HonestJobQueue) Pop() *Job {

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

if _, ok := hjq.banned[job.userID]; ok {
allBannedJobsExhausted := true
for _, queued := range hjq.queue {
if queued.userID == job.userID {
allBannedJobsExhausted = false
break
}
for _, ok := hjq.banned[job.userID]; ok; {
if hjq.queue.Len() == 0 {
return nil
}

if allBannedJobsExhausted {
delete(hjq.banned, job.userID)
}

return hjq.Pop()
job = heap.Pop(&hjq.queue).(*Job)
}

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

// if a user sent us a message then we're clearly unbanned
if _, ok := hjq.banned[userID]; ok {
delete(hjq.banned, userID)
}

hjq.users[userID] = priority + 1

job := newJob(userID, priority, runnable)
Expand Down
5 changes: 4 additions & 1 deletion app/tools/video_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func (vw *VideoWorker) BanUser(userID int64) {

func (vw *VideoWorker) run() {
for range vw.messenger {
vw.queue.Pop().Run()
job := vw.queue.Pop()
if job != nil {
job.Run()
}
}
}

Expand Down

0 comments on commit 3ad2369

Please sign in to comment.