Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Added support of PHP 7.3 to RateLimiter middleware(queue) serialization #35986

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/Illuminate/Queue/Middleware/RateLimited.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,21 @@ protected function getTimeUntilNextRetry($key)
*
* @return array
*/
public function __serialize()
public function __sleep()
{
return [
'limiterName' => $this->limiterName,
'shouldRelease' => $this->shouldRelease,
'limiterName',
'shouldRelease',
];
}

/**
* Prepare the object after unserialization.
*
* @param array $data
* @return void
*/
public function __unserialize(array $data)
public function __wakeup()
{
$this->limiter = Container::getInstance()->make(RateLimiter::class);

$this->limiterName = $data['limiterName'];
$this->shouldRelease = $data['shouldRelease'];
}
}
5 changes: 2 additions & 3 deletions src/Illuminate/Queue/Middleware/RateLimitedWithRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ protected function getTimeUntilNextRetry($key)
/**
* Prepare the object after unserialization.
*
* @param array $data
* @return void
*/
public function __unserialize(array $data)
public function __wakeup()
{
parent::__unserialize($data);
parent::__wakeup();

$this->redis = Container::getInstance()->make(Redis::class);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Queue/RateLimitedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public function testJobsCanHaveConditionalRateLimits()
$this->assertJobWasReleased(NonAdminTestJob::class);
}

public function testMiddlewareSerialization()
{
$rateLimited = new RateLimited('limiterName');
$rateLimited->shouldRelease = false;

$restoredRateLimited = unserialize(serialize($rateLimited));

$fetch = (function (string $name) {
return $this->{$name};
})->bindTo($restoredRateLimited, RateLimited::class);

$this->assertFalse($restoredRateLimited->shouldRelease);
$this->assertEquals('limiterName', $fetch('limiterName'));
$this->assertInstanceOf(RateLimiter::class, $fetch('limiter'));
}

protected function assertJobRanSuccessfully($class)
{
$class::$handled = false;
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Queue/RateLimitedWithRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
use Illuminate\Queue\CallQueuedHandler;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -111,6 +112,23 @@ public function testJobsCanHaveConditionalRateLimits()
$this->assertJobWasReleased($nonAdminJob);
}

public function testMiddlewareSerialization()
{
$rateLimited = new RateLimitedWithRedis('limiterName');
$rateLimited->shouldRelease = false;

$restoredRateLimited = unserialize(serialize($rateLimited));

$fetch = (function (string $name) {
return $this->{$name};
})->bindTo($restoredRateLimited, RateLimitedWithRedis::class);

$this->assertFalse($restoredRateLimited->shouldRelease);
$this->assertEquals('limiterName', $fetch('limiterName'));
$this->assertInstanceOf(RateLimiter::class, $fetch('limiter'));
$this->assertInstanceOf(Redis::class, $fetch('redis'));
}

protected function assertJobRanSuccessfully($testJob)
{
$testJob::$handled = false;
Expand Down