-
Notifications
You must be signed in to change notification settings - Fork 402
/
queue.go
53 lines (39 loc) · 1014 Bytes
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package audit
import (
"sync"
"github.com/zeebo/errs"
"storj.io/common/storj"
)
// ErrEmptyQueue is used to indicate that the queue is empty.
var ErrEmptyQueue = errs.Class("empty audit queue")
// Queue is a list of paths to audit, shared between the reservoir chore and audit workers.
type Queue struct {
mu sync.Mutex
queue []storj.Path
}
// Swap switches the backing queue slice with a new queue slice.
func (q *Queue) Swap(newQueue []storj.Path) {
q.mu.Lock()
defer q.mu.Unlock()
q.queue = newQueue
}
// Next gets the next item in the queue.
func (q *Queue) Next() (storj.Path, error) {
q.mu.Lock()
defer q.mu.Unlock()
// return error if queue is empty
if len(q.queue) == 0 {
return "", ErrEmptyQueue.New("")
}
next := q.queue[0]
q.queue = q.queue[1:]
return next, nil
}
// Size returns the size of the queue.
func (q *Queue) Size() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.queue)
}