mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Make sure to close the network allocators
This commit is contained in:
parent
2d425af1b1
commit
9107565d06
3 changed files with 48 additions and 4 deletions
47
network.go
47
network.go
|
@ -336,13 +336,20 @@ func newPortMapper() (*PortMapper, error) {
|
||||||
type PortAllocator struct {
|
type PortAllocator struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
inUse map[int]struct{}
|
inUse map[int]struct{}
|
||||||
fountain chan (int)
|
fountain chan int
|
||||||
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (alloc *PortAllocator) runFountain() {
|
func (alloc *PortAllocator) runFountain() {
|
||||||
for {
|
for {
|
||||||
for port := portRangeStart; port < portRangeEnd; port++ {
|
for port := portRangeStart; port < portRangeEnd; port++ {
|
||||||
alloc.fountain <- port
|
select {
|
||||||
|
case alloc.fountain <- port:
|
||||||
|
case quit := <-alloc.quit:
|
||||||
|
if quit {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -376,10 +383,18 @@ func (alloc *PortAllocator) Acquire(port int) (int, error) {
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (alloc *PortAllocator) Close() error {
|
||||||
|
alloc.quit <- true
|
||||||
|
close(alloc.quit)
|
||||||
|
close(alloc.fountain)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func newPortAllocator() (*PortAllocator, error) {
|
func newPortAllocator() (*PortAllocator, error) {
|
||||||
allocator := &PortAllocator{
|
allocator := &PortAllocator{
|
||||||
inUse: make(map[int]struct{}),
|
inUse: make(map[int]struct{}),
|
||||||
fountain: make(chan int),
|
fountain: make(chan int),
|
||||||
|
quit: make(chan bool),
|
||||||
}
|
}
|
||||||
go allocator.runFountain()
|
go allocator.runFountain()
|
||||||
return allocator, nil
|
return allocator, nil
|
||||||
|
@ -391,6 +406,7 @@ type IPAllocator struct {
|
||||||
queueAlloc chan allocatedIP
|
queueAlloc chan allocatedIP
|
||||||
queueReleased chan net.IP
|
queueReleased chan net.IP
|
||||||
inUse map[int32]struct{}
|
inUse map[int32]struct{}
|
||||||
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type allocatedIP struct {
|
type allocatedIP struct {
|
||||||
|
@ -435,6 +451,10 @@ func (alloc *IPAllocator) run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
case quit := <-alloc.quit:
|
||||||
|
if quit {
|
||||||
|
return
|
||||||
|
}
|
||||||
case alloc.queueAlloc <- ip:
|
case alloc.queueAlloc <- ip:
|
||||||
alloc.inUse[newNum] = struct{}{}
|
alloc.inUse[newNum] = struct{}{}
|
||||||
case released := <-alloc.queueReleased:
|
case released := <-alloc.queueReleased:
|
||||||
|
@ -467,12 +487,21 @@ func (alloc *IPAllocator) Release(ip net.IP) {
|
||||||
alloc.queueReleased <- ip
|
alloc.queueReleased <- ip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (alloc *IPAllocator) Close() error {
|
||||||
|
alloc.quit <- true
|
||||||
|
close(alloc.quit)
|
||||||
|
close(alloc.queueAlloc)
|
||||||
|
close(alloc.queueReleased)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func newIPAllocator(network *net.IPNet) *IPAllocator {
|
func newIPAllocator(network *net.IPNet) *IPAllocator {
|
||||||
alloc := &IPAllocator{
|
alloc := &IPAllocator{
|
||||||
network: network,
|
network: network,
|
||||||
queueAlloc: make(chan allocatedIP),
|
queueAlloc: make(chan allocatedIP),
|
||||||
queueReleased: make(chan net.IP),
|
queueReleased: make(chan net.IP),
|
||||||
inUse: make(map[int32]struct{}),
|
inUse: make(map[int32]struct{}),
|
||||||
|
quit: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
go alloc.run()
|
go alloc.run()
|
||||||
|
@ -662,6 +691,19 @@ func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
|
||||||
return iface, nil
|
return iface, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (manager *NetworkManager) Close() error {
|
||||||
|
err1 := manager.tcpPortAllocator.Close()
|
||||||
|
err2 := manager.udpPortAllocator.Close()
|
||||||
|
err3 := manager.ipAllocator.Close()
|
||||||
|
if err1 != nil {
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
if err2 != nil {
|
||||||
|
return err2
|
||||||
|
}
|
||||||
|
return err3
|
||||||
|
}
|
||||||
|
|
||||||
func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
|
func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
|
||||||
|
|
||||||
if bridgeIface == DisableNetworkBridge {
|
if bridgeIface == DisableNetworkBridge {
|
||||||
|
@ -708,5 +750,6 @@ func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
|
||||||
udpPortAllocator: udpPortAllocator,
|
udpPortAllocator: udpPortAllocator,
|
||||||
portMapper: portMapper,
|
portMapper: portMapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
return manager, nil
|
return manager, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,10 +61,9 @@ func (proxy *TCPProxy) clientLoop(client *net.TCPConn, quit chan bool) {
|
||||||
var broker = func(to, from *net.TCPConn) {
|
var broker = func(to, from *net.TCPConn) {
|
||||||
written, err := io.Copy(to, from)
|
written, err := io.Copy(to, from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err, ok := err.(*net.OpError)
|
|
||||||
// If the socket we are writing to is shutdown with
|
// If the socket we are writing to is shutdown with
|
||||||
// SHUT_WR, forward it to the other end of the pipe:
|
// SHUT_WR, forward it to the other end of the pipe:
|
||||||
if ok && err.Err == syscall.EPIPE {
|
if err, ok := err.(*net.OpError); ok && err.Err == syscall.EPIPE {
|
||||||
from.CloseWrite()
|
from.CloseWrite()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,6 +98,7 @@ done:
|
||||||
func (proxy *TCPProxy) Run() {
|
func (proxy *TCPProxy) Run() {
|
||||||
quit := make(chan bool)
|
quit := make(chan bool)
|
||||||
defer close(quit)
|
defer close(quit)
|
||||||
|
|
||||||
utils.Debugf("Starting proxy on tcp/%v for tcp/%v", proxy.frontendAddr, proxy.backendAddr)
|
utils.Debugf("Starting proxy on tcp/%v for tcp/%v", proxy.frontendAddr, proxy.backendAddr)
|
||||||
for {
|
for {
|
||||||
client, err := proxy.listener.Accept()
|
client, err := proxy.listener.Accept()
|
||||||
|
|
|
@ -42,6 +42,7 @@ func nuke(runtime *Runtime) error {
|
||||||
}(container)
|
}(container)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
runtime.networkManager.Close()
|
||||||
return os.RemoveAll(runtime.root)
|
return os.RemoveAll(runtime.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue