diff --git a/VERSION b/VERSION index a8fdfda..8decb92 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.8.1 +1.8.5 diff --git a/bird/bird.go b/bird/bird.go index a531663..44d0529 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -1,10 +1,12 @@ package bird import ( - "os/exec" + "reflect" "strings" "sync" "time" + + "os/exec" ) var ClientConf BirdConfig @@ -19,17 +21,24 @@ var Cache = struct { m map[string]Parsed }{m: make(map[string]Parsed)} +var NilParse Parsed = (Parsed)(nil) +var BirdError Parsed = Parsed{"error": "bird unreachable"} + +func isSpecial(ret Parsed) bool { + return reflect.DeepEqual(ret, NilParse) || reflect.DeepEqual(ret, BirdError) +} + func fromCache(key string) (Parsed, bool) { Cache.RLock() val, ok := Cache.m[key] Cache.RUnlock() if !ok { - return nil, false + return NilParse, false } ttl, correct := val["ttl"].(time.Time) if !correct || ttl.Before(time.Now()) { - return nil, false + return NilParse, false } return val, ok @@ -88,14 +97,14 @@ func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) { } if !checkRateLimit() { - return nil, false + return NilParse, false } out, err := Run(cmd) if err != nil { // ignore errors for now - return Parsed{}, false + return BirdError, false } parsed := parser(out) @@ -105,7 +114,7 @@ func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) { func Status() (Parsed, bool) { birdStatus, ok := RunAndParse("status", parseStatus) - if birdStatus == nil { + if isSpecial(birdStatus) { return birdStatus, ok } status := birdStatus["status"].(Parsed) @@ -145,7 +154,7 @@ func Protocols() (Parsed, bool) { func ProtocolsBgp() (Parsed, bool) { p, from_cache := Protocols() - if p == nil { + if isSpecial(p) { return p, from_cache } protocols := p["protocols"].([]string) diff --git a/birdwatcher.go b/birdwatcher.go index a1662fb..bdeee8c 100644 --- a/birdwatcher.go +++ b/birdwatcher.go @@ -13,7 +13,7 @@ import ( ) //go:generate versionize -var VERSION = "1.7.18" +var VERSION = "1.8.5" func isModuleEnabled(module string, modulesEnabled []string) bool { for _, enabled := range modulesEnabled { diff --git a/endpoints/endpoint.go b/endpoints/endpoint.go index a26aa91..50769c7 100644 --- a/endpoints/endpoint.go +++ b/endpoints/endpoint.go @@ -3,6 +3,7 @@ package endpoints import ( "fmt" "log" + "reflect" "strings" "compress/gzip" @@ -55,10 +56,17 @@ func Endpoint(wrapped endpoint) httprouter.Handle { res := make(map[string]interface{}) ret, from_cache := wrapped(r, ps) - if ret == nil { + if reflect.DeepEqual(ret, bird.NilParse) { w.WriteHeader(http.StatusTooManyRequests) return } + if reflect.DeepEqual(ret, bird.BirdError) { + w.WriteHeader(http.StatusInternalServerError) + w.Header().Set("Content-Type", "application/json") + js, _ := json.Marshal(ret) + w.Write(js) + return + } res["api"] = GetApiInfo(from_cache) for k, v := range ret {