Server now relays a 503 when upstream returns a non-200 response.

This commit is contained in:
Nathan Osman 2016-05-02 08:06:26 -07:00
parent 93a3795a7b
commit c4cfb04285
2 changed files with 18 additions and 5 deletions

15
cache/downloader.go vendored
View File

@ -1,7 +1,6 @@
package cache
import (
"errors"
"io"
"net/http"
"os"
@ -9,6 +8,16 @@ import (
"sync"
)
// DownloadError conveys information about a download request that failed.
type DownloadError struct {
Status string
}
// Error returns a description of the error.
func (d *DownloadError) Error() string {
return d.Status
}
// downloader attempts to download a file from a remote URL.
type downloader struct {
doneMutex sync.Mutex
@ -40,7 +49,9 @@ func newDownloader(rawurl, jsonFilename, dataFilename string) *downloader {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
d.err = errors.New(resp.Status)
d.err = &DownloadError{
Status: resp.Status,
}
return
}
f, err := os.Create(dataFilename)

View File

@ -48,8 +48,6 @@ func (s *Server) writeHeaders(w http.ResponseWriter, e *cache.Entry) {
w.WriteHeader(http.StatusOK)
}
// TODO: support for HEAD requests
// ServeHTTP processes an incoming request to the proxy. GET requests are
// served with the storage backend and every other request is (out of
// necessity) rejected since it can't be cached.
@ -67,7 +65,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer r.Close()
e, err := r.GetEntry()
if err != nil {
if dErr, ok := err.(*cache.DownloadError); ok {
http.Error(w, dErr.Error(), http.StatusServiceUnavailable)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Println("[ERR]", err)
return
}