Skip to content

Commit 21fee76

Browse files
committed
rename class
1 parent 0ddc7ab commit 21fee76

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

src/Illuminate/Queue/Middleware/CircuitBreaker.php renamed to src/Illuminate/Queue/Middleware/ThrottlesExceptions.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Illuminate\Container\Container;
77
use Throwable;
88

9-
class CircuitBreaker
9+
class ThrottlesExceptions
1010
{
1111
/**
12-
* The maximum number of attempts allowed before the circuit is opened.
12+
* The maximum number of attempts allowed before rate limiting applies.
1313
*
1414
* @var int
1515
*/
@@ -36,6 +36,13 @@ class CircuitBreaker
3636
*/
3737
protected $key;
3838

39+
/**
40+
* The callback that determines if rate limiting should apply.
41+
*
42+
* @var callable
43+
*/
44+
protected $whenCallback;
45+
3946
/**
4047
* The prefix of the rate limiter key.
4148
*
@@ -86,34 +93,40 @@ public function handle($job, $next)
8693

8794
$this->limiter->clear($jobKey);
8895
} catch (Throwable $throwable) {
96+
if ($this->whenCallback && ! call_user_func($this->whenCallback, $throwable)) {
97+
throw $throwable;
98+
}
99+
89100
$this->limiter->hit($jobKey, $this->decayMinutes * 60);
90101

91102
return $job->release($this->retryAfterMinutes * 60);
92103
}
93104
}
94105

95106
/**
96-
* Set the prefix of the rate limiter key.
107+
* Specify a callback that should determine if rate limiting behavior should apply.
97108
*
98-
* @param string $prefix
109+
* @param callable $callback
99110
* @return $this
100111
*/
101-
public function withPrefix(string $prefix)
112+
public function when(callable $callback)
102113
{
103-
$this->prefix = $prefix;
114+
$this->whenCallback = $callback;
104115

105116
return $this;
106117
}
107118

108119
/**
109-
* Get the number of seconds that should elapse before the job is retried.
120+
* Set the prefix of the rate limiter key.
110121
*
111-
* @param string $key
112-
* @return int
122+
* @param string $prefix
123+
* @return $this
113124
*/
114-
protected function getTimeUntilNextRetry($key)
125+
public function withPrefix(string $prefix)
115126
{
116-
return $this->limiter->availableIn($key) + 3;
127+
$this->prefix = $prefix;
128+
129+
return $this;
117130
}
118131

119132
/**
@@ -124,6 +137,17 @@ protected function getTimeUntilNextRetry($key)
124137
*/
125138
protected function getKey($job)
126139
{
127-
return md5($this->prefix.(empty($this->key) ? get_class($job) : $this->key));
140+
return $this->prefix.md5(empty($this->key) ? get_class($job) : $this->key);
141+
}
142+
143+
/**
144+
* Get the number of seconds that should elapse before the job is retried.
145+
*
146+
* @param string $key
147+
* @return int
148+
*/
149+
protected function getTimeUntilNextRetry($key)
150+
{
151+
return $this->limiter->availableIn($key) + 3;
128152
}
129153
}

tests/Integration/Queue/CircuitBreakerTest.php renamed to tests/Integration/Queue/ThrottlesExceptionsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
use Illuminate\Contracts\Queue\Job;
99
use Illuminate\Queue\CallQueuedHandler;
1010
use Illuminate\Queue\InteractsWithQueue;
11-
use Illuminate\Queue\Middleware\CircuitBreaker;
11+
use Illuminate\Queue\Middleware\ThrottlesExceptions;
1212
use Mockery as m;
1313
use Orchestra\Testbench\TestCase;
1414

1515
/**
1616
* @group integration
1717
*/
18-
class CircuitBreakerTest extends TestCase
18+
class ThrottlesExceptionsTest extends TestCase
1919
{
2020
protected function tearDown(): void
2121
{
@@ -122,7 +122,7 @@ public function handle()
122122

123123
public function middleware()
124124
{
125-
return [new CircuitBreaker(2, 10, 0, 'test')];
125+
return [new ThrottlesExceptions(2, 10, 0, 'test')];
126126
}
127127
}
128128

@@ -139,6 +139,6 @@ public function handle()
139139

140140
public function middleware()
141141
{
142-
return [new CircuitBreaker(2, 10, 0, 'test')];
142+
return [new ThrottlesExceptions(2, 10, 0, 'test')];
143143
}
144144
}

0 commit comments

Comments
 (0)