Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic port allocation limit #4949

Merged
merged 1 commit into from
Apr 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions runtime/networkdriver/portallocator/portallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type (
)

var (
ErrAllPortsAllocated = errors.New("all ports are allocated")
ErrPortAlreadyAllocated = errors.New("port has already been allocated")
ErrPortExceedsRange = errors.New("port exceeds upper range")
ErrUnknownProtocol = errors.New("unknown protocol")
)

Expand Down Expand Up @@ -152,17 +152,21 @@ func equalsDefault(ip net.IP) bool {

func findNextPort(proto string, allocated *collections.OrderedIntSet) (int, error) {
port := nextPort(proto)
startSearchPort := port
for allocated.Exists(port) {
port = nextPort(proto)
}
if port > EndPortRange {
return 0, ErrPortExceedsRange
if startSearchPort == port {
return 0, ErrAllPortsAllocated
}
}
return port, nil
}

func nextPort(proto string) int {
c := currentDynamicPort[proto] + 1
if c > EndPortRange {
c = BeginPortRange
}
currentDynamicPort[proto] = c
return c
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/networkdriver/portallocator/portallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func TestAllocateAllPorts(t *testing.T) {
}
}

if _, err := RequestPort(defaultIP, "tcp", 0); err != ErrPortExceedsRange {
t.Fatalf("Expected error %s got %s", ErrPortExceedsRange, err)
if _, err := RequestPort(defaultIP, "tcp", 0); err != ErrAllPortsAllocated {
t.Fatalf("Expected error %s got %s", ErrAllPortsAllocated, err)
}

_, err := RequestPort(defaultIP, "udp", 0)
Expand Down