Skip to content

Commit

Permalink
Merge pull request agola-io#180 from sgotti/api_write_flush_logs_headers
Browse files Browse the repository at this point in the history
*: write and flush header on log handlers
  • Loading branch information
sgotti committed Nov 15, 2019
2 parents 9cacc80 + b01bac1 commit 30feef8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions internal/services/executor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ func (h *logsHandler) readLogs(taskID string, setup bool, step int, logPath stri
w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10))
}

// write and flush the headers so the client will receive the response
// header also if there're currently no lines to send
w.WriteHeader(http.StatusOK)
var flusher http.Flusher
if fl, ok := w.(http.Flusher); ok {
flusher = fl
}
if flusher != nil {
flusher.Flush()
}

stop := false
flushstop := false
for {
Expand Down
10 changes: 10 additions & 0 deletions internal/services/gateway/api/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,18 @@ func (h *LogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// write and flush the headers so the client will receive the response
// header also if there're currently no lines to send
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
var flusher http.Flusher
if fl, ok := w.(http.Flusher); ok {
flusher = fl
}
if flusher != nil {
flusher.Flush()
}

defer resp.Body.Close()
if err := sendLogs(w, resp.Body); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions internal/services/runservice/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,23 @@ func (h *LogsHandler) readTaskLogs(ctx context.Context, runID, taskID string, se
return errors.Errorf("received http status: %d", req.StatusCode), true
}

// write and flush the headers so the client will receive the response
// header also if there're currently no lines to send
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
var flusher http.Flusher
if fl, ok := w.(http.Flusher); ok {
flusher = fl
}
if flusher != nil {
flusher.Flush()
}

return sendLogs(w, req.Body), false
}

func sendLogs(w http.ResponseWriter, r io.Reader) error {
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")

buf := make([]byte, 406)

var flusher http.Flusher
Expand Down

0 comments on commit 30feef8

Please sign in to comment.