H
H
hesy2020-05-04 13:33:44
PHP
hesy, 2020-05-04 13:33:44

PHP sleeps between requests, how to wake it up?

  • There is a certain bot which accepts the message (request) from the user.
  • Adds information that a request has arrived and puts a lock on subsequent requests (using memcached)
  • Performs 10 GET requests to the Google API, and since this is Google and the method is not entirely fair, therefore there is a limit between requests to the API usingsleep(1)
  • Works with the database (here, by the way, information about the lock is also stored and is checked against the cache + database when requested)


As a result, ~10 seconds elapse between the user sending a message and the bot's response .

According to the idea, since there is a block on subsequent requests, it is logical that the next response to the request that the user will receive will be:
"Sorry, please wait for the completion of the previous operation."

BUT! If the bot is resting (not actively accepting requests) and then abruptly throwing a request and then 10 more pieces, then it takes the first request to work, and does not respond to the next one. And as soon as the first request is completed, he takes the SECOND into work and already on the NEXT 8 he says "Sorry, wait."
If you continue to send it in the same way, it works as intended, but it's worth giving it a rest, the same story.

I don't understand why this is happening, is this a feature of Hosting/PHP and there's nothing to be done about it?
Slightly neutered code

<?php

if ($bot->isMessage() && !$bot->isCommand()) {
    if ($bot->state_name == 'process') {
        $bot->reply('Please, wait until the previous search is complete.');
        die;
    } else {
        $bot->setState('process');
    }
    
    $GLOBAL_STATUS = $bot->cache->get('GLOBAL_STATUS');
    if ($GLOBAL_STATUS == 'process') {
        $bot->clearState();
        $bot->reply('Queue busy, please try again later.');
        die;
    } else {
        $bot->cache->add('GLOBAL_STATUS', 'process', 60);
    }

    $bot->hear(['{default}'], function() use($bot) {
        $bot->reply(' Search, it takes ~10 seconds...');

        $q = $bot->message;

        // здесь как раз 10 итераций интервалом в sleep(1)
        $data = search($q, 100, $bot->user->data['sort']);

        $bot->say('OK, search completed.');
    });
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AUser0, 2020-05-04
@AUser0

So you have a logic failure in your code, the situation is processed incorrectly ...
That's it, the problem is solved?
PS And what do you want, what is the question - such is the answer.
Without full source codes, no one will point a finger at the error.

H
hesy, 2020-05-07
@hesy

In short, I solved the issue by rewriting the bot in Node.js.
A bot with a long pool generally flies, unlike PHP with a webhook, I'm very surprised.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question