@@ -14,7 +14,9 @@ import (
1414// be placed on the channel:
1515//
1616// input.read => number of messages processed during a Read call
17- // input.drop => number of messages dropped from queue during a Read call
17+ // input.queue => number of messages queued during a Read call
18+ // input.drop => number of new messages dropped during a Read call
19+ // input.evict => number of old messages evicted from the queue during a Read call
1820// input.read.duration => time taken to process a line from reader (micros)
1921type Input struct {
2022 bufferSize int
@@ -40,17 +42,20 @@ func (input *Input) Read() *sync.WaitGroup {
4042 return wg
4143}
4244
45+ func (input * Input ) record (name string , val float64 ) {
46+ if input .statsChannel != nil {
47+ input .statsChannel <- Stat {name : name , value : val }
48+ }
49+ }
50+
4351// Private synchronous portion of Read.
4452func (input * Input ) read () {
4553 reader := bufio .NewReaderSize (input .reader , input .bufferSize )
4654
4755 for {
4856 line , _ , err := reader .ReadLine ()
4957
50- var start time.Time
51- if input .statsChannel != nil {
52- start = time .Now ()
53- }
58+ start := time .Now ()
5459
5560 if err != nil {
5661 break
@@ -64,24 +69,39 @@ func (input *Input) read() {
6469
6570 copy (cp , line )
6671
67- if input .statsChannel != nil {
68- input .statsChannel <- Stat {name : "input.read" , value : 1.0 }
69- }
72+ input .record ("input.read" , 1.0 )
7073
7174 select {
7275 case input .queue <- cp :
7376 // queued
77+ input .record ("input.queue" , 1.0 )
7478 default :
75- // evict the oldest entry to make room
76- <- input .queue
77- if input .statsChannel != nil {
78- input .statsChannel <- Stat {name : "input.drop" , value : 1.0 }
79+ // try to evict the oldest entry to make room
80+ select {
81+ case <- input .queue :
82+ input .record ("input.evict" , 1.0 )
83+ // try again to queue
84+ select {
85+ case input .queue <- cp :
86+ // queued
87+ input .record ("input.queue" , 1.0 )
88+ default :
89+ // no room, drop it
90+ input .record ("input.drop" , 1.0 )
91+ }
92+ default :
93+ // queue is already empty, try to queue
94+ select {
95+ case input .queue <- cp :
96+ // queued
97+ input .record ("input.queue" , 1.0 )
98+ default :
99+ // no room, drop it
100+ input .record ("input.drop" , 1.0 )
101+ }
79102 }
80- input .queue <- cp
81103 }
82104
83- if input .statsChannel != nil {
84- input .statsChannel <- Stat {name : "input.read.duration" , value : float64 (time .Now ().Sub (start ).Nanoseconds ()) / float64 (1000 )}
85- }
105+ input .record ("input.read.duration" , float64 (time .Now ().Sub (start ).Nanoseconds ())/ float64 (1000 ))
86106 }
87107}
0 commit comments