Skip to content

Commit

Permalink
Improve timeout handling for character creation and deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
walkline committed Jan 9, 2024
1 parent e4acbc6 commit 556c7c6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions apps/game-load-balancer/session/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ func (s *GameSession) CreateCharacter(ctx context.Context, p *packet.Packet) err

go socket.ListenAndProcess(s.ctx)
newCtx, cancel := context.WithTimeout(s.ctx, time.Second*5)
defer cancel()

waitDone := make(chan struct{})
go func() {
defer cancel()
defer func() { waitDone <- struct{}{} }()

for {
Expand Down Expand Up @@ -144,6 +145,13 @@ func (s *GameSession) CreateCharacter(ctx context.Context, p *packet.Packet) err

<-waitDone

select {
case <-newCtx.Done():
sendCreateFailed()
return fmt.Errorf("character creation timeouted, gameserver: %s", serverResult.GameServer.Address)
default:
}

return nil
}

Expand Down Expand Up @@ -177,9 +185,10 @@ func (s *GameSession) DeleteCharacter(ctx context.Context, p *packet.Packet) err

go socket.ListenAndProcess(s.ctx)
newCtx, cancel := context.WithTimeout(s.ctx, time.Second*5)
defer cancel()

waitDone := make(chan struct{})
go func() {
defer cancel()
defer func() { waitDone <- struct{}{} }()

for {
Expand Down Expand Up @@ -212,8 +221,15 @@ func (s *GameSession) DeleteCharacter(ctx context.Context, p *packet.Packet) err

<-waitDone

select {
case <-newCtx.Done():
sendDelFailed()
return fmt.Errorf("character deletion timeouted, gameserver: %s", serverResult.GameServer.Address)
default:
}

// Let's wait some moment because delete command may take some time on worldserver side.
time.Sleep(time.Second * 2)
time.Sleep(time.Second * 1)

return nil
}

0 comments on commit 556c7c6

Please sign in to comment.