github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/port.go (about)

     1  package rpc
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  )
     7  
     8  var portRangeMin int = 10000
     9  var portRangeMax int = 11000
    10  
    11  // This sets the port range that the RPC stuff will use when creating
    12  // new temporary servers. Some RPC calls require the creation of temporary
    13  // RPC servers. These allow you to pick a range these bind to.
    14  func PortRange(min, max int) {
    15  	portRangeMin = min
    16  	portRangeMax = max
    17  }
    18  
    19  // This finds an open port in the given range and returns a listener
    20  // bound to that port.
    21  func netListenerInRange(min, max int) net.Listener {
    22  	for port := min; port <= max; port++ {
    23  		l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
    24  		if err == nil {
    25  			return l
    26  		}
    27  	}
    28  
    29  	return nil
    30  }