Skip to content

Commit

Permalink
Simplify the selection of the dynamic port in the Go sdk. (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberthbailey authored Oct 21, 2019
1 parent 15ce0a1 commit 1071930
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions sdks/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"os"
"strconv"
"time"

"agones.dev/agones/pkg/sdk"
Expand All @@ -28,8 +27,6 @@ import (
"google.golang.org/grpc"
)

const defaultPort = 59357

// GameServerCallback is a function definition to be called
// when a GameServer CRD has been changed
type GameServerCallback func(gs *sdk.GameServer)
Expand All @@ -41,32 +38,15 @@ type SDK struct {
health sdk.SDK_HealthClient
}

func port() int {
portStr := os.Getenv("AGONES_SDK_GRPC_PORT")
if portStr == "" {
// Environment variable is not set; use the default port.
_, _ = fmt.Fprintf(os.Stderr, "Environment variable AGONES_SDK_GRPC_PORT not defined, using default port %d\n", defaultPort)
return defaultPort
}
p, err := strconv.Atoi(portStr)
if err != nil {
// Environment variable cannot be parsed; use the default port.
_, _ = fmt.Fprintf(os.Stderr, "Unable to parse %q defined in AGONES_SDK_GRPC_PORT into an integer\n", portStr)
return defaultPort
}
if p < 1 || p > 65535 {
// Environment variable is not a valid port; use the default port.
_, _ = fmt.Fprintf(os.Stderr, "Invalid port %d defined in AGONES_SDK_GRPC_PORT. It must be between 1 and 65535\n", p)
return defaultPort
}
return p
}

// NewSDK starts a new SDK instance, and connects to
// localhost on port 59357. Blocks until connection and handshake are made.
// Times out after 30 seconds.
func NewSDK() (*SDK, error) {
addr := fmt.Sprintf("localhost:%d", port())
p := os.Getenv("AGONES_SDK_GRPC_PORT")
if p == "" {
p = "59357"
}
addr := fmt.Sprintf("localhost:%s", p)
s := &SDK{
ctx: context.Background(),
}
Expand Down

0 comments on commit 1071930

Please sign in to comment.