Stop two identical requests from executing almost simultaneously?

I developed an AJAX-based game in which an error occurred (very remote, but in volume it occurs at least once per hour), where for some reason two requests are sent to the processing page almost simultaneously (the last one I tracked, requests were a difference of 0.0001 ms). Before the request is executed, a check is performed to make sure that it is not executed twice, but since the difference is so small, the check is not yet completed before the next request is executed. I'm at a standstill, how can I prevent this, as this causes serious problems in the game.

To be more clear, the request starts a new round in the game, so when it is executed twice, it starts 2 rounds at the same time, which breaks the game, so I need to be able to stop the script from executing if the previous round has not ended, even if This previous round started .0001 ms ago.

+3
source share
3 answers

The trick is to use the atomic update operator to accomplish something that cannot be successful for both threads. They should not both succeed, so you can simply execute the query, for example:

UPDATE gameinfo SET round=round+1 WHERE round=1234

1234 - , . . , , - . , , autocommit.

+3

, , , - . flock() sem_acquire() , - , memcached .

- , MySQL , , , , , ( - . ....

.

+1

- , - . , , .

, $round++;. .

$round=$newround; , , .

0

Source: https://habr.com/ru/post/1736832/


All Articles