Skip to content

Commit

Permalink
Handles failures better.
Browse files Browse the repository at this point in the history
1. When 'failed' is closed !ok will be true -- we weren't properly
   hitting that case and the connection would stay open.
2. Cancels the request when a bad response code is hit.
  • Loading branch information
hackergrrl committed Feb 11, 2016
1 parent dc7fe00 commit 2b00a11
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,10 @@ func main() {
for {
chosen, value, ok := reflect.Select(cases)
if ok {
// Handle failure.
if value.Interface() == failed {
w.WriteHeader(500)
break
}

res := value.Interface().(*http.Response)

if allowedCodes[res.StatusCode] {
// Close all of the other pending request channels.
// Close all of the request channels.
for i := range cancels {
if i != chosen {
close(cancels[i])
Expand All @@ -131,10 +125,16 @@ func main() {
w.Header()[k] = v
}
} else {
close(cancels[chosen])
continue
}
io.Copy(w, res.Body)
break
} else {
// Handle failure.
w.WriteHeader(500)
w.(http.Flusher).Flush()
break
}
}
})
Expand Down

0 comments on commit 2b00a11

Please sign in to comment.