Skip to content

Commit

Permalink
logs: slow down truncation rate (#2221)
Browse files Browse the repository at this point in the history
  • Loading branch information
landism committed Sep 20, 2019
1 parent 1df6494 commit d26d807
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions pkg/model/log.go
Expand Up @@ -12,6 +12,12 @@ import (
// A render time of ~40ms was about when the interface started being noticeably laggy to me.
const maxLogLengthInBytes = 120 * 1000

// After a log hits its limit, we need to truncate it to keep it small
// we do this by cutting a big chunk at a time, so that we have rarer, larger changes, instead of
// a small change every time new data is written to the log
// https://github.com/windmilleng/tilt/issues/1935#issuecomment-531390353
const logTruncationTarget = maxLogLengthInBytes / 2

const newlineByte = byte('\n')

// All LogLines should end in a \n to be considered "complete".
Expand Down Expand Up @@ -143,13 +149,17 @@ type LogEvent interface {
}

func ensureMaxLength(lines []logLine) []logLine {
bytesLeft := maxLogLengthInBytes
bytesSpent := 0
truncationIndex := -1
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
if line.Len() > bytesLeft {
return lines[i+1:]
bytesSpent += line.Len()
if truncationIndex == -1 && bytesSpent > logTruncationTarget {
truncationIndex = i + 1
}
if bytesSpent > maxLogLengthInBytes {
return lines[truncationIndex:]
}
bytesLeft -= line.Len()
}

return lines
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/log_test.go
Expand Up @@ -41,7 +41,7 @@ func TestLog_AppendOverLimit(t *testing.T) {

l = AppendLog(l, logEvent{time.Time{}, s}, false, "")

assert.Equal(t, s, l.String())
assert.Equal(t, s[:logTruncationTarget], l.String())
}

func TestLog_Timestamps(t *testing.T) {
Expand Down

0 comments on commit d26d807

Please sign in to comment.