@@ -758,7 +758,7 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
758758 tailLines = 1000
759759 }
760760
761- const maxMemoryBytes = 1024 * 1024
761+ const maxMemoryBytes = 1024 * 1024 // 1MB memory limit
762762 var lines []string
763763 var currentMemoryUsage int
764764 totalLines := 0
@@ -769,13 +769,14 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
769769 for scanner .Scan () {
770770 line := scanner .Text ()
771771 totalLines ++
772- lineSize := len (line ) + 1
772+ lineSize := len (line ) + 1 // +1 for newline character
773773
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
778- lines = lines [1 :]
774+ if currentMemoryUsage + lineSize > maxMemoryBytes {
775+ for currentMemoryUsage + lineSize > maxMemoryBytes && len (lines ) > 0 {
776+ removedLineSize := len (lines [0 ]) + 1
777+ currentMemoryUsage -= removedLineSize
778+ lines = lines [1 :]
779+ }
779780 }
780781
781782 lines = append (lines , line )
@@ -786,6 +787,10 @@ func downloadLogContent(logURL string, tailLines int) (string, int, *http.Respon
786787 return "" , 0 , httpResp , fmt .Errorf ("failed to read log content: %w" , err )
787788 }
788789
790+ if len (lines ) > tailLines {
791+ lines = lines [len (lines )- tailLines :]
792+ }
793+
789794 return strings .Join (lines , "\n " ), totalLines , httpResp , nil
790795}
791796
0 commit comments