-
Notifications
You must be signed in to change notification settings - Fork 0
/
delivery_stream.go
104 lines (89 loc) · 1.94 KB
/
delivery_stream.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
package toyhose
import (
"context"
"sort"
"sync"
"time"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/google/uuid"
)
type deliveryStream struct {
arn string
deliveryStreamName string
deliveryStreamType string
recordCh chan *deliveryRecord
closer context.CancelFunc
destDesc *firehose.DestinationDescription
sourceDesc *firehose.SourceDescription
createdAt time.Time
}
func (d *deliveryStream) Close() {
d.closer()
close(d.recordCh)
}
type deliveryRecord struct {
id string
data []byte
}
func newDeliveryRecord(data []byte) *deliveryRecord {
return &deliveryRecord{
id: uuid.New().String(),
data: data,
}
}
type deliveryStreamPool struct {
mutex sync.RWMutex
pool map[string]*deliveryStream
}
func (p *deliveryStreamPool) Add(d *deliveryStream) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.pool[d.arn] = d
}
func (p *deliveryStreamPool) Find(arn string) *deliveryStream {
p.mutex.RLock()
defer p.mutex.RUnlock()
if ds, ok := p.pool[arn]; ok {
return ds
}
return nil
}
func (p *deliveryStreamPool) Delete(arn string) *deliveryStream {
p.mutex.Lock()
defer p.mutex.Unlock()
if ds, ok := p.pool[arn]; ok {
delete(p.pool, arn)
return ds
}
return nil
}
func (p *deliveryStreamPool) FindAllBySource(streamType string, from *string, limit *int64) ([]*deliveryStream, bool) {
var pickups []*deliveryStream
for _, ds := range p.pool {
if ds.deliveryStreamType != streamType {
continue
}
pickups = append(pickups, ds)
}
sort.Slice(pickups, func(i, j int) bool {
return pickups[i].createdAt.Before(pickups[j].createdAt)
})
if from != nil {
idx := 0
for i, v := range pickups {
if v.deliveryStreamName == *from {
idx = i + 1
break
}
}
pickups = pickups[idx:]
}
hasNext := false
if limit != nil {
if lim := int(*limit); len(pickups) > lim {
hasNext = true
pickups = pickups[:lim]
}
}
return pickups, hasNext
}