From e85455880a3688bc33ef756550f9cf2755df4c9b Mon Sep 17 00:00:00 2001 From: Thomas Buckley-Houston Date: Wed, 19 Jun 2019 13:23:42 +0300 Subject: [PATCH] HTTP server: timeout for page loading --- interfacer/src/browsh/config_sample.go | 3 ++ interfacer/src/browsh/raw_text_server.go | 37 +++++++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/interfacer/src/browsh/config_sample.go b/interfacer/src/browsh/config_sample.go index 9ee24b9f..8da12ce0 100644 --- a/interfacer/src/browsh/config_sample.go +++ b/interfacer/src/browsh/config_sample.go @@ -67,6 +67,9 @@ bind = "0.0.0.0" # parsed, too long and you wait unecessarily. render_delay = 100 +# The length of time in seconds to wait before aborting the page load +timeout = 30 + # The dimensions of a char-based window onto a webpage. # The columns are ultimately the width of the final text. Whereas the rows # represent the height of the original web page made visible to the original diff --git a/interfacer/src/browsh/raw_text_server.go b/interfacer/src/browsh/raw_text_server.go index 230b5ee3..8f50ff9c 100644 --- a/interfacer/src/browsh/raw_text_server.go +++ b/interfacer/src/browsh/raw_text_server.go @@ -242,25 +242,36 @@ func getRawTextMode(r *http.Request) string { func waitForResponse(rawTextRequestID string, w http.ResponseWriter) { var rawTextRequestResponse string - var jsonResponse rawTextResponse - var totalTime, pageLoad, parsing string var ok bool - for { + isSent := false + maxTime := time.Duration(viper.GetInt("http-server.timeout")) * time.Second + start := time.Now() + for time.Since(start) < maxTime { if rawTextRequestResponse, ok = rawTextRequests.load(rawTextRequestID); ok { - jsonResponse = unpackResponse(rawTextRequestResponse) - requestStart, _ := rawTextRequests.load(rawTextRequestID + "-start") - totalTime = getTotalTiming(requestStart) - pageLoad = fmt.Sprintf("%d", jsonResponse.PageloadDuration) - parsing = fmt.Sprintf("%d", jsonResponse.ParsingDuration) - w.Header().Set("X-Browsh-Duration-Total", totalTime) - w.Header().Set("X-Browsh-Duration-Pageload", pageLoad) - w.Header().Set("X-Browsh-Duration-Parsing", parsing) - io.WriteString(w, jsonResponse.Text) - rawTextRequests.remove(rawTextRequestID) + sendResponse(rawTextRequestResponse, rawTextRequestID, w) + isSent = true break } time.Sleep(1 * time.Millisecond) } + rawTextRequests.remove(rawTextRequestID) + if !isSent { + timeout := viper.GetInt("http-server.timeout") + message := fmt.Sprintf("Browsh rendering aborted after %ds timeout.", timeout) + io.WriteString(w, message) + } +} + +func sendResponse(response, rawTextRequestID string, w http.ResponseWriter) { + jsonResponse := unpackResponse(response) + requestStart, _ := rawTextRequests.load(rawTextRequestID + "-start") + totalTime := getTotalTiming(requestStart) + pageLoad := fmt.Sprintf("%d", jsonResponse.PageloadDuration) + parsing := fmt.Sprintf("%d", jsonResponse.ParsingDuration) + w.Header().Set("X-Browsh-Duration-Total", totalTime) + w.Header().Set("X-Browsh-Duration-Pageload", pageLoad) + w.Header().Set("X-Browsh-Duration-Parsing", parsing) + io.WriteString(w, jsonResponse.Text) } func unpackResponse(jsonString string) rawTextResponse {