-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
allocator.go
69 lines (54 loc) · 1.45 KB
/
allocator.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package idallocator
import (
"fmt"
"github.com/cilium/cilium/pkg/idpool"
)
const (
minID = idpool.ID(1)
maxID = idpool.ID(4095)
)
var (
pool = idpool.NewIDPool(minID, maxID)
)
// ReallocatePool starts over with a new pool. This function is only used for
// tests and its implementation is not optimized for production.
func ReallocatePool() {
for i := uint16(minID); i <= uint16(maxID); i++ {
Release(i)
}
}
// Allocate returns a new random ID from the pool
func Allocate() uint16 {
id := pool.AllocateID()
// Out of endpoint IDs
if id == idpool.NoID {
return uint16(0)
}
return uint16(id)
}
// Reuse grabs a specific endpoint ID for reuse. This can be used when
// restoring endpoints.
func Reuse(id uint16) error {
if idpool.ID(id) < minID {
return fmt.Errorf("unable to reuse endpoint: %d < %d", id, minID)
}
// When restoring endpoints, the existing endpoint ID can be outside of
// the range. This is fine (tm) and we can just skip to reserve the ID
// from the pool as the pool will not cover it.
if idpool.ID(id) > maxID {
return nil
}
if !pool.Remove(idpool.ID(id)) {
return fmt.Errorf("endpoint ID %d is already in use", id)
}
return nil
}
// Release releases an endpoint ID that was previously allocated or reused
func Release(id uint16) error {
if !pool.Insert(idpool.ID(id)) {
return fmt.Errorf("Unable to release endpoint ID %d", id)
}
return nil
}