From 69f1eddd56207cd87ae2a7c9568badb6bf3cd787 Mon Sep 17 00:00:00 2001 From: tjmehta Date: Mon, 31 Mar 2014 22:21:52 -0700 Subject: [PATCH] make findNextPort circular, add all-ports-allocated error Docker-DCO-1.1-Signed-off-by: Tejesh Mehta (github: tjmehta) Upstream-commit: 739d1244807bc3522a0af4dc3490305d6f037601 Component: engine --- .../networkdriver/portallocator/portallocator.go | 12 ++++++++---- .../portallocator/portallocator_test.go | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/components/engine/runtime/networkdriver/portallocator/portallocator.go b/components/engine/runtime/networkdriver/portallocator/portallocator.go index 4d698f2de2..9ecd447116 100644 --- a/components/engine/runtime/networkdriver/portallocator/portallocator.go +++ b/components/engine/runtime/networkdriver/portallocator/portallocator.go @@ -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") ) @@ -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 } diff --git a/components/engine/runtime/networkdriver/portallocator/portallocator_test.go b/components/engine/runtime/networkdriver/portallocator/portallocator_test.go index f01bcfc99e..8b4062c37c 100644 --- a/components/engine/runtime/networkdriver/portallocator/portallocator_test.go +++ b/components/engine/runtime/networkdriver/portallocator/portallocator_test.go @@ -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)