Helper function to stream output with memory limits
(tempFile *os.File, lastPos *int64, totalSent *int64, maxOutputSize int64, maxChunkSize int64, w http.ResponseWriter)
| 2040 | |
| 2041 | // Helper function to stream output with memory limits |
| 2042 | func streamRemainingOutput(tempFile *os.File, lastPos *int64, totalSent *int64, maxOutputSize int64, maxChunkSize int64, w http.ResponseWriter) { |
| 2043 | if *totalSent >= maxOutputSize { |
| 2044 | return // Stop streaming if we've hit the limit |
| 2045 | } |
| 2046 | |
| 2047 | fileInfo, err := tempFile.Stat() |
| 2048 | if err != nil { |
| 2049 | return |
| 2050 | } |
| 2051 | |
| 2052 | if fileInfo.Size() > *lastPos { |
| 2053 | remainingAllowed := maxOutputSize - *totalSent |
| 2054 | toRead := fileInfo.Size() - *lastPos |
| 2055 | |
| 2056 | if toRead > remainingAllowed { |
| 2057 | toRead = remainingAllowed |
| 2058 | } |
| 2059 | if toRead > maxChunkSize { |
| 2060 | toRead = maxChunkSize |
| 2061 | } |
| 2062 | |
| 2063 | // Read only the new portion in a small chunk |
| 2064 | buffer := make([]byte, toRead) |
| 2065 | tempFile.Seek(*lastPos, 0) |
| 2066 | n, err := tempFile.Read(buffer) |
| 2067 | if err != nil && err != io.EOF { |
| 2068 | return |
| 2069 | } |
| 2070 | |
| 2071 | if n > 0 { |
| 2072 | // Truncate to actual bytes read |
| 2073 | newContent := string(buffer[:n]) |
| 2074 | outputData, _ := json.Marshal(map[string]string{"output": newContent}) |
| 2075 | fmt.Fprintf(w, "data: %s\n\n", outputData) |
| 2076 | w.(http.Flusher).Flush() |
| 2077 | *lastPos += int64(n) |
| 2078 | *totalSent += int64(n) |
| 2079 | } |
| 2080 | |
| 2081 | if *totalSent >= maxOutputSize { |
| 2082 | // Send truncation warning |
| 2083 | warningData, _ := json.Marshal(map[string]string{ |
| 2084 | "output": "\n[OUTPUT TRUNCATED - Limit of 10MB reached]\n", |
| 2085 | }) |
| 2086 | fmt.Fprintf(w, "data: %s\n\n", warningData) |
| 2087 | w.(http.Flusher).Flush() |
| 2088 | } |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | // handleKillInstances handles POST requests to kill processes |
| 2093 | func handleKillInstances(w http.ResponseWriter, r *http.Request) { |