Server Event Optimizations (#116)

This commit is contained in:
Matthew Penner
2022-01-17 20:23:29 -07:00
committed by GitHub
parent 521cc2aef2
commit 649dc9663e
18 changed files with 551 additions and 282 deletions

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"strconv"
"strings"
"sync"
"time"
@@ -38,14 +37,14 @@ func MustInt(v string) int {
return i
}
func ScanReader(r io.Reader, callback func(line string)) error {
func ScanReader(r io.Reader, callback func(line []byte)) error {
br := bufio.NewReader(r)
// Avoid constantly re-allocating memory when we're flooding lines through this
// function by using the same buffer for the duration of the call and just truncating
// the value back to 0 every loop.
var str strings.Builder
buf := &bytes.Buffer{}
for {
str.Reset()
buf.Reset()
var err error
var line []byte
var isPrefix bool
@@ -57,7 +56,7 @@ func ScanReader(r io.Reader, callback func(line string)) error {
// in line with that it thinks is the terminal size. Those returns break a lot of output handling,
// so we'll just replace them with proper new-lines and then split it later and send each line as
// its own event in the response.
str.Write(bytes.Replace(line, cr, crr, -1))
buf.Write(bytes.Replace(line, cr, crr, -1))
// Finish this loop and begin outputting the line if there is no prefix (the line fit into
// the default buffer), or if we hit the end of the line.
if !isPrefix || err == io.EOF {
@@ -71,8 +70,9 @@ func ScanReader(r io.Reader, callback func(line string)) error {
}
// Publish the line for this loop. Break on new-line characters so every line is sent as a single
// output event, otherwise you get funky handling in the browser console.
for _, line := range strings.Split(str.String(), "\r\n") {
callback(line)
s := bufio.NewScanner(buf)
for s.Scan() {
callback(s.Bytes())
}
// If the error we got previously that lead to the line being output is an io.EOF we want to
// exit the entire looping process.