Commit ac29f07a authored by Jack'lul's avatar Jack'lul

Some changes...

parent fe988b7f
...@@ -1071,15 +1071,18 @@ class DB ...@@ -1071,15 +1071,18 @@ class DB
try { try {
$sth = self::$pdo->prepare('SELECT $sth = self::$pdo->prepare('SELECT
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE ((`chat_id` = :chat_id AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL)) AND `created_at` >= :date) as CURRENT, (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :date) as LIMIT_PER_SEC_ALL,
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :date) as TOTAL (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE ((`chat_id` = :chat_id AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL)) AND `created_at` >= :date) as LIMIT_PER_SEC,
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE
'); ');
$date = self::getTimestamp(); $date = self::getTimestamp(time());
$date_minute = self::getTimestamp(strtotime('-1 minute'));
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); $sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR);
$sth->bindParam(':date', $date, \PDO::PARAM_STR); $sth->bindParam(':date', $date, \PDO::PARAM_STR);
$sth->bindParam(':date_minute', $date_minute, \PDO::PARAM_STR);
$sth->execute(); $sth->execute();
......
...@@ -1027,6 +1027,7 @@ class Request ...@@ -1027,6 +1027,7 @@ class Request
if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) { if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) {
$timeout = 60; $timeout = 60;
$tick = 500000; //msec
if (!is_numeric($data['chat_id'])) { if (!is_numeric($data['chat_id'])) {
$data['chat_id'] = 0; $data['chat_id'] = 0;
...@@ -1035,15 +1036,16 @@ class Request ...@@ -1035,15 +1036,16 @@ class Request
while (true) { while (true) {
$requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null)); $requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null));
if ($requests['CURRENT'] <= 0 && ((isset($data['inline_message_id']) && $requests['TOTAL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['TOTAL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['TOTAL'] < 20))) { if ($requests['LIMIT_PER_SEC'] <= 0 && ((isset($data['inline_message_id']) && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] > 0 && $requests['LIMIT_PER_SEC_ALL'] < 30) || (isset($data['chat_id']) && $data['chat_id'] < 0 && $requests['LIMIT_PER_MINUTE'] < 20))) {
break; break;
} }
sleep(1); usleep($tick);
$timeout--; $timeout = $timeout - ($tick / 1000000);
if ($timeout <= 0) { if ($timeout <= 0) {
throw new TelegramException('Timed out while waiting for a request slot!'); TelegramLog::debug('Timed out while waiting for a request slot, retrying with 10 seconds delay!' . PHP_EOL . 'Request data:' . PHP_EOL . print_r($data, true) . PHP_EOL . PHP_EOL);
$timeout = 10;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment