Skip to content

Commit

Permalink
listener/tcp: better handling of min/max port env vars (#111)
Browse files Browse the repository at this point in the history
* listener/tcp: better handling of min/max port env vars

* wrap errors to make the return value more indicative

* docs: add references to the port env vars
  • Loading branch information
calvn committed May 9, 2019
1 parent 5692942 commit a1756f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
13 changes: 13 additions & 0 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ Where:
is omitted (older versions), this is "netrpc" for Go net/rpc. This can
also be "grpc". This is the protocol that the plugin wants to speak to
the host process with.

## Environment Variables

When serving a plugin over TCP, the following environment variables can be
specified to restrict the port that will be assigned to be from within a
specific range. If not values are provided, the port will be randomly assigned
by the operating system.

* `PLUGIN_MIN_PORT`: Specifies the minimum port value that will be assigned to
* the listener.

* `PLUGIN_MAX_PORT`: Specifies the maximum port value that will be assigned to
* the listener.
32 changes: 26 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,34 @@ func serverListener() (net.Listener, error) {
}

func serverListener_tcp() (net.Listener, error) {
minPort, err := strconv.ParseInt(os.Getenv("PLUGIN_MIN_PORT"), 10, 32)
if err != nil {
return nil, err
envMinPort := os.Getenv("PLUGIN_MIN_PORT")
envMaxPort := os.Getenv("PLUGIN_MAX_PORT")

var minPort, maxPort int64
var err error

switch {
case len(envMinPort) == 0:
minPort = 0
default:
minPort, err = strconv.ParseInt(envMinPort, 10, 32)
if err != nil {
return nil, fmt.Errorf("Couldn't get value from PLUGIN_MIN_PORT: %v", err)
}
}

maxPort, err := strconv.ParseInt(os.Getenv("PLUGIN_MAX_PORT"), 10, 32)
if err != nil {
return nil, err
switch {
case len(envMaxPort) == 0:
maxPort = 0
default:
maxPort, err = strconv.ParseInt(envMaxPort, 10, 32)
if err != nil {
return nil, fmt.Errorf("Couldn't get value from PLUGIN_MAX_PORT: %v", err)
}
}

if minPort > maxPort {
return nil, fmt.Errorf("ENV_MIN_PORT value of %d is greater than PLUGIN_MAX_PORT value of %d", minPort, maxPort)
}

for port := minPort; port <= maxPort; port++ {
Expand Down

0 comments on commit a1756f3

Please sign in to comment.