From e2872e786e08d0c8710f276e6051961945054534 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 12 Nov 2020 21:41:35 -0800 Subject: [PATCH] Tiny optimization for handling string output from server console --- environment/docker/container.go | 17 ++++++++++++++--- environment/docker/power.go | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/environment/docker/container.go b/environment/docker/container.go index f789b7b..ce376bb 100644 --- a/environment/docker/container.go +++ b/environment/docker/container.go @@ -296,9 +296,20 @@ func (e *Environment) followOutput() error { defer reader.Close() 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: for { - var b bytes.Buffer + str.Reset() var line []byte 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, // 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. - 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 // 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 // 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) } diff --git a/environment/docker/power.go b/environment/docker/power.go index 3a8de59..a327425 100644 --- a/environment/docker/power.go +++ b/environment/docker/power.go @@ -183,7 +183,7 @@ func (e *Environment) WaitForStop(seconds uint, terminate bool) error { case <-ctx.Done(): if ctxErr := ctx.Err(); ctxErr != nil { 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)) }