-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
queue.go
119 lines (94 loc) · 2.77 KB
/
queue.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package containerlifecycle
import (
"errors"
"sync"
model "github.com/DataDog/agent-payload/v5/contlcycle"
)
type queue struct {
chunkSize int
data []*model.EventsPayload
sync.RWMutex
}
// newQueue returns a new *queue.
func newQueue(chunkSize int) *queue {
return &queue{
chunkSize: chunkSize,
data: []*model.EventsPayload{},
}
}
// flush returns and resets the queue content. Returns nil if the queue is empty.
// flush is thread-safe.
func (q *queue) flush() []*model.EventsPayload {
q.Lock()
defer q.Unlock()
if q.isEmpty() {
return nil
}
data := q.data
// Reset the data in the queue.
q.data = []*model.EventsPayload{}
return data
}
// add enqueues a new event.
// add is thread-safe.
func (q *queue) add(ev event) error {
q.Lock()
defer q.Unlock()
if q.isEmpty() || q.isLastPayloadFull() {
return q.addPayload(ev)
}
return q.addEvent(ev)
}
// addPayload enqueues the event in a new payload entry in the queue.
// To be used if the queue is empty or if the last payload entry in the queue is full.
// addPayload is not thread-safe, the caller must lock the queue.
func (q *queue) addPayload(ev event) error {
payload, err := ev.toPayloadModel()
if err != nil {
return err
}
q.data = append(q.data, payload)
return nil
}
// addEvent enqueues the event in last payload entry.
// To be used if the queue is not empty and the last payload entry is not full.
// addEvent is not thread-safe, the caller must lock the queue.
func (q *queue) addEvent(ev event) error {
if q.isEmpty() {
return errors.New("cannot add event to an empty queue")
}
event, err := ev.toEventModel()
if err != nil {
return err
}
lenQueue := len(q.data)
q.data[lenQueue-1].Events = append(q.data[lenQueue-1].Events, event)
return nil
}
// isEmpty returns whether the queue is empty.
// isEmpty is not thread-safe, the caller must lock the queue.
func (q *queue) isEmpty() bool {
return len(q.data) == 0
}
// lastPayload returns the last payload entry in the queue.
// lastPayload is not thread-safe, the caller must lock the queue.
func (q *queue) lastPayload() (*model.EventsPayload, error) {
if q.isEmpty() {
return nil, errors.New("empty queue")
}
return q.data[len(q.data)-1], nil
}
// isLastPayloadFull returns whether the last payload entry
// is full compared to the configured chunk size.
// isLastPayloadFull is not thread-safe, the caller must lock the queue.
func (q *queue) isLastPayloadFull() bool {
lastElem, err := q.lastPayload()
if err != nil {
return false
}
return len(lastElem.Events) >= q.chunkSize
}