Skip to content

Commit 37e48ba

Browse files
committed
formatting and fix key usage
1 parent a4678ce commit 37e48ba

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/Illuminate/Queue/Middleware/ThrottlesExceptions.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class ThrottlesExceptions
1010
{
11+
/**
12+
* The developer specified key that the rate limiter should use.
13+
*
14+
* @var string
15+
*/
16+
protected $key;
17+
1118
/**
1219
* The maximum number of attempts allowed before rate limiting applies.
1320
*
@@ -27,14 +34,7 @@ class ThrottlesExceptions
2734
*
2835
* @var int
2936
*/
30-
protected $retryAfterMinutes;
31-
32-
/**
33-
* The rate limiter key.
34-
*
35-
* @var string
36-
*/
37-
protected $key;
37+
protected $retryAfterMinutes = 0;
3838

3939
/**
4040
* The callback that determines if rate limiting should apply.
@@ -48,7 +48,7 @@ class ThrottlesExceptions
4848
*
4949
* @var string
5050
*/
51-
protected $prefix = 'circuit_breaker:';
51+
protected $prefix = 'laravel_throttles_exceptions:';
5252

5353
/**
5454
* The rate limiter instance.
@@ -62,15 +62,13 @@ class ThrottlesExceptions
6262
*
6363
* @param int $maxAttempts
6464
* @param int $decayMinutes
65-
* @param int $retryAfterMinutes
6665
* @param string $key
66+
* @return void
6767
*/
68-
public function __construct($maxAttempts = 10, $decayMinutes = 10, $retryAfterMinutes = 0, string $key = '')
68+
public function __construct($maxAttempts = 10, $decayMinutes = 10)
6969
{
7070
$this->maxAttempts = $maxAttempts;
7171
$this->decayMinutes = $decayMinutes;
72-
$this->retryAfterMinutes = $retryAfterMinutes;
73-
$this->key = $key;
7472
}
7573

7674
/**
@@ -129,6 +127,19 @@ public function withPrefix(string $prefix)
129127
return $this;
130128
}
131129

130+
/**
131+
* Specify the number of seconds a job should be delayed when it is released (before it has reached its max exceptions).
132+
*
133+
* @param int $backoff
134+
* @return $this
135+
*/
136+
public function backoff($backoff)
137+
{
138+
$this->retryAfterMinutes = $backoff;
139+
140+
return $this;
141+
}
142+
132143
/**
133144
* Get the cache key associated for the rate limiter.
134145
*
@@ -137,7 +148,20 @@ public function withPrefix(string $prefix)
137148
*/
138149
protected function getKey($job)
139150
{
140-
return $this->prefix.md5(empty($this->key) ? get_class($job) : $this->key);
151+
return $this->key ? $this->prefix.$this->key : $this->prefix.$job->job->uuid();
152+
}
153+
154+
/**
155+
* Set the value that the rate limiter should be keyed by.
156+
*
157+
* @param string $key
158+
* @return $this
159+
*/
160+
public function by($key)
161+
{
162+
$this->key = $key;
163+
164+
return $this;
141165
}
142166

143167
/**

tests/Integration/Queue/ThrottlesExceptionsTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function assertJobWasReleasedImmediately($class)
5858
$job->shouldReceive('release')->with(0)->once();
5959
$job->shouldReceive('isReleased')->andReturn(true);
6060
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);
61+
$job->shouldReceive('uuid')->andReturn('simple-test-uuid');
6162

6263
$instance->call($job, [
6364
'command' => serialize($command = new $class),
@@ -79,6 +80,7 @@ protected function assertJobWasReleasedWithDelay($class)
7980
})->once();
8081
$job->shouldReceive('isReleased')->andReturn(true);
8182
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);
83+
$job->shouldReceive('uuid')->andReturn('simple-test-uuid');
8284

8385
$instance->call($job, [
8486
'command' => serialize($command = new $class),
@@ -98,6 +100,7 @@ protected function assertJobRanSuccessfully($class)
98100
$job->shouldReceive('isReleased')->andReturn(false);
99101
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false);
100102
$job->shouldReceive('delete')->once();
103+
$job->shouldReceive('uuid')->andReturn('simple-test-uuid');
101104

102105
$instance->call($job, [
103106
'command' => serialize($command = new $class),
@@ -122,7 +125,7 @@ public function handle()
122125

123126
public function middleware()
124127
{
125-
return [new ThrottlesExceptions(2, 10, 0, 'test')];
128+
return [(new ThrottlesExceptions(2, 10))->by('test')];
126129
}
127130
}
128131

@@ -139,6 +142,6 @@ public function handle()
139142

140143
public function middleware()
141144
{
142-
return [new ThrottlesExceptions(2, 10, 0, 'test')];
145+
return [(new ThrottlesExceptions(2, 10))->by('test')];
143146
}
144147
}

0 commit comments

Comments
 (0)