Skip to content

Commit 5a76cbd

Browse files
committed
add better memory usage calculation
1 parent ade3852 commit 5a76cbd

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

pkg/github/actions.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,29 +758,34 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
758758
tailLines = 1000
759759
}
760760

761-
const maxMemoryBytes = 1024 * 1024 // 1MB memory limit
761+
const maxMemoryBytes = 1024 * 1024
762762
var lines []string
763-
var currentMemoryUsage int
764763
totalLines := 0
765764

765+
calculateMemoryUsage := func() int {
766+
total := 0
767+
for _, line := range lines {
768+
total += len(line) + 1
769+
}
770+
return total
771+
}
772+
766773
scanner := bufio.NewScanner(httpResp.Body)
767774
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
768775

769776
for scanner.Scan() {
770777
line := scanner.Text()
771778
totalLines++
772-
lineSize := len(line) + 1 // +1 for newline character
779+
lineSize := len(line) + 1
773780

781+
currentMemoryUsage := calculateMemoryUsage()
774782
if currentMemoryUsage+lineSize > maxMemoryBytes {
775-
for currentMemoryUsage+lineSize > maxMemoryBytes && len(lines) > 0 {
776-
removedLineSize := len(lines[0]) + 1
777-
currentMemoryUsage -= removedLineSize
783+
for calculateMemoryUsage()+lineSize > maxMemoryBytes && len(lines) > 0 {
778784
lines = lines[1:]
779785
}
780786
}
781787

782788
lines = append(lines, line)
783-
currentMemoryUsage += lineSize
784789
}
785790

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

0 commit comments

Comments
 (0)