mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #4949 from tjmehta/4908-fix_dynamic_port_allocation_limit
Fix dynamic port allocation limit
This commit is contained in:
commit
7462cc6479
2 changed files with 10 additions and 6 deletions
|
@ -18,8 +18,8 @@ type (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
ErrAllPortsAllocated = errors.New("all ports are allocated")
|
||||||
ErrPortAlreadyAllocated = errors.New("port has already been allocated")
|
ErrPortAlreadyAllocated = errors.New("port has already been allocated")
|
||||||
ErrPortExceedsRange = errors.New("port exceeds upper range")
|
|
||||||
ErrUnknownProtocol = errors.New("unknown protocol")
|
ErrUnknownProtocol = errors.New("unknown protocol")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -152,17 +152,21 @@ func equalsDefault(ip net.IP) bool {
|
||||||
|
|
||||||
func findNextPort(proto string, allocated *collections.OrderedIntSet) (int, error) {
|
func findNextPort(proto string, allocated *collections.OrderedIntSet) (int, error) {
|
||||||
port := nextPort(proto)
|
port := nextPort(proto)
|
||||||
|
startSearchPort := port
|
||||||
for allocated.Exists(port) {
|
for allocated.Exists(port) {
|
||||||
port = nextPort(proto)
|
port = nextPort(proto)
|
||||||
}
|
if startSearchPort == port {
|
||||||
if port > EndPortRange {
|
return 0, ErrAllPortsAllocated
|
||||||
return 0, ErrPortExceedsRange
|
}
|
||||||
}
|
}
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextPort(proto string) int {
|
func nextPort(proto string) int {
|
||||||
c := currentDynamicPort[proto] + 1
|
c := currentDynamicPort[proto] + 1
|
||||||
|
if c > EndPortRange {
|
||||||
|
c = BeginPortRange
|
||||||
|
}
|
||||||
currentDynamicPort[proto] = c
|
currentDynamicPort[proto] = c
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,8 +110,8 @@ func TestAllocateAllPorts(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := RequestPort(defaultIP, "tcp", 0); err != ErrPortExceedsRange {
|
if _, err := RequestPort(defaultIP, "tcp", 0); err != ErrAllPortsAllocated {
|
||||||
t.Fatalf("Expected error %s got %s", ErrPortExceedsRange, err)
|
t.Fatalf("Expected error %s got %s", ErrAllPortsAllocated, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := RequestPort(defaultIP, "udp", 0)
|
_, err := RequestPort(defaultIP, "udp", 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue