-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/vendor/ | ||
composer.lock | ||
test.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: dino.ma | ||
* Date: 2019/9/6 | ||
* Time: 11:22 AM | ||
*/ | ||
|
||
namespace SpeedLimit\Exceptions; | ||
|
||
|
||
class RateException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: dino.ma | ||
* Date: 2019/9/6 | ||
* Time: 3:25 PM | ||
*/ | ||
|
||
namespace SpeedLimit; | ||
|
||
use Predis\Client; | ||
use SpeedLimit\Exceptions\RateException; | ||
|
||
class RateLimit | ||
{ | ||
|
||
const MICROSECOND = 'microsecond'; | ||
const MILLISECOND = 'millisecond'; | ||
const SECOND = 'second'; | ||
const MINUTE = 'minute'; | ||
const HOUR = 'hour'; | ||
const DAY = 'day'; | ||
const WEEK = 'week'; | ||
const MONTH = 'month'; | ||
const YEAR = 'year'; | ||
|
||
private static $unitMap = [ | ||
self::MICROSECOND => 0.000001, | ||
self::MILLISECOND => 0.001, | ||
self::SECOND => 1, | ||
self::MINUTE => 60, | ||
self::HOUR => 3600, | ||
self::DAY => 86400, | ||
self::WEEK => 604800, | ||
self::MONTH => 2629743.83, | ||
self::YEAR => 31556926, | ||
]; | ||
|
||
private $redis; | ||
|
||
private $redisKey; | ||
|
||
private $rate; | ||
|
||
private $timer; | ||
|
||
public function __construct(int $rate, string $timer, Client $redis) | ||
{ | ||
if (!isset(self::$unitMap[$timer])) { | ||
throw new \Exception('Failed is not timer'); | ||
} | ||
$this->rate = (int)$rate; | ||
$this->timer = self::$unitMap[$timer]; | ||
$this->redis = $redis; | ||
} | ||
|
||
public function setRedisKey(string $id) : self | ||
{ | ||
$this->redisKey = md5(sha1($id.'ACK'.$this->timer)); | ||
|
||
return $this; | ||
} | ||
|
||
|
||
public function getRedisKey() : string | ||
{ | ||
return $this->redisKey; | ||
} | ||
|
||
|
||
public function rate() : bool | ||
{ | ||
try { | ||
$nowTime = microtime(true); | ||
if (is_null($this->getRedisKey())) { | ||
throw new RateException('You must be set redis key'); | ||
} | ||
$this->redis->watch($this->getRedisKey()); | ||
$rateInfo = $this->redis->get($this->getRedisKey()); | ||
$newVal = json_encode(['max_num' => $this->rate, 'timer' => $this->timer, 'last_time' => $nowTime]); | ||
if (!is_null($rateInfo)) { | ||
$rateObj = json_decode($rateInfo); | ||
$remainTime = $nowTime - $rateObj->last_time; | ||
if ($remainTime >= $this->timer) { | ||
//超过时间则重新设置 | ||
$this->redis->set($this->getRedisKey(), $newVal); | ||
return true; | ||
} | ||
$rateObj->max_num--; | ||
if ($rateObj->max_num > 0 && $remainTime > 0) { | ||
//走本时间段内的次数减少 | ||
$newVal = json_encode(['max_num' => $rateObj->max_num--, 'timer' => $remainTime, 'last_time' => $nowTime]); | ||
$this->redis->set($this->getRedisKey(), $newVal); | ||
return true; | ||
} | ||
} else { | ||
$this->redis->set($this->getRedisKey(), $newVal); | ||
return true; | ||
} | ||
return false; | ||
} catch (RateException $exception) { | ||
throw new RateException($exception->getMessage()); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.