Tiny optimization for handling string output from server console
This commit is contained in:
parent
f81e35d960
commit
e2872e786e
|
@ -296,9 +296,20 @@ func (e *Environment) followOutput() error {
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
|
|
||||||
r := bufio.NewReader(reader)
|
r := bufio.NewReader(reader)
|
||||||
|
|
||||||
|
// Micro-optimization to create these replacements one time when this routine
|
||||||
|
// fires up, rather than on every line that is executed.
|
||||||
|
cr := []byte(" \r")
|
||||||
|
crr := []byte("\r\n")
|
||||||
|
|
||||||
|
|
||||||
|
// 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
|
||||||
ParentLoop:
|
ParentLoop:
|
||||||
for {
|
for {
|
||||||
var b bytes.Buffer
|
str.Reset()
|
||||||
var line []byte
|
var line []byte
|
||||||
var isPrefix bool
|
var isPrefix bool
|
||||||
|
|
||||||
|
@ -310,7 +321,7 @@ func (e *Environment) followOutput() error {
|
||||||
// in line with that it thinks is the terminal size. Those returns break a lot of output handling,
|
// 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
|
// 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.
|
// its own event in the response.
|
||||||
b.Write(bytes.ReplaceAll(line, []byte(" \r"), []byte("\r\n")))
|
str.Write(bytes.Replace(line, cr, crr, -1))
|
||||||
|
|
||||||
// Finish this loop and begin outputting the line if there is no prefix (the line fit into
|
// 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.
|
// the default buffer), or if we hit the end of the line.
|
||||||
|
@ -327,7 +338,7 @@ func (e *Environment) followOutput() error {
|
||||||
|
|
||||||
// Publish the line for this loop. Break on new-line characters so every line is sent as a single
|
// 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.
|
// output event, otherwise you get funky handling in the browser console.
|
||||||
for _, line := range strings.Split(b.String(), "\r\n") {
|
for _, line := range strings.Split(str.String(), "\r\n") {
|
||||||
e.Events().Publish(environment.ConsoleOutputEvent, line)
|
e.Events().Publish(environment.ConsoleOutputEvent, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -183,7 +183,7 @@ func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||||
if terminate {
|
if terminate {
|
||||||
log.WithField("container_id", e.Id).Debug("server did not stop in time, executing process termination")
|
log.WithField("container_id", e.Id).Info("server did not stop in time, executing process termination")
|
||||||
|
|
||||||
return errors.WithStackIf(e.Terminate(os.Kill))
|
return errors.WithStackIf(e.Terminate(os.Kill))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user