/
api_endpoints_neighbors.go
71 lines (64 loc) · 1.65 KB
/
api_endpoints_neighbors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package http
import (
"context"
"net/http"
"sort"
"github.com/julienschmidt/httprouter"
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/config"
)
// Handle get neighbors on routeserver
func (s *Server) apiNeighborsList(
ctx context.Context,
req *http.Request,
params httprouter.Params,
) (response, error) {
rsID, err := validateSourceID(params.ByName("id"))
if err != nil {
return nil, err
}
var neighborsResponse *api.NeighborsResponse
// Try to fetch neighbors from store, only fall back
// to RS query if store is not ready yet.
// The stored neighbors response includes details like
// the number of filtered routes which might be lacking
// from the summary.
if s.neighborsStore.IsInitialized(rsID) {
status, err := s.neighborsStore.GetStatus(rsID)
if err != nil {
return nil, err
}
neighbors, err := s.neighborsStore.GetNeighborsAt(ctx, rsID)
if err != nil {
return nil, err
}
// Make response
neighborsResponse = &api.NeighborsResponse{
Response: api.Response{
Meta: &api.Meta{
Version: config.Version,
CacheStatus: api.CacheStatus{
OrigTTL: 0,
CachedAt: status.LastRefresh,
},
ResultFromCache: true, // you bet!
TTL: s.neighborsStore.SourceCacheTTL(ctx, rsID),
},
},
Neighbors: neighbors,
}
} else {
source := s.cfg.SourceInstanceByID(rsID)
if source == nil {
return nil, ErrSourceNotFound
}
neighborsResponse, err = source.NeighborsSummary(ctx)
if err != nil {
s.logSourceError("neighbors", rsID, err)
return nil, err
}
}
// Sort result
sort.Sort(&neighborsResponse.Neighbors)
return neighborsResponse, nil
}