From c4cfb042852bb2191a3dd3e8a679f930be75d787 Mon Sep 17 00:00:00 2001 From: Nathan Osman Date: Mon, 2 May 2016 08:06:26 -0700 Subject: [PATCH] Server now relays a 503 when upstream returns a non-200 response. --- cache/downloader.go | 15 +++++++++++++-- server.go | 8 +++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cache/downloader.go b/cache/downloader.go index be4bdb1..f3c2191 100644 --- a/cache/downloader.go +++ b/cache/downloader.go @@ -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) diff --git a/server.go b/server.go index b34daef..b6d935b 100644 --- a/server.go +++ b/server.go @@ -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 { - http.Error(w, err.Error(), http.StatusInternalServerError) + 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 }