-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathheroStore.go
More file actions
58 lines (46 loc) · 1.59 KB
/
heroStore.go
File metadata and controls
58 lines (46 loc) · 1.59 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package queue
import (
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/module"
)
var defaultMsgEntityFactoryFunc = NewMessageEntity
type HeroStoreOption func(heroStore *HeroStore)
func WithMessageEntityFactory(f func(message *engine.Message) MessageEntity) HeroStoreOption {
return func(heroStore *HeroStore) {
heroStore.msgEntityFactory = f
}
}
// HeroStore is a FIFO (first-in-first-out) size-bound queue for maintaining engine.Message types.
// It is based on HeroQueue.
type HeroStore struct {
q *HeroQueue
msgEntityFactory func(message *engine.Message) MessageEntity
}
func NewHeroStore(sizeLimit uint32, logger zerolog.Logger, collector module.HeroCacheMetrics, opts ...HeroStoreOption) *HeroStore {
h := &HeroStore{
q: NewHeroQueue(sizeLimit, logger, collector),
msgEntityFactory: defaultMsgEntityFactoryFunc,
}
for _, opt := range opts {
opt(h)
}
return h
}
// Put enqueues the message into the message store.
//
// Boolean returned variable determines whether enqueuing was successful, i.e.,
// put may be dropped if queue is full or already exists.
func (c *HeroStore) Put(message *engine.Message) bool {
return c.q.Push(c.msgEntityFactory(message))
}
// Get pops the queue, i.e., it returns the head of queue, and updates the head to the next element.
// Boolean return value determines whether pop is successful, i.e., popping an empty queue returns false.
func (c *HeroStore) Get() (*engine.Message, bool) {
head, ok := c.q.Pop()
if !ok {
return nil, false
}
msg := head.(MessageEntity).Msg
return &msg, true
}