mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
reminder: never forget to go fmt
This commit is contained in:
parent
5bb95f264f
commit
a1b5dabcd3
3 changed files with 22 additions and 23 deletions
39
bird/bird.go
39
bird/bird.go
|
@ -3,32 +3,31 @@ package bird
|
|||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
"sync"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var BirdCmd string
|
||||
|
||||
var Cache = struct{
|
||||
sync.RWMutex
|
||||
m map[string]Parsed
|
||||
var Cache = struct {
|
||||
sync.RWMutex
|
||||
m map[string]Parsed
|
||||
}{m: make(map[string]Parsed)}
|
||||
|
||||
func fromCache(key string) (Parsed, bool) {
|
||||
Cache.RLock()
|
||||
val, ok := Cache.m[key]
|
||||
Cache.RUnlock()
|
||||
return val, ok
|
||||
Cache.RLock()
|
||||
val, ok := Cache.m[key]
|
||||
Cache.RUnlock()
|
||||
return val, ok
|
||||
}
|
||||
|
||||
func toCache(key string, val Parsed) {
|
||||
val["ttl"] = time.Now().Add(5 * time.Minute)
|
||||
Cache.Lock()
|
||||
Cache.m[key] = val
|
||||
Cache.Unlock()
|
||||
val["ttl"] = time.Now().Add(5 * time.Minute)
|
||||
Cache.Lock()
|
||||
Cache.m[key] = val
|
||||
Cache.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func Run(args string) ([]byte, error) {
|
||||
args = "show " + args
|
||||
argsList := strings.Split(args, " ")
|
||||
|
@ -36,9 +35,9 @@ func Run(args string) ([]byte, error) {
|
|||
}
|
||||
|
||||
func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) {
|
||||
if val, ok := fromCache(cmd); ok {
|
||||
return val, true
|
||||
}
|
||||
if val, ok := fromCache(cmd); ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
out, err := Run(cmd)
|
||||
|
||||
|
@ -48,8 +47,8 @@ func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) {
|
|||
}
|
||||
|
||||
parsed := parser(out)
|
||||
toCache(cmd, parsed)
|
||||
return parsed, false
|
||||
toCache(cmd, parsed)
|
||||
return parsed, false
|
||||
}
|
||||
|
||||
func Status() (Parsed, bool) {
|
||||
|
@ -62,7 +61,7 @@ func Protocols() (Parsed, bool) {
|
|||
|
||||
func ProtocolsBgp() (Parsed, bool) {
|
||||
p, from_cache := Protocols()
|
||||
protocols := p["protocols"].([]string)
|
||||
protocols := p["protocols"].([]string)
|
||||
|
||||
bgpProto := Parsed{}
|
||||
|
||||
|
|
|
@ -10,11 +10,11 @@ func Symbols(ps httprouter.Params) (bird.Parsed, bool) {
|
|||
}
|
||||
|
||||
func SymbolTables(ps httprouter.Params) (bird.Parsed, bool) {
|
||||
val, from_cache := bird.Symbols()
|
||||
val, from_cache := bird.Symbols()
|
||||
return bird.Parsed{"symbols": val["routing table"]}, from_cache
|
||||
}
|
||||
|
||||
func SymbolProtocols(ps httprouter.Params) (bird.Parsed, bool) {
|
||||
val, from_cache := bird.Symbols()
|
||||
val, from_cache := bird.Symbols()
|
||||
return bird.Parsed{"symbols": val["protocols"]}, from_cache
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ func GetApiInfo(from_cache bool) *APIInfo {
|
|||
ai := &APIInfo{}
|
||||
|
||||
ai.Version = "1.0"
|
||||
ai.ResultFromCache = from_cache
|
||||
ai.ResultFromCache = from_cache
|
||||
|
||||
return ai
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue