Skip to content

Commit

Permalink
Fix Xonotic example race condition
Browse files Browse the repository at this point in the history
There was a race condition in the original
implementation. This fixes that issue!

(and makes the code cleaner!)
  • Loading branch information
markmandel authored and Cyril TOVENA committed Jun 17, 2018
1 parent 9ee599b commit 18f69a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/xonotic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ REPOSITORY = gcr.io/agones-images
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
agones_path := $(realpath $(project_path)/../..)
image_tag = $(REPOSITORY)/xonotic-example:0.2
image_tag = $(REPOSITORY)/xonotic-example:0.3

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
Expand Down
2 changes: 1 addition & 1 deletion examples/xonotic/fleet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ spec:
spec:
containers:
- name: xonotic
image: gcr.io/agones-images/xonotic-example:0.2
image: gcr.io/agones-images/xonotic-example:0.3
2 changes: 1 addition & 1 deletion examples/xonotic/gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ spec:
spec:
containers:
- name: xonotic
image: gcr.io/agones-images/xonotic-example:0.2
image: gcr.io/agones-images/xonotic-example:0.3
# imagePullPolicy: Always # add for development
41 changes: 17 additions & 24 deletions examples/xonotic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ import (
"agones.dev/agones/sdks/go"
)

const (
begin = "BEGIN"
configLoaded = "CONFIGLOADED"
listening = "LISTENING"
)

type interceptor struct {
forward io.Writer
intercept func(p []byte)
Expand Down Expand Up @@ -66,34 +60,33 @@ func main() {
fmt.Println(">>> Starting wrapper for Xonotic!")
fmt.Printf(">>> Path to Xonotic server script: %s \n", *input)

// state tracks the state
state := begin
// track references to listening count
listeningCount := 0

cmd := exec.Command(*input) // #nosec
cmd.Stderr = &interceptor{forward: os.Stderr}
cmd.Stdout = &interceptor{
forward: os.Stdout,
intercept: func(p []byte) {
if state == listening {
if listeningCount >= 4 {
return
}

str := strings.TrimSpace(string(p))
// since the game server starts, then loads the server.cfg
// and then restarts itself, we need to wait for this line
// before we look for "Server listening on address", since
// it is output twice.
if state == begin && str == "execing server.cfg" {
fmt.Printf(">>> Moving to configLoaded: %s", str)
state = configLoaded
return
}
if state == configLoaded && strings.Contains(str, "Server listening on address") {
state = listening
fmt.Printf(">>> Moving to listening: %s", str)
err = s.Ready()
if err != nil {
log.Fatalf("Could not send ready message")
// Xonotic will say "Server listening" 4 times before being ready,
// once for ipv4 and once for ipv6.
// but it does it each twice because it loads the maps between
// each one, and resets state as it does so
if count := strings.Count(str, "Server listening on address"); count > 0 {
listeningCount += count
fmt.Printf(">>> Found 'listening' statement: %d \n", listeningCount)

if listeningCount == 4 {
fmt.Printf(">>> Moving to READY: %s \n", str)
err = s.Ready()
if err != nil {
log.Fatalf("Could not send ready message")
}
}
}
}}
Expand Down

0 comments on commit 18f69a1

Please sign in to comment.