Newer
Older
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
package routingtable
import (
"sync"
)
type contributingASN struct {
asn uint32
count uint32
}
// ContributingASNs contains a list of contributing ASN to a LocRIB to check ASPaths for possible routing loops.
type ContributingASNs struct {
contributingASNs []contributingASN
mu sync.RWMutex
}
// NewContributingASNs creates a list of contributing ASNs to a LocRIB for routing loop prevention.
func NewContributingASNs() *ContributingASNs {
c := &ContributingASNs{
contributingASNs: []contributingASN{},
}
return c
}
// 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) {
c.mu.RLock()
defer c.mu.RUnlock()
for _, cASN := range c.contributingASNs {
if cASN.asn == asn {
cASN.count++
return
}
}
c.contributingASNs = append(c.contributingASNs, contributingASN{
asn: asn,
count: 1,
})
}
// Remove a ASN to the list of contributing ASNs or decrement the ref count of an existing one.
func (c *ContributingASNs) Remove(asn uint32) {
c.mu.RLock()
defer c.mu.RUnlock()
asnList := c.contributingASNs
for i, cASN := range asnList {
if cASN.asn == asn {
cASN.count--
if cASN.count == 0 {
copy(asnList[i:], asnList[i+1:])
asnList = asnList[:len(asnList)]
c.contributingASNs = asnList[:len(asnList)-1]
}
return
}
}
}