|
@@ -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
|
|
}
|
|
}
|