Browse Source

portallocator: minor refactor for readability

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 4 năm trước cách đây
mục cha
commit
1f398f06aa
1 tập tin đã thay đổi với 6 bổ sung10 xóa
  1. 6 10
      libnetwork/portallocator/portallocator.go

+ 6 - 10
libnetwork/portallocator/portallocator.go

@@ -35,7 +35,6 @@ var (
 	defaultIP          = net.ParseIP("0.0.0.0")
 	once               sync.Once
 	instance           *PortAllocator
-	createInstance     = func() { instance = newInstance() }
 )
 
 // ErrPortAlreadyAllocated is the returned error information when a requested port is already being used
@@ -99,7 +98,9 @@ func Get() *PortAllocator {
 	// the OS so that it can have up to date view of the OS port allocation.
 	// When this happens singleton behavior will be removed. Clients do not
 	// need to worry about this, they will not see a change in behavior.
-	once.Do(createInstance)
+	once.Do(func() {
+		instance = newInstance()
+	})
 	return instance
 }
 
@@ -199,15 +200,10 @@ func (p *PortAllocator) SetPortRange(portBegin, portEnd int) error {
 	var err error
 	if portBegin == 0 && portEnd == 0 {
 		begin, end = getDefaultPortRange()
-
-	} else {
-		begin, end, err = sanitizePortRange(portBegin, portEnd)
-		if err != nil {
-			return err
-		}
+	} else if begin, end, err = sanitizePortRange(portBegin, portEnd); err != nil {
+		return err
 	}
-	logrus.Debugf("Setting up port allocator to range %v-%v, current %v-%v",
-		begin, end, p.Begin, p.End)
+	logrus.Debugf("Setting up port allocator to range %v-%v, current %v-%v", begin, end, p.Begin, p.End)
 	p.mutex.Lock()
 	defer p.mutex.Unlock()
 	if p.Begin == begin && p.End == end {