forked from elastic/logstash-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spooler.go
62 lines (53 loc) · 1.69 KB
/
spooler.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
package main
import (
"time"
)
func Spool(input chan *FileEvent,
output chan []*FileEvent,
max_size uint64,
idle_timeout time.Duration) {
// heartbeat periodically. If the last flush was longer than
// 'idle_timeout' time ago, then we'll force a flush to prevent us from
// holding on to spooled events for too long.
ticker := time.NewTicker(idle_timeout / 2)
// slice for spooling into
// TODO(sissel): use container.Ring?
spool := make([]*FileEvent, max_size)
// Current write position in the spool
var spool_i int = 0
next_flush_time := time.Now().Add(idle_timeout)
for {
select {
case event := <-input:
//append(spool, event)
spool[spool_i] = event
spool_i++
// Flush if full
if spool_i == cap(spool) {
//spoolcopy := make([]*FileEvent, max_size)
var spoolcopy []*FileEvent
//fmt.Println(spool[0])
spoolcopy = append(spoolcopy, spool[:]...)
output <- spoolcopy
next_flush_time = time.Now().Add(idle_timeout)
spool_i = 0
}
case <-ticker.C:
//fmt.Println("tick")
if now := time.Now(); now.After(next_flush_time) {
// if current time is after the next_flush_time, flush!
//fmt.Printf("timeout: %d exceeded by %d\n", idle_timeout,
//now.Sub(next_flush_time))
// Flush what we have, if anything
if spool_i > 0 {
var spoolcopy []*FileEvent
spoolcopy = append(spoolcopy, spool[0:spool_i]...)
output <- spoolcopy
next_flush_time = now.Add(idle_timeout)
spool_i = 0
}
} /* if 'now' is after 'next_flush_time' */
/* case ... */
} /* select */
} /* for */
} /* spool */