Skip to content

Commit b17e39b

Browse files
elboulangerojbkempf
authored andcommitted
http: Fix "Internal Server Error" regressions
If the database is not reachable for some reason, mirrorbits must not return "500 Internal Server Error", instead it must redirect users to the fallback mirror(s). This was broken in ed1db9e, here's the fix. Closes: #195
1 parent b2b9d3d commit b17e39b

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

http/http.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,11 @@ func (h *HTTP) mirrorHandler(w http.ResponseWriter, r *http.Request, ctx *Contex
333333
return
334334
}
335335

336-
// Get details about the requested file
336+
// Get details about the requested file. Errors are not fatal, and
337+
// expected when the database is not ready: fallbacks will handle it.
337338
fileInfo, err := h.cache.GetFileInfo(urlPath)
338339
if err != nil {
339-
log.Errorf("Error while fetching Fileinfo: %s", err.Error())
340-
http.Error(w, err.Error(), http.StatusInternalServerError)
341-
return
340+
//log.Debugf("Error while fetching Fileinfo: %s", err.Error())
342341
}
343342

344343
if checkIfModifiedSince(r, fileInfo.ModTime) == condFalse {

http/selection.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package http
55

66
import (
7+
"errors"
78
"fmt"
89
"math"
910
"math/rand"
@@ -18,6 +19,10 @@ import (
1819
"github.com/etix/mirrorbits/utils"
1920
)
2021

22+
var (
23+
ErrInvalidFileInfo = errors.New("Invalid file info (modtime is zero)")
24+
)
25+
2126
type mirrorSelection interface {
2227
// Selection must return an ordered list of selected mirror,
2328
// a list of rejected mirrors and and an error code.
@@ -29,6 +34,12 @@ type DefaultEngine struct{}
2934

3035
// Selection returns an ordered list of selected mirror, a list of rejected mirrors and and an error code
3136
func (h DefaultEngine) Selection(ctx *Context, cache *mirrors.Cache, fileInfo *filesystem.FileInfo, clientInfo network.GeoIPRecord) (mlist mirrors.Mirrors, excluded mirrors.Mirrors, err error) {
37+
// Bail out early if we don't have valid file details
38+
if fileInfo.ModTime.IsZero() {
39+
err = ErrInvalidFileInfo
40+
return
41+
}
42+
3243
// Prepare and return the list of all potential mirrors
3344
mlist, err = cache.GetMirrors(fileInfo.Path, clientInfo)
3445
if err != nil {

0 commit comments

Comments
 (0)