diff --git a/codec/codecutil/lex.go b/codec/codecutil/lex.go index 158839dd..e1498b96 100644 --- a/codec/codecutil/lex.go +++ b/codec/codecutil/lex.go @@ -48,13 +48,6 @@ func init() { close(zeroTicks) } -func newTicker(d time.Duration) *time.Ticker { - if d == 0 { - return &time.Ticker{C: zeroTicks} - } - return time.NewTicker(d) -} - // Lex reads *l.bufSize bytes from src and writes them to dst every d seconds. func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, d time.Duration) error { if l.bufSize == nil { @@ -68,7 +61,14 @@ func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, d time.Duration) error { return fmt.Errorf("invalid delay: %v", d) } - ticker := newTicker(d) + var ticker *time.Ticker + if d == 0 { + ticker = &time.Ticker{C: zeroTicks} + } else { + ticker = time.NewTicker(d) + defer ticker.Stop() + } + buf := make([]byte, bufSize) for { <-ticker.C