diff --git a/codec/codecutil/lex.go b/codec/codecutil/lex.go index 3e1d862d..98953a93 100644 --- a/codec/codecutil/lex.go +++ b/codec/codecutil/lex.go @@ -52,6 +52,8 @@ func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, t time.Duration) error { if t < 0 { return fmt.Errorf("invalid delay: %v", t) } + + // Set up delay, make loop instant if t is 0. var ticker *time.Ticker var zeroTicks chan time.Time if t > 0 { @@ -76,28 +78,3 @@ func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, t time.Duration) error { } } } - -func main() { - for _, delay := range []time.Duration{0, 10 * time.Millisecond} { - t := newTicker(delay) - for i := 0; i < 10; i++ { - <-t.C - fmt.Println(time.Now(), i) - } - fmt.Println() - } -} - -var zeroTicks chan time.Time - -func init() { - zeroTicks = make(chan time.Time) - close(zeroTicks) -} - -func newTicker(d time.Duration) *time.Ticker { - if d == 0 { - return &time.Ticker{C: zeroTicks} - } - return time.NewTicker(d) -}