Skip to content
Snippets Groups Projects
Commit 68e33f6a authored by Maximilian Wilhelm's avatar Maximilian Wilhelm
Browse files

Use RWLock with menaingful name, overflow check, white-sapce fixes.

parent a1bc58e3
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,9 @@ import ( ...@@ -7,10 +7,9 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
"github.com/bio-routing/bio-rd/config" "github.com/bio-routing/bio-rd/config"
"github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
......
...@@ -26,7 +26,7 @@ go_test( ...@@ -26,7 +26,7 @@ go_test(
name = "go_default_test", name = "go_default_test",
srcs = [ srcs = [
"client_manager_test.go", "client_manager_test.go",
"contributing_asn_list_test.go", "contributing_asn_list_test.go",
"table_test.go", "table_test.go",
"trie_test.go", "trie_test.go",
"update_helper_test.go", "update_helper_test.go",
......
package routingtable package routingtable
import ( import (
"fmt"
"math"
"sync" "sync"
) )
...@@ -11,8 +13,8 @@ type contributingASN struct { ...@@ -11,8 +13,8 @@ type contributingASN struct {
// ContributingASNs contains a list of contributing ASN to a LocRIB to check ASPaths for possible routing loops. // ContributingASNs contains a list of contributing ASN to a LocRIB to check ASPaths for possible routing loops.
type ContributingASNs struct { type ContributingASNs struct {
contributingASNs []*contributingASN contributingASNs []*contributingASN
mu sync.RWMutex contributingASNsLock sync.RWMutex
} }
// NewContributingASNs creates a list of contributing ASNs to a LocRIB for routing loop prevention. // NewContributingASNs creates a list of contributing ASNs to a LocRIB for routing loop prevention.
...@@ -26,12 +28,17 @@ func NewContributingASNs() *ContributingASNs { ...@@ -26,12 +28,17 @@ func NewContributingASNs() *ContributingASNs {
// Add a new ASN to the list of contributing ASNs or add the ref count of an existing one. // Add a new ASN to the list of contributing ASNs or add the ref count of an existing one.
func (c *ContributingASNs) Add(asn uint32) { func (c *ContributingASNs) Add(asn uint32) {
c.mu.RLock() c.contributingASNsLock.Lock()
defer c.mu.RUnlock() defer c.contributingASNsLock.Unlock()
for _, cASN := range c.contributingASNs { for _, cASN := range c.contributingASNs {
if cASN.asn == asn { if cASN.asn == asn {
cASN.count++ cASN.count++
if cASN.count == math.MaxUint32 {
panic(fmt.Sprintf("Contributing ASNs counter overflow triggered for AS %d. Dyning of shame.", asn))
}
return return
} }
} }
...@@ -44,27 +51,33 @@ func (c *ContributingASNs) Add(asn uint32) { ...@@ -44,27 +51,33 @@ func (c *ContributingASNs) Add(asn uint32) {
// Remove a ASN to the list of contributing ASNs or decrement the ref count of an existing one. // Remove a ASN to the list of contributing ASNs or decrement the ref count of an existing one.
func (c *ContributingASNs) Remove(asn uint32) { func (c *ContributingASNs) Remove(asn uint32) {
c.mu.RLock() c.contributingASNsLock.Lock()
defer c.mu.RUnlock() defer c.contributingASNsLock.Unlock()
asnList := c.contributingASNs asnList := c.contributingASNs
for i, cASN := range asnList { for i, cASN := range asnList {
if cASN.asn == asn { if cASN.asn != asn {
cASN.count-- continue
}
if cASN.count == 0 { cASN.count--
copy(asnList[i:], asnList[i+1:])
asnList = asnList[:len(asnList)] if cASN.count == 0 {
c.contributingASNs = asnList[:len(asnList)-1] copy(asnList[i:], asnList[i+1:])
} asnList = asnList[:len(asnList)]
return c.contributingASNs = asnList[:len(asnList)-1]
} }
return
} }
} }
// IsContributingASN checks if a given ASN is part of the contributing ASNs // IsContributingASN checks if a given ASN is part of the contributing ASNs
func (c *ContributingASNs) IsContributingASN(asn uint32) bool { func (c *ContributingASNs) IsContributingASN(asn uint32) bool {
c.contributingASNsLock.Lock()
defer c.contributingASNsLock.Unlock()
for _, cASN := range c.contributingASNs { for _, cASN := range c.contributingASNs {
if asn == cASN.asn { if asn == cASN.asn {
return true return true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment