-
Notifications
You must be signed in to change notification settings - Fork 403
/
docker_networks.go
59 lines (48 loc) · 1.02 KB
/
docker_networks.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
package docker
import (
"math"
"strings"
"sync"
"github.com/fnproject/fn/api/agent/drivers"
)
type DockerNetworks struct {
// protects networks map
networksLock sync.Mutex
networks map[string]uint64
}
func NewDockerNetworks(conf drivers.Config) *DockerNetworks {
obj := &DockerNetworks{
networks: make(map[string]uint64),
}
// Record DockerNetworks both in wait-list and in network allocations
for _, net := range strings.Fields(conf.DockerNetworks) {
obj.networks[net] = 0
}
return obj
}
// pick least used network
func (n *DockerNetworks) AllocNetwork() string {
if len(n.networks) == 0 {
return ""
}
var id string
min := uint64(math.MaxUint64)
n.networksLock.Lock()
for key, val := range n.networks {
if val < min {
id = key
min = val
}
}
n.networks[id]++
n.networksLock.Unlock()
return id
}
// unregister network
func (n *DockerNetworks) FreeNetwork(id string) {
n.networksLock.Lock()
if count, ok := n.networks[id]; ok {
n.networks[id] = count - 1
}
n.networksLock.Unlock()
}