Fix races in set/get currentInterfaces in networkdriver

Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
This commit is contained in:
Alexandr Morozov 2014-05-29 16:28:06 +04:00
parent ce9e9ff4a1
commit 4e5f6951f2

View file

@ -6,6 +6,7 @@ import (
"log"
"net"
"strings"
"sync"
"github.com/dotcloud/docker/daemon/networkdriver"
"github.com/dotcloud/docker/daemon/networkdriver/ipallocator"
@ -28,6 +29,24 @@ type networkInterface struct {
PortMappings []net.Addr // there are mappings to the host interfaces
}
type ifaces struct {
c map[string]*networkInterface
sync.Mutex
}
func (i *ifaces) Set(key string, n *networkInterface) {
i.Lock()
i.c[key] = n
i.Unlock()
}
func (i *ifaces) Get(key string) *networkInterface {
i.Lock()
res := i.c[key]
i.Unlock()
return res
}
var (
addrs = []string{
// Here we don't follow the convention of using the 1st IP of the range for the gateway.
@ -53,7 +72,7 @@ var (
bridgeNetwork *net.IPNet
defaultBindingIP = net.ParseIP("0.0.0.0")
currentInterfaces = make(map[string]*networkInterface)
currentInterfaces = ifaces{c: make(map[string]*networkInterface)}
)
func InitDriver(job *engine.Job) engine.Status {
@ -321,9 +340,9 @@ func Allocate(job *engine.Job) engine.Status {
size, _ := bridgeNetwork.Mask.Size()
out.SetInt("IPPrefixLen", size)
currentInterfaces[id] = &networkInterface{
currentInterfaces.Set(id, &networkInterface{
IP: *ip,
}
})
out.WriteTo(job.Stdout)
@ -334,7 +353,7 @@ func Allocate(job *engine.Job) engine.Status {
func Release(job *engine.Job) engine.Status {
var (
id = job.Args[0]
containerInterface = currentInterfaces[id]
containerInterface = currentInterfaces.Get(id)
ip net.IP
port int
proto string
@ -383,7 +402,7 @@ func AllocatePort(job *engine.Job) engine.Status {
origHostPort = job.GetenvInt("HostPort")
containerPort = job.GetenvInt("ContainerPort")
proto = job.Getenv("Proto")
network = currentInterfaces[id]
network = currentInterfaces.Get(id)
)
if hostIP != "" {