Commit c20ae3c8 authored by Jordan Sissel's avatar Jordan Sissel

- use 'next_flush_time' as a time.Time instead of computing

  duration-since-last-flush-or-start; I think it makes the code more
  readable to say "is the current time after next_flush_time?"
parent 9a16b2a5
......@@ -12,13 +12,17 @@ func Spooler(input chan *FileEvent,
// 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)
var spool_i int = 0
timeout := 1 * time.Second
start := time.Now()
// Current write position in the spool
var spool_i int = 0
next_flush_time := time.Now().Add(idle_timeout)
for {
select {
case event := <- input:
......@@ -28,21 +32,23 @@ func Spooler(input chan *FileEvent,
// Flush if full
if spool_i == len(spool) {
output <- &EventEnvelope{Events: spool[:]}
start = time.Now() // reset 'start' time
next_flush_time = time.Now().Add(idle_timeout)
spool_i = 0
}
case <- ticker.C:
if duration := time.Since(start); duration > timeout {
/* Timeout occurred */
fmt.Printf("timeout: %d > %d\n", time.Since(start), timeout)
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 {
start = time.Now()
next_flush_time = now.Add(idle_timeout)
output <- &EventEnvelope{Events: spool[0:spool_i]}
spool_i = 0
}
} /* if duration > timeout */
} /* if 'now' is after 'next_flush_time' */
/* case ... */
} /* select */
} /* for */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment