From fc686df4cb99a1b51dfdc744d941c6bf47b52902 Mon Sep 17 00:00:00 2001 From: Matthias Hannig Date: Fri, 6 Jul 2018 17:04:09 +0200 Subject: [PATCH] used pointers instead --- backend/api.go | 2 +- backend/api/response.go | 24 ++++++++-------- backend/neighbours_store.go | 8 +++--- backend/neighbours_store_test.go | 10 +++---- backend/routes_store.go | 40 +++++++++++++------------- backend/routes_store_test.go | 4 +-- backend/sources/birdwatcher/parsers.go | 10 +++---- backend/sources/birdwatcher/source.go | 8 +++--- backend/sources/birdwatcher/utils.go | 4 +-- 9 files changed, 55 insertions(+), 55 deletions(-) diff --git a/backend/api.go b/backend/api.go index 130353c..8ce5d58 100644 --- a/backend/api.go +++ b/backend/api.go @@ -220,7 +220,7 @@ func apiLookupPrefixGlobal(req *http.Request, params httprouter.Params) (api.Res t0 := time.Now() // Perform query - var routes []api.LookupRoute + var routes []*api.LookupRoute if lookupPrefix { routes = AliceRoutesStore.LookupPrefix(q) diff --git a/backend/api/response.go b/backend/api/response.go index 1c5d4a9..48b4f2d 100644 --- a/backend/api/response.go +++ b/backend/api/response.go @@ -81,7 +81,7 @@ type RouteserversResponse struct { } // Neighbours -type Neighbours []Neighbour +type Neighbours []*Neighbour type Neighbour struct { Id string `json:"id"` @@ -122,7 +122,7 @@ type NeighboursResponse struct { Neighbours Neighbours `json:"neighbours"` } -type NeighboursLookupResults map[int][]Neighbour +type NeighboursLookupResults map[int][]*Neighbour // BGP type Community []int @@ -155,9 +155,9 @@ type Route struct { // Lookup Prefixes type LookupRoute struct { - Id string `json:"id"` - NeighbourId string `json:"neighbour_id"` - Neighbour Neighbour `json:"neighbour"` + Id string `json:"id"` + NeighbourId string `json:"neighbour_id"` + Neighbour *Neighbour `json:"neighbour"` State string `json:"state"` // Filtered, Imported, ... @@ -174,7 +174,7 @@ type LookupRoute struct { Details Details `json:"details"` } -type Routes []Route +type Routes []*Route // Implement sorting interface for routes func (routes Routes) Len() int { @@ -191,18 +191,18 @@ func (routes Routes) Swap(i, j int) { type RoutesResponse struct { Api ApiStatus `json:"api"` - Imported []Route `json:"imported"` - Filtered []Route `json:"filtered"` - NotExported []Route `json:"not_exported"` + Imported []*Route `json:"imported"` + Filtered []*Route `json:"filtered"` + NotExported []*Route `json:"not_exported"` } type RoutesLookupResponse struct { - Api ApiStatus `json:"api"` - Routes []LookupRoute `json:"routes"` + Api ApiStatus `json:"api"` + Routes []*LookupRoute `json:"routes"` } type RoutesLookupResponseGlobal struct { - Routes []LookupRoute `json:"routes"` + Routes []*LookupRoute `json:"routes"` // Pagination TotalRoutes int `json:"total_routes"` diff --git a/backend/neighbours_store.go b/backend/neighbours_store.go index 84de336..1c1477d 100644 --- a/backend/neighbours_store.go +++ b/backend/neighbours_store.go @@ -10,7 +10,7 @@ import ( "github.com/alice-lg/alice-lg/backend/api" ) -type NeighboursIndex map[string]api.Neighbour +type NeighboursIndex map[string]*api.Neighbour type NeighboursStore struct { neighboursMap map[int]NeighboursIndex @@ -122,7 +122,7 @@ func (self *NeighboursStore) update() { func (self *NeighboursStore) GetNeighbourAt( sourceId int, id string, -) api.Neighbour { +) *api.Neighbour { // Lookup neighbour on RS self.RLock() neighbours := self.neighboursMap[sourceId] @@ -133,8 +133,8 @@ func (self *NeighboursStore) GetNeighbourAt( func (self *NeighboursStore) LookupNeighboursAt( sourceId int, query string, -) []api.Neighbour { - results := []api.Neighbour{} +) []*api.Neighbour { + results := []*api.Neighbour{} self.RLock() neighbours := self.neighboursMap[sourceId] diff --git a/backend/neighbours_store_test.go b/backend/neighbours_store_test.go index 80cacda..b0d4ff9 100644 --- a/backend/neighbours_store_test.go +++ b/backend/neighbours_store_test.go @@ -23,26 +23,26 @@ func makeTestNeighboursStore() *NeighboursStore { // Populate neighbours rs1 := NeighboursIndex{ - "ID2233_AS2342": api.Neighbour{ + "ID2233_AS2342": &api.Neighbour{ Id: "ID2233_AS2342", Description: "PEER AS2342 192.9.23.42 Customer Peer 1", }, - "ID2233_AS2343": api.Neighbour{ + "ID2233_AS2343": &api.Neighbour{ Id: "ID2233_AS2343", Description: "PEER AS2343 192.9.23.43 Different Peer 1", }, - "ID2233_AS2344": api.Neighbour{ + "ID2233_AS2344": &api.Neighbour{ Id: "ID2233_AS2344", Description: "PEER AS2344 192.9.23.44 3rd Peer from the sun", }, } rs2 := NeighboursIndex{ - "ID2233_AS2342": api.Neighbour{ + "ID2233_AS2342": &api.Neighbour{ Id: "ID2233_AS2342", Description: "PEER AS2342 192.9.23.42 Customer Peer 1", }, - "ID2233_AS4223": api.Neighbour{ + "ID2233_AS4223": &api.Neighbour{ Id: "ID2233_AS4223", Description: "PEER AS4223 192.9.42.23 Cloudfoo Inc.", }, diff --git a/backend/routes_store.go b/backend/routes_store.go index 41b4d3c..5b2be16 100644 --- a/backend/routes_store.go +++ b/backend/routes_store.go @@ -153,13 +153,13 @@ func (self *RoutesStore) Stats() RoutesStoreStats { } // Lookup routes transform -func routeToLookupRoute(source SourceConfig, state string, route api.Route) api.LookupRoute { +func routeToLookupRoute(source SourceConfig, state string, route *api.Route) *api.LookupRoute { // Get neighbour neighbour := AliceNeighboursStore.GetNeighbourAt(source.Id, route.NeighbourId) // Make route - lookup := api.LookupRoute{ + lookup := &api.LookupRoute{ Id: route.Id, NeighbourId: route.NeighbourId, @@ -187,12 +187,12 @@ func routeToLookupRoute(source SourceConfig, state string, route api.Route) api. // Routes filter func filterRoutesByPrefix( source SourceConfig, - routes []api.Route, + routes []*api.Route, prefix string, state string, -) []api.LookupRoute { +) []*api.LookupRoute { - results := []api.LookupRoute{} + results := []*api.LookupRoute{} for _, route := range routes { // Naiive filtering: if strings.HasPrefix(route.Network, prefix) { @@ -205,12 +205,12 @@ func filterRoutesByPrefix( func filterRoutesByNeighbourIds( source SourceConfig, - routes []api.Route, + routes []*api.Route, neighbourIds []string, state string, -) []api.LookupRoute { +) []*api.LookupRoute { - results := []api.LookupRoute{} + results := []*api.LookupRoute{} for _, route := range routes { // Filtering: if MemberOf(neighbourIds, route.NeighbourId) == true { @@ -225,8 +225,8 @@ func filterRoutesByNeighbourIds( func (self *RoutesStore) LookupNeighboursPrefixesAt( sourceId int, neighbourIds []string, -) chan []api.LookupRoute { - response := make(chan []api.LookupRoute) +) chan []*api.LookupRoute { + response := make(chan []*api.LookupRoute) go func() { self.RLock() @@ -245,7 +245,7 @@ func (self *RoutesStore) LookupNeighboursPrefixesAt( neighbourIds, "imported") - var result []api.LookupRoute + var result []*api.LookupRoute result = append(filtered, imported...) response <- result @@ -258,9 +258,9 @@ func (self *RoutesStore) LookupNeighboursPrefixesAt( func (self *RoutesStore) LookupPrefixAt( sourceId int, prefix string, -) chan []api.LookupRoute { +) chan []*api.LookupRoute { - response := make(chan []api.LookupRoute) + response := make(chan []*api.LookupRoute) go func() { self.RLock() @@ -279,7 +279,7 @@ func (self *RoutesStore) LookupPrefixAt( prefix, "imported") - var result []api.LookupRoute + var result []*api.LookupRoute result = append(filtered, imported...) response <- result @@ -288,9 +288,9 @@ func (self *RoutesStore) LookupPrefixAt( return response } -func (self *RoutesStore) LookupPrefix(prefix string) []api.LookupRoute { - result := []api.LookupRoute{} - responses := []chan []api.LookupRoute{} +func (self *RoutesStore) LookupPrefix(prefix string) []*api.LookupRoute { + result := []*api.LookupRoute{} + responses := []chan []*api.LookupRoute{} // Dispatch self.RLock() @@ -312,10 +312,10 @@ func (self *RoutesStore) LookupPrefix(prefix string) []api.LookupRoute { func (self *RoutesStore) LookupPrefixForNeighbours( neighbours api.NeighboursLookupResults, -) []api.LookupRoute { +) []*api.LookupRoute { - result := []api.LookupRoute{} - responses := []chan []api.LookupRoute{} + result := []*api.LookupRoute{} + responses := []chan []*api.LookupRoute{} // Dispatch for sourceId, locals := range neighbours { diff --git a/backend/routes_store_test.go b/backend/routes_store_test.go index 1c2218a..9c1b4fa 100644 --- a/backend/routes_store_test.go +++ b/backend/routes_store_test.go @@ -193,8 +193,8 @@ func TestLookupNeighboursPrefixesAt(t *testing.T) { func TestLookupPrefixForNeighbours(t *testing.T) { // Construct a neighbours lookup result neighbours := api.NeighboursLookupResults{ - 1: []api.Neighbour{ - api.Neighbour{ + 1: []*api.Neighbour{ + &api.Neighbour{ Id: "ID163_AS31078", }, }, diff --git a/backend/sources/birdwatcher/parsers.go b/backend/sources/birdwatcher/parsers.go index 4570c96..ceedd50 100644 --- a/backend/sources/birdwatcher/parsers.go +++ b/backend/sources/birdwatcher/parsers.go @@ -115,7 +115,7 @@ func parseRelativeServerTime(uptime interface{}, config Config) time.Duration { } // Parse neighbours response -func parseNeighbours(bird ClientResponse, config Config) ([]api.Neighbour, error) { +func parseNeighbours(bird ClientResponse, config Config) ([]*api.Neighbour, error) { neighbours := api.Neighbours{} protocols := bird["protocols"].(map[string]interface{}) @@ -127,7 +127,7 @@ func parseNeighbours(bird ClientResponse, config Config) ([]api.Neighbour, error uptime := parseRelativeServerTime(protocol["state_changed"], config) lastError := mustString(protocol["last_error"], "") - neighbour := api.Neighbour{ + neighbour := &api.Neighbour{ Id: protocolId, Address: mustString(protocol["neighbor_address"], "error"), @@ -259,7 +259,7 @@ func parseRoutesData(birdRoutes []interface{}, config Config) api.Routes { rtype := mustStringList(rdata["type"]) bgpInfo := parseRouteBgpInfo(rdata["bgp"]) - route := api.Route{ + route := &api.Route{ Id: mustString(rdata["network"], "unknown"), NeighbourId: mustString(rdata["from_protocol"], "unknown neighbour"), @@ -280,10 +280,10 @@ func parseRoutesData(birdRoutes []interface{}, config Config) api.Routes { } // Parse routes response -func parseRoutes(bird ClientResponse, config Config) ([]api.Route, error) { +func parseRoutes(bird ClientResponse, config Config) ([]*api.Route, error) { birdRoutes, ok := bird["routes"].([]interface{}) if !ok { - return []api.Route{}, fmt.Errorf("Routes response missing") + return []*api.Route{}, fmt.Errorf("Routes response missing") } routes := parseRoutesData(birdRoutes, config) diff --git a/backend/sources/birdwatcher/source.go b/backend/sources/birdwatcher/source.go index 2b9a2fe..4fdd7dd 100644 --- a/backend/sources/birdwatcher/source.go +++ b/backend/sources/birdwatcher/source.go @@ -106,7 +106,7 @@ func (self *Birdwatcher) Routes(neighbourId string) (api.RoutesResponse, error) result_filtered := make(api.Routes, 0, len(filtered)) result_imported := make(api.Routes, 0, len(imported)) - importedMap := make(map[string]api.Route) // for O(1) access + importedMap := make(map[string]*api.Route) // for O(1) access for _, route := range imported { importedMap[route.Id] = route } @@ -136,7 +136,7 @@ func (self *Birdwatcher) Routes(neighbourId string) (api.RoutesResponse, error) log.Println("WARNING Could not retrieve routes not exported:", err) log.Println("Is the 'routes_noexport' module active in birdwatcher?") } else { - result_noexport := make([]api.Route, 0, len(noexport)) + result_noexport := make([]*api.Route, 0, len(noexport)) // choose routes with next_hop == gateway of this neighbour for _, route := range noexport { if (route.Gateway == gateway) || (route.Gateway == learnt_from) { @@ -179,11 +179,11 @@ func (self *Birdwatcher) LookupPrefix(prefix string) (api.RoutesLookupResponse, routes, err := parseRoutes(bird, self.config) // Add corresponding neighbour and source rs to result - results := []api.LookupRoute{} + results := []*api.LookupRoute{} for _, src := range routes { // Okay. This is actually really hacky. // A less bruteforce approach would be highly appreciated - route := api.LookupRoute{ + route := &api.LookupRoute{ Id: src.Id, Routeserver: rs, diff --git a/backend/sources/birdwatcher/utils.go b/backend/sources/birdwatcher/utils.go index 96e85d9..4985ad1 100644 --- a/backend/sources/birdwatcher/utils.go +++ b/backend/sources/birdwatcher/utils.go @@ -11,13 +11,13 @@ Helper functions for dealing with birdwatcher API data */ // Get neighbour by protocol id -func getNeighbourById(neighbours api.Neighbours, id string) (api.Neighbour, error) { +func getNeighbourById(neighbours api.Neighbours, id string) (*api.Neighbour, error) { for _, n := range neighbours { if n.Id == id { return n, nil } } - unknown := api.Neighbour{ + unknown := &api.Neighbour{ Id: "unknown", Description: "Unknown neighbour", }