Skip to content

Commit 47d94a1

Browse files
committed
Lock for Empty and readToChan
1 parent d82abd9 commit 47d94a1

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

modules/queue/queue_bytefifo.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"sync"
12-
"sync/atomic"
1312
"time"
1413

1514
"code.gitea.io/gitea/modules/log"
@@ -81,6 +80,8 @@ func (q *ByteFIFOQueue) PushFunc(data Data, fn func() error) error {
8180

8281
// IsEmpty checks if the queue is empty
8382
func (q *ByteFIFOQueue) IsEmpty() bool {
83+
q.lock.Lock()
84+
defer q.lock.Unlock()
8485
if !q.WorkerPool.IsEmpty() {
8586
return false
8687
}
@@ -119,32 +120,32 @@ func (q *ByteFIFOQueue) readToChan() {
119120
q.cancel()
120121
return
121122
default:
122-
atomic.AddInt64(&q.numInQueue, 1)
123+
q.lock.Lock()
123124
bs, err := q.byteFIFO.Pop()
124125
if err != nil {
126+
q.lock.Unlock()
125127
log.Error("%s: %s Error on Pop: %v", q.typ, q.name, err)
126-
atomic.AddInt64(&q.numInQueue, -1)
127128
time.Sleep(time.Millisecond * 100)
128129
continue
129130
}
130131

131132
if len(bs) == 0 {
132-
atomic.AddInt64(&q.numInQueue, -1)
133+
q.lock.Unlock()
133134
time.Sleep(time.Millisecond * 100)
134135
continue
135136
}
136137

137138
data, err := unmarshalAs(bs, q.exemplar)
138139
if err != nil {
139140
log.Error("%s: %s Failed to unmarshal with error: %v", q.typ, q.name, err)
140-
atomic.AddInt64(&q.numInQueue, -1)
141+
q.lock.Unlock()
141142
time.Sleep(time.Millisecond * 100)
142143
continue
143144
}
144145

145146
log.Trace("%s %s: Task found: %#v", q.typ, q.name, data)
146147
q.WorkerPool.Push(data)
147-
atomic.AddInt64(&q.numInQueue, -1)
148+
q.lock.Unlock()
148149
}
149150
}
150151
}
@@ -222,6 +223,5 @@ func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) {
222223
if err != nil {
223224
return false, err
224225
}
225-
226226
return q.byteFIFO.(UniqueByteFIFO).Has(bs)
227227
}

0 commit comments

Comments
 (0)