mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
include caching information alongside ttl
This commit is contained in:
parent
f82f265612
commit
712510b3bc
3 changed files with 50 additions and 7 deletions
12
bird/bird.go
12
bird/bird.go
|
@ -53,7 +53,13 @@ func toCache(key string, val Parsed) {
|
|||
if ClientConf.CacheTtl > 0 {
|
||||
ttl = ClientConf.CacheTtl
|
||||
}
|
||||
val["ttl"] = time.Now().Add(time.Duration(ttl) * time.Minute)
|
||||
cachedAt := time.Now().UTC()
|
||||
cacheTtl := cachedAt.Add(time.Duration(ttl) * time.Minute)
|
||||
|
||||
// This is not a really ... clean way of doing this.
|
||||
val["ttl"] = cacheTtl
|
||||
val["cached_at"] = cachedAt
|
||||
|
||||
Cache.Lock()
|
||||
Cache.m[key] = val
|
||||
Cache.Unlock()
|
||||
|
@ -186,7 +192,9 @@ func ProtocolsBgp() (Parsed, bool) {
|
|||
}
|
||||
}
|
||||
|
||||
return Parsed{"protocols": bgpProto, "ttl": p["ttl"]}, from_cache
|
||||
return Parsed{"protocols": bgpProto,
|
||||
"ttl": p["ttl"],
|
||||
"cached_at": p["cached_at"]}, from_cache
|
||||
}
|
||||
|
||||
func Symbols() (Parsed, bool) {
|
||||
|
|
|
@ -67,7 +67,7 @@ func Endpoint(wrapped endpoint) httprouter.Handle {
|
|||
w.Write(js)
|
||||
return
|
||||
}
|
||||
res["api"] = GetApiInfo(from_cache)
|
||||
res["api"] = GetApiInfo(&ret, from_cache)
|
||||
|
||||
for k, v := range ret {
|
||||
res[k] = v
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
package endpoints
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/alice-lg/birdwatcher/bird"
|
||||
)
|
||||
|
||||
type TimeInfo struct {
|
||||
Date string `json:"date"`
|
||||
TimezoneType string `json:"timezone_type"`
|
||||
Timezone string `json:"timezone"`
|
||||
Date time.Time `json:"date"`
|
||||
TimezoneType string `json:"timezone_type"`
|
||||
Timezone string `json:"timezone"`
|
||||
}
|
||||
|
||||
type CacheStatus struct {
|
||||
|
@ -20,11 +26,40 @@ type APIInfo struct {
|
|||
// go generate does not work in subdirectories. Beautious.
|
||||
var VERSION string
|
||||
|
||||
func GetApiInfo(from_cache bool) *APIInfo {
|
||||
func GetApiInfo(res *bird.Parsed, from_cache bool) *APIInfo {
|
||||
ai := &APIInfo{}
|
||||
|
||||
ai.Version = VERSION
|
||||
ai.ResultFromCache = from_cache
|
||||
|
||||
api := *res
|
||||
|
||||
// Derive cache status from TTL
|
||||
cachedAt, ok := api["cached_at"].(time.Time)
|
||||
if !ok {
|
||||
cachedAt = time.Time{}
|
||||
}
|
||||
|
||||
// tbh. I have no clue what the difference between
|
||||
// timezone type and timezone actually is.
|
||||
// I could trace back the timezonetype to the symphony framework
|
||||
// Barry was using; the docs say it accepts timezones like
|
||||
// "America/New_York", however nothing about UTC could be found.
|
||||
//
|
||||
// As we convert everything to UTC and let the client
|
||||
// render it in local time, it is safe to set this to a fixed
|
||||
// value.
|
||||
|
||||
cacheInfo := CacheStatus{
|
||||
OrigTTL: -1,
|
||||
CachedAt: TimeInfo{
|
||||
Date: cachedAt,
|
||||
TimezoneType: "UTC",
|
||||
Timezone: "UTC",
|
||||
},
|
||||
}
|
||||
|
||||
ai.CacheStatus = cacheInfo
|
||||
|
||||
return ai
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue