Return from Noop lex routine

In the Noop lexer we start a routine that's responsible for output
of the written frames at a consistent rate (to make certain
destinations happy i.e. youtube). We weren't however returning
from this in the case that the lexing function is returned, we'd
therefore then be left with a hanging routine (this would
eventually keep happening until stack overflow if had long enough
runtime for the service; not a big deal for systemd services, but
still crappy). So, now we create a context which is cancelled when
when the lex function returns and can detect this in the output
routine.

Approved-by: Alan Noble
This commit is contained in:
Saxon Milton 2024-04-07 11:47:31 +00:00
parent d4af5c0194
commit d519f50fe5
1 changed files with 9 additions and 1 deletions

View File

@ -30,6 +30,7 @@ import (
"io" "io"
"math" "math"
"time" "time"
"context"
) )
// ByteLexer is used to lex bytes using a buffer size which is configured upon construction. // ByteLexer is used to lex bytes using a buffer size which is configured upon construction.
@ -127,13 +128,20 @@ func Noop(dst io.Writer, src io.Reader, d time.Duration) error {
defer delay.Stop() defer delay.Stop()
// This routine is responsible for frame output to rest of the pipeline. // This routine is responsible for frame output to rest of the pipeline.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() { go func() {
for { for {
err := rb.writeTo(dst) err := rb.writeTo(dst)
if err != nil && !errors.Is(err, errBuffChanReceiveTimeout) { if err != nil && !errors.Is(err, errBuffChanReceiveTimeout) {
errCh <- fmt.Errorf("could not write to dst: %w", err) errCh <- fmt.Errorf("could not write to dst: %w", err)
} }
<-delay.C
select {
case <-delay.C:
case <-ctx.Done():
return
}
// Adjust delay using proportional controller. // Adjust delay using proportional controller.
adj := coef * float64(target-rb.len()) adj := coef * float64(target-rb.len())