-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathfifoqueue.go
More file actions
43 lines (37 loc) · 1.42 KB
/
fifoqueue.go
File metadata and controls
43 lines (37 loc) · 1.42 KB
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
package engine
import (
"github.com/onflow/flow-go/engine/common/fifoqueue"
)
// FifoMessageStore wraps a FiFo Queue to implement the MessageStore interface.
type FifoMessageStore struct {
*fifoqueue.FifoQueue
}
var _ MessageStore = (*FifoMessageStore)(nil)
// NewFifoMessageStore creates a FifoMessageStore backed by a [fifoqueue.FifoQueue].
// No errors are expected during normal operations.
func NewFifoMessageStore(maxCapacity int) (*FifoMessageStore, error) {
queue, err := fifoqueue.NewFifoQueue(maxCapacity)
if err != nil {
return nil, err
}
return &FifoMessageStore{FifoQueue: queue}, nil
}
// Put appends the given value to the tail of the queue.
// Returns true if and only if the element was added, or false if the
// element was dropped due the queue being full.
// Elements successfully added to the queue stay in the queue until they
// are popped by a Get() call. In other words, a return value of `true`
// implies that the message will eventually be processed. This provides
// stronger guarantees than minimally required by the [MessageStore] interface.
func (s *FifoMessageStore) Put(msg *Message) bool {
return s.Push(msg)
}
// Get retrieves the next message from the head of the queue. It returns
// true if a message is retrieved, and false if the message store is empty.
func (s *FifoMessageStore) Get() (*Message, bool) {
msgint, ok := s.Pop()
if !ok {
return nil, false
}
return msgint.(*Message), true
}