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
package adjRIBOutAddPath
import (
"fmt"
)
var maxUint32 = ^uint32(0)
// pathIDManager manages BGP path identifiers for add-path. This is no thread safe (and doesn't need to be).
type pathIDManager struct {
ids map[uint32]struct{}
last uint32
used uint32
}
func newPathIDManager() *pathIDManager {
return &pathIDManager{
ids: make(map[uint32]struct{}),
}
}
func (fm *pathIDManager) getNewID() (uint32, error) {
if fm.used == maxUint32 {
return 0, fmt.Errorf("Out of path IDs")
}
fm.last++
for {
if _, exists := fm.ids[fm.last]; exists {
fm.last++
continue
}
break
}
ret := fm.last
fm.used++
return ret, nil
}
func (fm *pathIDManager) releaseID(id uint32) {
if _, exists := fm.ids[id]; exists {
delete(fm.ids, id)
fm.used--
}
}