mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
added graceful error handling if bird is unreachable
This commit is contained in:
parent
087175475d
commit
58bb10469c
4 changed files with 27 additions and 10 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
1.8.1
|
||||
1.8.5
|
||||
|
|
23
bird/bird.go
23
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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue