Skip to content

Commit

Permalink
SDK WatchGameServer logs error on shutdown (#3271)
Browse files Browse the repository at this point in the history
* SDK WatchGameServer logs error on shutdown

* update-comment

* suggested changes in sdk.go

* added !

* Recommended modification

* change in condition

* check nil pointer for gs

---------

Co-authored-by: Mark Mandel <markmandel@google.com>
  • Loading branch information
Kalaiselvi84 and markmandel committed Jul 24, 2023
1 parent 8f575ed commit 2f8a583
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sdks/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ type SDK struct {
alpha *Alpha
}

// ErrorLog is a function to log the error.
type ErrorLog func(string, error)

// Logger is a pluggable function that outputs the error message to standard error.
var Logger ErrorLog = func(msg string, err error) {
fmt.Fprintf(os.Stderr, "%s: %s\n", msg, err)
}

// NewSDK starts a new SDK instance, and connects to localhost
// on port "AGONES_SDK_GRPC_PORT" which by default is 9357.
// Blocks until connection and handshake are made.
Expand Down Expand Up @@ -132,17 +140,22 @@ func (s *SDK) WatchGameServer(f GameServerCallback) error {
if err != nil {
return errors.Wrap(err, "could not watch gameserver")
}

log := func(gs *sdk.GameServer, msg string, err error) {
if gs == nil || gs.ObjectMeta.DeletionTimestamp == 0 {
return
}
Logger(msg, err)
}
go func() {
for {
var gs *sdk.GameServer
gs, err = stream.Recv()
if err != nil {
if err == io.EOF {
_, _ = fmt.Fprintln(os.Stderr, "gameserver event stream EOF received")
log(gs, "gameserver event stream EOF received", nil)
return
}
_, _ = fmt.Fprintf(os.Stderr, "error watching GameServer: %s\n", err.Error())
log(gs, "error watching GameServer", err)
// This is to wait for the reconnection, and not peg the CPU at 100%.
time.Sleep(time.Second)
continue
Expand Down

0 comments on commit 2f8a583

Please sign in to comment.