HTTP server: timeout for page loading

pull/286/head
Thomas Buckley-Houston 2019-06-19 13:23:42 +03:00
rodzic 759e8a125a
commit e85455880a
2 zmienionych plików z 27 dodań i 13 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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 {