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

Add optional parameter 'interval' to modify check interval for request limiter

parent 5cb0eba0
...@@ -64,7 +64,9 @@ try { ...@@ -64,7 +64,9 @@ try {
//$telegram->setUploadPath('../Upload'); //$telegram->setUploadPath('../Upload');
// Requests Limiter (tries to prevent reaching Telegram API limits) // Requests Limiter (tries to prevent reaching Telegram API limits)
// Second argument are options
$telegram->enableLimiter(); $telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Run user selected commands // Run user selected commands
$telegram->runCommands($commands); $telegram->runCommands($commands);
......
...@@ -70,7 +70,9 @@ try { ...@@ -70,7 +70,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]); //$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits) // Requests Limiter (tries to prevent reaching Telegram API limits)
// Second argument are options
$telegram->enableLimiter(); $telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram getUpdates request // Handle telegram getUpdates request
$serverResponse = $telegram->handleGetUpdates(); $serverResponse = $telegram->handleGetUpdates();
......
...@@ -69,7 +69,9 @@ try { ...@@ -69,7 +69,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]); //$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits) // Requests Limiter (tries to prevent reaching Telegram API limits)
// Second argument are options
$telegram->enableLimiter(); $telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram webhook request // Handle telegram webhook request
$telegram->handle(); $telegram->handle();
......
...@@ -53,6 +53,13 @@ class Request ...@@ -53,6 +53,13 @@ class Request
*/ */
private static $limiter_enabled; private static $limiter_enabled;
/**
* Request limiter's interval between checks
*
* @var boolean
*/
private static $limiter_interval;
/** /**
* Available actions to send * Available actions to send
* *
...@@ -990,10 +997,24 @@ class Request ...@@ -990,10 +997,24 @@ class Request
* Enable request limiter * Enable request limiter
* *
* @param boolean $value * @param boolean $value
* @param array $options
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public static function setLimiter($value = true) public static function setLimiter($value = true, array $options = [])
{ {
if (DB::isDbConnected()) { if (DB::isDbConnected()) {
$options_default = [
'interval' => 1,
];
$options = array_merge($options_default, $options);
if (!is_numeric($options['interval']) || $options['interval'] <= 0) {
throw new TelegramException('Interval must be a number and must be greater than zero!');
}
self::$limiter_interval = $options['interval'];
self::$limiter_enabled = $value; self::$limiter_enabled = $value;
} }
} }
...@@ -1044,13 +1065,13 @@ class Request ...@@ -1044,13 +1065,13 @@ class Request
if ($requests['LIMIT_PER_SEC'] == 0 // No more than one message per second inside a particular chat if ($requests['LIMIT_PER_SEC'] == 0 // No more than one message per second inside a particular chat
&& ((($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) // No more than 30 messages per second globally && ((($chat_id > 0 || $inline_message_id) && $requests['LIMIT_PER_SEC_ALL'] < 30) // No more than 30 messages per second globally
|| ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20)) || ($chat_id < 0 && $requests['LIMIT_PER_MINUTE'] < 20)) // No more than 20 messages per minute in groups and channels
) { ) {
break; break;
} }
$timeout--; $timeout--;
sleep(1); usleep(self::$limiter_interval * 1000000);
} }
DB::insertTelegramRequest($action, $data); DB::insertTelegramRequest($action, $data);
......
...@@ -882,10 +882,14 @@ class Telegram ...@@ -882,10 +882,14 @@ class Telegram
/** /**
* Enable requests limiter * Enable requests limiter
*
* @param array $options
*
* @return \Longman\TelegramBot\Telegram
*/ */
public function enableLimiter() public function enableLimiter(array $options = [])
{ {
Request::setLimiter(true); Request::setLimiter(true, $options);
return $this; return $this;
} }
......
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