Skip to content

Commit 98fcf1f

Browse files
author
Robert Bailey
authored
Update the simple game server to avoid a race condition when (agones-dev#2451)
transitioning from ready to allocated.
1 parent 32d22cf commit 98fcf1f

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎examples/simple-game-server/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ WITH_WINDOWS=1
3535

3636
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
3737
project_path := $(dir $(mkfile_path))
38-
server_tag = $(REGISTRY)/simple-game-server:0.8
38+
server_tag = $(REGISTRY)/simple-game-server:0.9
3939
ifeq ($(WITH_WINDOWS), 1)
4040
server_tag_linux_amd64 = $(server_tag)-linux_amd64
4141
else

‎examples/simple-game-server/main.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ func shutdownAfterNAllocations(s *sdk.SDK, readyIterations, shutdownDelaySec int
153153
log.Printf("Initial game Server state = %s", gs.Status.State)
154154

155155
m := sync.Mutex{} // protects the following two variables
156-
currentState := gs.Status.State
156+
lastAllocated := gs.ObjectMeta.Annotations["agones.dev/last-allocated"]
157157
remainingIterations := readyIterations
158158

159159
if err := s.WatchGameServer(func(gs *coresdk.GameServer) {
160160
m.Lock()
161161
defer m.Unlock()
162-
log.Printf("Watch Game Server callback fired. State = %s", gs.Status.State)
163-
if currentState == "Ready" && gs.Status.State == "Allocated" {
162+
la := gs.ObjectMeta.Annotations["agones.dev/last-allocated"]
163+
log.Printf("Watch Game Server callback fired. State = %s, Last Allocated = %q", gs.Status.State, la)
164+
if lastAllocated != la {
164165
log.Println("Game Server Allocated")
166+
lastAllocated = la
165167
remainingIterations--
166168
// Run asynchronously
167169
go func(iterations int) {
@@ -173,18 +175,19 @@ func shutdownAfterNAllocations(s *sdk.SDK, readyIterations, shutdownDelaySec int
173175
if readyErr != nil {
174176
log.Fatalf("Could not set game server to ready: %v", readyErr)
175177
}
178+
log.Println("Game Server is Ready")
176179
return
177180
}
178181

179-
log.Println("Shutting down Game Server")
182+
log.Println("Moving Game Server to Shutdown")
180183
shutdownErr := s.Shutdown()
181184
if shutdownErr != nil {
182185
log.Fatalf("Could not shutdown game server: %v", shutdownErr)
183186
}
187+
log.Println("Exiting Game Server")
184188
os.Exit(0)
185189
}(remainingIterations)
186190
}
187-
currentState = gs.Status.State
188191
}); err != nil {
189192
log.Fatalf("Could not watch Game Server events, %v", err)
190193
}

0 commit comments

Comments
 (0)