Skip to content

Commit

Permalink
flush only once if sending takes >=100ms
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Feb 8, 2025
1 parent 965fe98 commit e7f54f8
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions front/text/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func LineBuffered(inner io.Writer) *LineWriter {
t := time.NewTicker(flushInterval)
defer t.Stop()

drain := func() {
for {
select {
case <-t.C:
default:
return
}
}
}

loop:
for {
select {
Expand All @@ -73,6 +83,7 @@ func LineBuffered(inner io.Writer) *LineWriter {
if i == -1 {
w.buffer.Write(buf)
t.Stop()
drain()
break
}

Expand All @@ -81,15 +92,16 @@ func LineBuffered(inner io.Writer) *LineWriter {

// flush if we have $bufferSize lines in the buffer
if lines == bufferSize {
t.Stop()
drain()

if _, err := w.inner.Write(w.buffer.Bytes()); err != nil {
w.stop(err)
return
}

w.buffer.Reset()
lines = 0

t.Stop()
} else if lines > 0 {
t.Reset(flushInterval)
}
Expand All @@ -105,15 +117,16 @@ func LineBuffered(inner io.Writer) *LineWriter {
continue
}

t.Stop()
drain()

if _, err := w.inner.Write(buf); err != nil {
w.stop(err)
return
}

w.buffer.Reset()
lines = 0

t.Stop()
}
}

Expand Down

0 comments on commit e7f54f8

Please sign in to comment.