Debian Codegraph @

autopaho/queue/memory/queue.go

raw symbols SCIP
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
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 // A queue implementation that stores all data in RAM
28
29 // Queue - basic memory based queue
30 type Queue struct {
31 mu sync.Mutex
32 messages [][]byte
33 waiting []chan<- struct{} // closed when something arrives in the queue
34 waitingForEmpty []chan<- struct{} // closed when queue is empty
35 }
36
37 // New creates a new memory-based queue
38 func New() *Queue {
39 return &Queue{}
40 }
41
42 // Wait returns a channel that is closed when there is something in the queue
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 // WaitForEmpty returns a channel which will be closed when the queue is empty
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 // Enqueue add item to the queue.
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 // Peek retrieves the oldest item from the queue (without removing it)
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 // Queue implements Entry directly (as this always references q.messages[0]
95 return q, nil
96 }
97
98 // Reader implements Entry.Reader - As the entry will always be the first item in the queue this is implemented
99 // against Queue rather than as a separate struct.
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 // Leave implements Entry.Leave - the entry (will be returned on subsequent calls to Peek)
110 func (q *Queue) Leave() error {
111 return nil // No action (item is already in the queue and there is nothing to close)
112 }
113
114 // Remove implements Entry.Remove this entry from the queue
115 func (q *Queue) Remove() error {
116 return q.remove()
117 }
118
119 // Quarantine implements Entry.Quarantine - Flag that this entry has an error (remove from queue, potentially retaining data with error flagged)
120 func (q *Queue) Quarantine() error {
121 return q.remove() // No way for us to actually quarantine this, so we just remove the item from the queue
122 }
123
124 // remove removes the first item in the queue.
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 { // Queue is now, or was already, empty
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 }