-
Notifications
You must be signed in to change notification settings - Fork 179
/
queues.go
73 lines (60 loc) · 1.57 KB
/
queues.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package stdmap
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool"
"github.com/onflow/flow-go/module/mempool/queue"
_ "github.com/onflow/flow-go/utils/binstat"
)
type Queues struct {
*Backend
}
// QueuesBackdata is mempool map for ingestion.Queues (head Node ID -> Queues)
type QueuesBackdata struct {
mempool.BackData
}
func NewQueues() *Queues {
return &Queues{NewBackend(WithEject(EjectPanic))}
}
func (b *QueuesBackdata) ByID(queueID flow.Identifier) (*queue.Queue, bool) {
entity, exists := b.BackData.ByID(queueID)
if !exists {
return nil, false
}
queue := entity.(*queue.Queue)
return queue, true
}
func (b *QueuesBackdata) All() []*queue.Queue {
entities := b.BackData.All()
queues := make([]*queue.Queue, len(entities))
i := 0
for _, entity := range entities {
queue, ok := entity.(*queue.Queue)
if !ok {
panic(fmt.Sprintf("invalid entity in queue mempool (%T)", entity))
}
queues[i] = queue
i++
}
return queues
}
func (b *Queues) Add(queue *queue.Queue) bool {
return b.Backend.Add(queue)
}
func (b *Queues) Get(queueID flow.Identifier) (*queue.Queue, bool) {
backdata := &QueuesBackdata{b.backData}
return backdata.ByID(queueID)
}
func (b *Queues) Run(f func(backdata *QueuesBackdata) error) error {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(Queues)Run")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Queues)Run")
defer b.Unlock()
err := f(&QueuesBackdata{b.backData})
//binstat.Leave(bs2)
if err != nil {
return err
}
return nil
}