/
logstorage.go
198 lines (154 loc) · 3.63 KB
/
logstorage.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package logstorage
import (
"context"
"fmt"
"math/rand"
"sort"
"sync"
"time"
)
type Store struct {
lock sync.Mutex
streams map[string][]Log
subscriptions subscriptions
}
type Log struct {
Prefix string
Message string
Timestamp time.Time
}
type Receiver chan Log
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func New() Store {
s := Store{streams: map[string][]Log{}}
go s.startCleaner()
return s
}
func (s *Store) Append(stream string, ts time.Time, prefix, message string) {
s.lock.Lock()
defer s.lock.Unlock()
log := Log{Message: message, Prefix: prefix, Timestamp: ts}
ls, ok := s.streams[stream]
if !ok {
ls = []Log{}
}
n := sort.Search(len(ls), func(i int) bool { return ls[i].Timestamp.After(ts) })
ls = append(ls, Log{})
copy(ls[n+1:], ls[n:])
ls[n] = log
s.streams[stream] = ls
s.subscriptions.send(stream, log)
}
func (s *Store) Subscribe(ctx context.Context, ch Receiver, stream string, start time.Time, follow bool) {
s.lock.Lock()
defer s.lock.Unlock()
if ls, ok := s.streams[stream]; ok {
n := sort.Search(len(ls), func(i int) bool { return !ls[i].Timestamp.Before(start) })
go sendMultiple(ch, ls[n:], func() {
if !follow {
close(ch)
}
})
}
if follow {
s.subscriptions.Subscribe(ctx, ch, stream, start)
}
}
func (s *Store) cleanupLogs() {
s.lock.Lock()
defer s.lock.Unlock()
for name := range s.streams {
ls := s.streams[name]
n := sort.Search(len(ls), func(i int) bool { return ls[i].Timestamp.After(time.Now().Add(30 * time.Second)) })
s.streams[name] = ls[n:]
}
}
func (s *Store) startCleaner() {
for range time.Tick(30 * time.Second) {
s.cleanupLogs()
}
}
type subscriptions struct {
lock sync.Mutex
subscriptions map[string]map[string]*subscription
}
type subscription struct {
ch Receiver
lock sync.Mutex
queue []Log
start time.Time
}
func (s *subscriptions) Subscribe(ctx context.Context, ch Receiver, stream string, start time.Time) {
s.add(ctx, ch, stream, start)
}
func (s *subscriptions) add(ctx context.Context, ch Receiver, stream string, start time.Time) {
s.lock.Lock()
defer s.lock.Unlock()
if s.subscriptions == nil {
s.subscriptions = map[string]map[string]*subscription{}
}
if _, ok := s.subscriptions[stream]; !ok {
s.subscriptions[stream] = map[string]*subscription{}
}
handle := fmt.Sprintf("%v:%d", ch, rand.Int63())
s.subscriptions[stream][handle] = &subscription{ch: ch, start: start}
go s.watch(ctx, stream, handle)
}
func (s *subscriptions) remove(stream, handle string) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.subscriptions[stream]; !ok {
return
}
delete(s.subscriptions[stream], handle)
}
func (s *subscriptions) send(stream string, l Log) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.subscriptions[stream]; !ok {
return
}
for _, sub := range s.subscriptions[stream] {
if !sub.start.After(l.Timestamp) {
sub.add(l)
}
}
}
func (s *subscriptions) watch(ctx context.Context, stream, handle string) {
defer s.remove(stream, handle)
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
select {
case <-ctx.Done():
return
case <-tick.C:
if ss, ok := s.subscriptions[stream]; ok {
if sub, ok := ss[handle]; ok && len(sub.queue) > 0 {
sub.flush()
}
}
}
}
}
func (s *subscription) add(l Log) {
s.lock.Lock()
defer s.lock.Unlock()
s.queue = append(s.queue, l)
}
func (s *subscription) flush() {
s.lock.Lock()
defer s.lock.Unlock()
for _, l := range s.queue {
s.ch <- l
}
s.queue = s.queue[:0]
}
func sendMultiple(ch Receiver, ls []Log, done func()) {
defer done()
for _, l := range ls {
ch <- l
}
}