| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package memory |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"github.com/eclipse/paho.golang/autopaho/queue" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
type Queue struct { |
| 31 |
mu sync.Mutex |
| 32 |
messages [][]byte |
| 33 |
waiting []chan<- struct{} |
| 34 |
waitingForEmpty []chan<- struct{} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func New() *Queue { |
| 39 |
return &Queue{} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (q *Queue) Wait() chan struct{} { |
| 44 |
c := make(chan struct{}) |
| 45 |
q.mu.Lock() |
| 46 |
if len(q.messages) > 0 { |
| 47 |
q.mu.Unlock() |
| 48 |
close(c) |
| 49 |
return c |
| 50 |
} |
| 51 |
q.waiting = append(q.waiting, c) |
| 52 |
q.mu.Unlock() |
| 53 |
return c |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func (q *Queue) WaitForEmpty() chan struct{} { |
| 58 |
c := make(chan struct{}) |
| 59 |
q.mu.Lock() |
| 60 |
if len(q.messages) == 0 { |
| 61 |
q.mu.Unlock() |
| 62 |
close(c) |
| 63 |
return c |
| 64 |
} |
| 65 |
q.waitingForEmpty = append(q.waitingForEmpty, c) |
| 66 |
q.mu.Unlock() |
| 67 |
return c |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (q *Queue) Enqueue(p io.Reader) error { |
| 72 |
var b bytes.Buffer |
| 73 |
_, err := b.ReadFrom(p) |
| 74 |
if err != nil { |
| 75 |
return fmt.Errorf("Queue.Push failed to read into buffer: %w", err) |
| 76 |
} |
| 77 |
q.mu.Lock() |
| 78 |
defer q.mu.Unlock() |
| 79 |
q.messages = append(q.messages, b.Bytes()) |
| 80 |
for _, c := range q.waiting { |
| 81 |
close(c) |
| 82 |
} |
| 83 |
q.waiting = q.waiting[:0] |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func (q *Queue) Peek() (queue.Entry, error) { |
| 89 |
q.mu.Lock() |
| 90 |
defer q.mu.Unlock() |
| 91 |
if len(q.messages) == 0 { |
| 92 |
return nil, queue.ErrEmpty |
| 93 |
} |
| 94 |
|
| 95 |
return q, nil |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func (q *Queue) Reader() (io.Reader, error) { |
| 101 |
q.mu.Lock() |
| 102 |
defer q.mu.Unlock() |
| 103 |
if len(q.messages) == 0 { |
| 104 |
return nil, queue.ErrEmpty |
| 105 |
} |
| 106 |
return bytes.NewReader(q.messages[0]), nil |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (q *Queue) Leave() error { |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func (q *Queue) Remove() error { |
| 116 |
return q.remove() |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func (q *Queue) Quarantine() error { |
| 121 |
return q.remove() |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (q *Queue) remove() error { |
| 126 |
q.mu.Lock() |
| 127 |
defer q.mu.Unlock() |
| 128 |
initialLen := len(q.messages) |
| 129 |
if initialLen > 0 { |
| 130 |
q.messages = q.messages[1:] |
| 131 |
} |
| 132 |
if initialLen <= 1 { |
| 133 |
for _, c := range q.waitingForEmpty { |
| 134 |
close(c) |
| 135 |
} |
| 136 |
q.waitingForEmpty = q.waitingForEmpty[:0] |
| 137 |
if initialLen == 0 { |
| 138 |
return queue.ErrEmpty |
| 139 |
} |
| 140 |
} |
| 141 |
return nil |
| 142 |
} |