finished refactoring

This commit is contained in:
Annika Hannig 2021-12-07 19:11:11 +01:00
parent 478f828e22
commit 5fc5841ece
11 changed files with 100 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"strconv"
"strings"
@ -35,7 +36,8 @@ func printBanner(
neighborsStore *store.NeighborsStore,
routesStore *store.RoutesStore,
) {
status, _ := http.CollectAppStatus(routesStore, neighborsStore)
ctx := context.Background()
status, _ := http.CollectAppStatus(ctx, routesStore, neighborsStore)
mapper := strings.NewReplacer(
"?VERSION", status.Version,
"?LISTEN", cfg.Server.Listen,

View File

@ -8,10 +8,10 @@ import (
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/http"
"github.com/alice-lg/alice-lg/pkg/store"
"github.com/alice-lg/alice-lg/pkg/store/backends/memory"
)
func main() {
done := make(chan bool)
ctx := context.Background()
// Handle commandline parameters
@ -29,8 +29,11 @@ func main() {
}
// Setup local routes store
neighborsStore := store.NewNeighborsStore(cfg)
routesStore := store.NewRoutesStore(neighborsStore, cfg)
neighborsBackend := memory.NewNeighborsBackend()
routesBackend := memory.NewRoutesBackend()
neighborsStore := store.NewNeighborsStore(cfg, neighborsBackend)
routesStore := store.NewRoutesStore(neighborsStore, cfg, routesBackend)
// Say hi
printBanner(cfg, neighborsStore, routesStore)
@ -49,5 +52,5 @@ func main() {
server := http.NewServer(cfg, routesStore, neighborsStore)
go server.Start()
<-done
<-ctx.Done()
}

View File

@ -11,10 +11,11 @@ import (
// Handle Status Endpoint, this is intended for
// monitoring and service health checks
func (s *Server) apiStatusShow(
_req *http.Request,
req *http.Request,
_params httprouter.Params,
) (response, error) {
status, err := CollectAppStatus(s.routesStore, s.neighborsStore)
ctx := req.Context()
status, err := CollectAppStatus(ctx, s.routesStore, s.neighborsStore)
return status, err
}

View File

@ -41,7 +41,7 @@ func (s *Server) apiNeighborsList(
CachedAt: status.LastRefresh,
},
ResultFromCache: true, // you bet!
TTL: s.neighborsStore.SourceCacheTTL(rsID),
TTL: s.neighborsStore.SourceCacheTTL(ctx, rsID),
},
},
Neighbors: neighbors,

View File

@ -112,15 +112,8 @@ func (s *Server) apiLookupPrefixGlobal(
// Calculate query duration
queryDuration := time.Since(t0)
cachedAt, err := s.routesStore.CachedAt(ctx)
if err != nil {
return nil, err
}
ttl, err := s.routesStore.CacheTTL(ctx)
if err != nil {
return nil, err
}
cachedAt := s.routesStore.CachedAt(ctx)
ttl := s.routesStore.CacheTTL(ctx)
// Make response
response := api.PaginatedRoutesLookupResponse{
@ -157,9 +150,13 @@ func (s *Server) apiLookupNeighborsGlobal(
req *http.Request,
params httprouter.Params,
) (response, error) {
ctx := req.Context()
// Query neighbors store
filter := api.NeighborFilterFromQuery(req.URL.Query())
neighbors := s.neighborsStore.FilterNeighbors(filter)
neighbors, err := s.neighborsStore.FilterNeighbors(ctx, filter)
if err != nil {
return nil, err
}
sort.Sort(neighbors)
@ -168,10 +165,10 @@ func (s *Server) apiLookupNeighborsGlobal(
Response: api.Response{
Meta: &api.Meta{
CacheStatus: api.CacheStatus{
CachedAt: s.neighborsStore.CachedAt(),
CachedAt: s.neighborsStore.CachedAt(ctx),
},
ResultFromCache: true, // You would not have guessed.
TTL: s.neighborsStore.CacheTTL(),
TTL: s.neighborsStore.CacheTTL(ctx),
},
},
Neighbors: neighbors,

View File

@ -3,6 +3,7 @@
package http
import (
"context"
"log"
"net/http"
"time"
@ -38,9 +39,10 @@ func NewServer(
// on the configured port.
func (s *Server) Start() {
router := httprouter.New()
ctx := context.Background()
// Register routes
if err := s.webRegisterAssets(router); err != nil {
if err := s.webRegisterAssets(ctx, router); err != nil {
log.Fatal(err)
}
if err := s.apiRegisterEndpoints(router); err != nil {

View File

@ -1,6 +1,8 @@
package http
import (
"context"
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/store"
@ -8,7 +10,7 @@ import (
// AppStatus contains application status information
type AppStatus struct {
Version string `json:"version"`
Version string `json:"version"`
Routes *api.RoutesStoreStats `json:"routes"`
Neighbors *api.NeighborsStoreStats `json:"neighbors"`
}
@ -17,6 +19,7 @@ type AppStatus struct {
// status with stats gathered from the various
// application modules.
func CollectAppStatus(
ctx context.Context,
routesStore *store.RoutesStore,
neighborsStore *store.NeighborsStore,
) (*AppStatus, error) {
@ -27,7 +30,7 @@ func CollectAppStatus(
neighborsStatus := &api.NeighborsStoreStats{}
if neighborsStore != nil {
neighborsStatus = neighborsStore.Stats()
neighborsStatus = neighborsStore.Stats(ctx)
}
status := &AppStatus{

View File

@ -1,6 +1,7 @@
package http
import (
"context"
"io"
"log"
"net/http"
@ -16,8 +17,11 @@ import (
// Prepare client HTML:
// Set paths and add version to assets.
func (s *Server) webPrepareClientHTML(html string) string {
status, _ := CollectAppStatus(s.routesStore, s.neighborsStore)
func (s *Server) webPrepareClientHTML(
ctx context.Context,
html string,
) string {
status, _ := CollectAppStatus(ctx, s.routesStore, s.neighborsStore)
// Replace paths and tags
rewriter := strings.NewReplacer(
@ -34,7 +38,10 @@ func (s *Server) webPrepareClientHTML(html string) string {
// Register assets handler and index handler
// at /static and /
func (s *Server) webRegisterAssets(router *httprouter.Router) error {
func (s *Server) webRegisterAssets(
ctx context.Context,
router *httprouter.Router,
) error {
log.Println("Preparing and installing assets")
// Prepare client html: Rewrite paths
@ -51,7 +58,7 @@ func (s *Server) webRegisterAssets(router *httprouter.Router) error {
}
// Update paths
indexHTML = s.webPrepareClientHTML(indexHTML)
indexHTML = s.webPrepareClientHTML(ctx, indexHTML)
// Register static assets
router.Handler("GET", "/static/*path", client.AssetsHTTPHandler("/static"))

View File

@ -186,6 +186,21 @@ func (s *NeighborsStore) update() {
}
}
// CachedAt returns the time of the oldest partial
// refresh of the dataset.
func (s *NeighborsStore) CachedAt(
ctx context.Context,
) time.Time {
return s.sources.CachedAt(ctx)
}
// CacheTTL returns the TTL time
func (s *NeighborsStore) CacheTTL(
ctx context.Context,
) time.Time {
return s.sources.NextRefresh(ctx)
}
// GetNeighborsAt gets all neighbors from a routeserver
func (s *NeighborsStore) GetNeighborsAt(
ctx context.Context,
@ -329,6 +344,9 @@ func (s *NeighborsStore) SourceCachedAt(sourceID string) time.Time {
// SourceCacheTTL returns the next time when a refresh
// will be started.
func (s *NeighborsStore) SourceCacheTTL(sourceID string) time.Time {
return s.sources.NextRefresh(sourceID)
func (s *NeighborsStore) SourceCacheTTL(
ctx context.Context,
sourceID string,
) time.Time {
return s.sources.NextRefresh(ctx)
}

View File

@ -258,16 +258,18 @@ func (s *RoutesStore) Stats() *api.RoutesStoreStats {
return storeStats
}
// CachedAt returns the time of the most recent partial
// CachedAt returns the time of the oldest partial
// refresh of the dataset.
func (s *RoutesStore) CachedAt(
ctx context.Context,
) (time.Time, error) {
) time.Time {
return s.sources.CachedAt(ctx)
}
// CacheTTL returns the TTL time
func (s *RoutesStore) CacheTTL(id string) time.Time {
func (s *RoutesStore) CacheTTL(
ctx context.Context,
) time.Time {
return s.sources.NextRefresh(ctx)
}

View File

@ -111,15 +111,24 @@ func (s *SourcesStore) IsInitialized(sourceID string) (bool, error) {
}
// NextRefresh calculates the next refresh time
func (s *SourcesStore) NextRefresh(ctx context.Context) time.Time {
status, err := s.GetStatus(sourceID)
if err != nil {
log.Println("get status error:", err)
return time.Time{}
// TODO: I doubt the usefulness of these numbers.
//
func (s *SourcesStore) NextRefresh(
ctx context.Context,
) time.Time {
s.Lock()
defer s.Unlock()
t := time.Time{}
for _, status := range s.status {
nextRefresh := status.LastRefresh.Add(
s.refreshInterval)
if nextRefresh.After(t) {
t = nextRefresh
}
}
nextRefresh := status.LastRefresh.Add(
s.refreshInterval)
return nextRefresh
return t
}
// ShouldRefresh checks if the source needs a
@ -143,6 +152,22 @@ func (s *SourcesStore) ShouldRefresh(
return true // Go for it
}
// CachedAt retrievs the oldest refresh time
// from all sources. All data is then guaranteed to be older
// than the CachedAt date.
func (s *SourcesStore) CachedAt(ctx context.Context) time.Time {
s.Lock()
defer s.Unlock()
t := time.Now().UTC()
for _, status := range s.status {
if status.LastRefresh.Before(t) {
t = status.LastRefresh
}
}
return t
}
// GetInstance retrieves a source instance by ID
func (s *SourcesStore) GetInstance(sourceID string) sources.Source {
s.Lock()