Skip to content

Commit

Permalink
HTTP server: timeout for page loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tombh committed Jun 19, 2019
1 parent 759e8a1 commit e854558
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
3 changes: 3 additions & 0 deletions interfacer/src/browsh/config_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 24 additions & 13 deletions interfacer/src/browsh/raw_text_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e854558

Please sign in to comment.