Answer the question
In order to leave comments, you need to log in
How to set up the server and code for multiple simultaneous requests?
I'll write it in order. Three points: Task, My crutch solution (problems) and Question.
Task:
id|parent_record_id|user_id
2 |_______________1|_____45
3 |_______________2|_____32
4 |_______________3|_____98
DB::beginTransaction(); // начинается неразрывная транзакция
$checkLine = Users::where('line_is_busy', '=', 1)->count(); // проверяем занята ли линия
if ($checkLine) { // если да то кидаем ошибку
return "Линия занята, кликните еще раз, вдруг вам повезет попасть, а то разработчик не знает как тут реализовать грамотно очередь";
}
// если линия свободна то занимаем ее
Users::whereId($user->id)->update(['line_is_busy' => 1]);
//
// здесь выполняется тот скрипт на 0.1-0.6 секунд
//
Users::whereId($user->id)->update(['line_is_busy' => 0]); // освободили линию
DB::commit();
Answer the question
In order to leave comments, you need to log in
Possible implementation option (one of the possible):
When you click on the button:
- block the button
- display the inscription "wait for a response"
- AJAX - request to the server
- on the server we put the request in the queue
, the client either waits for the request from the queue to be executed or does not wait, but periodically checks the status to unlock the button and show "OK"
Instead of AJAX, you can fasten a WEB-socket, then the server will be able to notify the client about the end of the operation.
On the server, provide a request check for uniqueness so that one user does not block the system with many requests, etc.
start transaction, lock table exclusively, perform
operations, commit transaction
https://dev.mysql.com/doc/refman/8.0/en/lock-table...
In principle, the blocking and waiting on it will only be for the duration of the transaction, which should be much less than the total execution time of the script
Perhaps it is worth looking towards semaphores, the same mutex for example.
It can be implemented through a database, or through a file or, for example, through redis... Have you
considered it?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question