jonpchin / gochess

Online real time chess web server using websockets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Games restoring

inmylo opened this issue · comments

Hi,

I've looked a little at your project, but didn't found one thing. What do you do when your service crashes and is started again? I see you have a function that reads saved games, but what if there was a game with a time limit (for example 15 mins), every player has spent 5 mins, and then the service has crashed (no matter why)? After the service is running again - how many minutes both players still will have?

Another question is if I am a player, I've spent 5 of 15 mins, service has crashed, then it was restored after 1 hour, but I will notice this after 2 hours or not at all (in any case my 10 mins are lost) - how do you handle this?

Thanks

commented

hi imilo,

The current implementation I have is if there are players currently in a game and I want to shut down the web server in order to do a code update or do maintenance I could send a kill signal(SIGTERM) by pressing Crtl-C which initiates a graceful shutdown, notifies all users there is an immediate shutdown and will save all current games in memory to the database before shutting down the web server.

The time controls are saved into the database as well so if someone spent 5 minutes and 2 seconds of their 15 minutes then those exact minutes and seconds will be saved in the database so when the players resume the game, they will start again with the time they had before the web server was shut down. In order to resume playing the games that were adjourned both players need to be present and logged on the site, one person in the lobby and one person in adjourned profile page initiating the request to resume the unfinished game. Thus the clock does not count down on adjourned games until both players are ready.

Thanks for a response!

And what if the second player never comes back? The first one can only wait?

Another scenario is when a service wasn't shut down manually, but after a critical error or server has lost power (no chance to save data). Probably game still will be restored, but what about timing?

commented

And what if the second player never comes back? The first one can only wait?

If the second player never comes back then the first player will have to wait. Adjourned games are removed from the database after a certain period of time, currently I've set it to 180 days.

Another way professional chess sites handle this situation is using game adjudication with a chess engine or a site admin. This feature is not implemented yet in Go Play Chess but it may one day. Adjudication is when a game has been sitting in adjourned in the database for an extended period of time say over 2 weeks and the second player was in a clearly superior position with a huge lead in material or an impending checkmate then a chess engine could analyze the position or a moderator could confirm the game is won for the second player and the ratings and game history will be adjusted as if the second player won the game.

If the position is close to a tie, unclear, or cannot be determined who is winning then nothing will be done to the game and it will sit in the adjourned state in the database until the expiration date or when the players resume the game.

Another scenario is when a service wasn't shut down manually, but after a critical error or server has >lost power (no chance to save data). Probably game still will be restored, but what about timing?

I try to avoid a critical error or unexpected shutdowns of the web server. This can happen if the machine I am hosting my web server crashes or if there is a race condition in my Golang code. I've seen the graceful shutdown successfully save all chess games and their minutes/seconds into the database with no problem when these types of "unexpected crashes" occur but not always. If the entire machine crashes I do not know what will happen because I have not done much testing on it but I believe the games will not be saved in this scenario. The best way to deal with this type of crash is to periodically save games into the database after a certain time has passed, say every few minutes, like how Microsoft Word auto saves after a certain amount of time.