forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiQueue.go
49 lines (41 loc) · 1.2 KB
/
apiQueue.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
package state
import (
"github.com/FactomProject/factomd/common/interfaces"
)
// APIMSGQueue counts incoming and outgoing messages for API queue
type APIMSGQueue chan interfaces.IMsg
func NewAPIQueue(capacity int) APIMSGQueue {
channel := make(chan interfaces.IMsg, capacity)
return channel
}
// Length of underlying channel
func (q APIMSGQueue) Length() int {
return len(chan interfaces.IMsg(q))
}
// Cap of underlying channel
func (q APIMSGQueue) Cap() int {
return cap(chan interfaces.IMsg(q))
}
// Enqueue adds item to channel and instruments based on type
func (q APIMSGQueue) Enqueue(m interfaces.IMsg) {
measureMessage(TotalMessageQueueApiGeneralVec, m, true)
measureMessage(CurrentMessageQueueApiGeneralVec, m, true)
q <- m
}
// Dequeue removes an item from channel and instruments based on type. Returns nil if nothing in
// queue
func (q APIMSGQueue) Dequeue() interfaces.IMsg {
select {
case v := <-q:
measureMessage(CurrentMessageQueueApiGeneralVec, v, false)
return v
default:
return nil
}
}
// BlockingDequeue will block until it retrieves from queue
func (q APIMSGQueue) BlockingDequeue() interfaces.IMsg {
v := <-q
measureMessage(CurrentMessageQueueApiGeneralVec, v, false)
return v
}