Skip to content

Commit e65c0f0

Browse files
committed
only use up to 1mb of memory for logs
1 parent 271c7c2 commit e65c0f0

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

pkg/github/actions.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ func getJobLogData(ctx context.Context, client *github.Client, owner, repo strin
744744

745745
// downloadLogContent downloads the actual log content from a GitHub logs URL
746746
func downloadLogContent(logURL string, tailLines int) (string, int, *http.Response, error) {
747-
httpResp, err := http.Get(logURL) //nolint:gosec // URLs are provided by GitHub API and are safe
747+
httpResp, err := http.Get(logURL) //nolint:gosec
748748
if err != nil {
749749
return "", 0, httpResp, fmt.Errorf("failed to download logs: %w", err)
750750
}
@@ -758,27 +758,35 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
758758
tailLines = 1000
759759
}
760760

761-
lines := make([]string, 0, tailLines)
762-
scanner := bufio.NewScanner(httpResp.Body)
761+
const maxMemoryBytes = 1024 * 1024
762+
var lines []string
763+
var currentMemoryUsage int
764+
totalLines := 0
763765

764-
buf := make([]byte, 0, 64*1024)
765-
scanner.Buffer(buf, 1024*1024)
766+
scanner := bufio.NewScanner(httpResp.Body)
767+
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
766768

767769
for scanner.Scan() {
768770
line := scanner.Text()
769-
lines = append(lines, line)
771+
totalLines++
772+
lineSize := len(line) + 1
770773

771-
if len(lines) > tailLines {
774+
// Remove old lines if we exceed memory limit or line count limit
775+
for (currentMemoryUsage+lineSize > maxMemoryBytes || len(lines) >= tailLines) && len(lines) > 0 {
776+
removedLineSize := len(lines[0]) + 1
777+
currentMemoryUsage -= removedLineSize
772778
lines = lines[1:]
773779
}
780+
781+
lines = append(lines, line)
782+
currentMemoryUsage += lineSize
774783
}
775784

776785
if err := scanner.Err(); err != nil {
777786
return "", 0, httpResp, fmt.Errorf("failed to read log content: %w", err)
778787
}
779788

780-
content := strings.Join(lines, "\n")
781-
return content, len(lines), httpResp, nil
789+
return strings.Join(lines, "\n"), totalLines, httpResp, nil
782790
}
783791

784792
// RerunWorkflowRun creates a tool to re-run an entire workflow run

0 commit comments

Comments
 (0)