Skip to content

Commit 52e531e

Browse files
committed
add incremental memory tracking
1 parent 8002fbd commit 52e531e

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

pkg/github/actions.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -761,14 +761,7 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
761761
const maxMemoryBytes = 5 * 1024 * 1024
762762
var lines []string
763763
totalLines := 0
764-
765-
calculateMemoryUsage := func() int {
766-
total := 0
767-
for _, line := range lines {
768-
total += len(line) + 1
769-
}
770-
return total
771-
}
764+
currentMemoryUsage := 0
772765

773766
scanner := bufio.NewScanner(httpResp.Body)
774767
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
@@ -778,14 +771,16 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
778771
totalLines++
779772
lineSize := len(line) + 1
780773

781-
currentMemoryUsage := calculateMemoryUsage()
782-
if currentMemoryUsage+lineSize > maxMemoryBytes {
783-
for calculateMemoryUsage()+lineSize > maxMemoryBytes && len(lines) > 0 {
784-
lines = lines[1:]
785-
}
774+
// Remove lines from the front until we have space for the new line
775+
for currentMemoryUsage+lineSize > maxMemoryBytes && len(lines) > 0 {
776+
removedLineSize := len(lines[0]) + 1
777+
currentMemoryUsage -= removedLineSize
778+
lines = lines[1:]
786779
}
787780

781+
// Add the new line
788782
lines = append(lines, line)
783+
currentMemoryUsage += lineSize
789784
}
790785

791786
if err := scanner.Err(); err != nil {

0 commit comments

Comments
 (0)