Skip to content

Commit

Permalink
Changes...
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed Jan 9, 2017
1 parent ac29f07 commit 0dee13b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ public static function getTelegramRequestCount($chat_id = null, $inline_message_
$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_STR);
$sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR);
$sth->bindParam(':date', $date, \PDO::PARAM_STR);
$sth->bindParam(':date_minute', $date_minute, \PDO::PARAM_STR);
Expand All @@ -1095,8 +1095,8 @@ public static function getTelegramRequestCount($chat_id = null, $inline_message_
/**
* Insert Telegram API request in db
*
* @param string $method
* @param array $data
* @param string $method
* @param array $data
*
* @return bool If the insert was successful
* @throws \Longman\TelegramBot\Exception\TelegramException
Expand All @@ -1107,7 +1107,7 @@ public static function insertTelegramRequest($method, $data)
return false;
}

$chat_id = ((isset($data['chat_id']) && $data['chat_id'] != 0) ? $data['chat_id'] : null);
$chat_id = ((isset($data['chat_id'])) ? $data['chat_id'] : null);
$inline_message_id = (isset($data['inline_message_id']) ? $data['inline_message_id'] : null);

try {
Expand All @@ -1122,7 +1122,7 @@ public static function insertTelegramRequest($method, $data)

$created_at = self::getTimestamp();

$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR);
$sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR);
$sth->bindParam(':method', $method, \PDO::PARAM_STR);
$sth->bindParam(':date', $created_at, \PDO::PARAM_STR);
Expand Down
20 changes: 11 additions & 9 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,7 @@ private static function limitTelegramRequests($action, array $data = [])

if ((isset($data['chat_id']) || isset($data['inline_message_id'])) && in_array($action, $limited_methods)) {
$timeout = 60;
$tick = 500000; //msec

if (!is_numeric($data['chat_id'])) {
$data['chat_id'] = 0;
}
$retry = false;

while (true) {
$requests = DB::getTelegramRequestCount((isset($data['chat_id']) ? $data['chat_id'] : null), (isset($data['inline_message_id']) ? $data['inline_message_id'] : null));
Expand All @@ -1040,12 +1036,18 @@ private static function limitTelegramRequests($action, array $data = [])
break;
}

usleep($tick);
$timeout = $timeout - ($tick / 1000000);
sleep(1);
$timeout = $timeout - 1;

if ($timeout <= 0) {
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;
if ($retry) {
throw new TelegramException('Timed out while waiting for a request slot after 2 tries!');
} else {
$timeout = 60;
$retry = true;

TelegramLog::debug('Timed out while waiting for a request slot, retrying! Request data:' . PHP_EOL . print_r($data, true) . PHP_EOL . PHP_EOL);
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,10 @@ CREATE TABLE IF NOT EXISTS `botan_shortener` (

CREATE TABLE IF NOT EXISTS `request_limiter` (
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Unique chat identifier',
`chat_id` char(255) NULL DEFAULT NULL COMMENT 'Unique chat identifier',
`inline_message_id` char(255) NULL DEFAULT NULL COMMENT 'Identifier of the sent inline message',
`method` char(255) DEFAULT NULL COMMENT 'Request method',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date creation',

PRIMARY KEY (`id`),

FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT charSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

0 comments on commit 0dee13b

Please sign in to comment.