Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue\Middleware;
4
 
5
use Illuminate\Cache\RateLimiter;
6
use Illuminate\Container\Container;
7
use Throwable;
8
 
9
class ThrottlesExceptions
10
{
11
    /**
12
     * The developer specified key that the rate limiter should use.
13
     *
14
     * @var string
15
     */
16
    protected $key;
17
 
18
    /**
19
     * Indicates whether the throttle key should use the job's UUID.
20
     *
21
     * @var bool
22
     */
23
    protected $byJob = false;
24
 
25
    /**
26
     * The maximum number of attempts allowed before rate limiting applies.
27
     *
28
     * @var int
29
     */
30
    protected $maxAttempts;
31
 
32
    /**
33
     * The number of minutes until the maximum attempts are reset.
34
     *
35
     * @var int
36
     */
37
    protected $decayMinutes;
38
 
39
    /**
40
     * The number of minutes to wait before retrying the job after an exception.
41
     *
42
     * @var int
43
     */
44
    protected $retryAfterMinutes = 0;
45
 
46
    /**
47
     * The callback that determines if rate limiting should apply.
48
     *
49
     * @var callable
50
     */
51
    protected $whenCallback;
52
 
53
    /**
54
     * The prefix of the rate limiter key.
55
     *
56
     * @var string
57
     */
58
    protected $prefix = 'laravel_throttles_exceptions:';
59
 
60
    /**
61
     * The rate limiter instance.
62
     *
63
     * @var \Illuminate\Cache\RateLimiter
64
     */
65
    protected $limiter;
66
 
67
    /**
68
     * Create a new middleware instance.
69
     *
70
     * @param  int  $maxAttempts
71
     * @param  int  $decayMinutes
72
     * @return void
73
     */
74
    public function __construct($maxAttempts = 10, $decayMinutes = 10)
75
    {
76
        $this->maxAttempts = $maxAttempts;
77
        $this->decayMinutes = $decayMinutes;
78
    }
79
 
80
    /**
81
     * Process the job.
82
     *
83
     * @param  mixed  $job
84
     * @param  callable  $next
85
     * @return mixed
86
     */
87
    public function handle($job, $next)
88
    {
89
        $this->limiter = Container::getInstance()->make(RateLimiter::class);
90
 
91
        if ($this->limiter->tooManyAttempts($jobKey = $this->getKey($job), $this->maxAttempts)) {
92
            return $job->release($this->getTimeUntilNextRetry($jobKey));
93
        }
94
 
95
        try {
96
            $next($job);
97
 
98
            $this->limiter->clear($jobKey);
99
        } catch (Throwable $throwable) {
100
            if ($this->whenCallback && ! call_user_func($this->whenCallback, $throwable)) {
101
                throw $throwable;
102
            }
103
 
104
            $this->limiter->hit($jobKey, $this->decayMinutes * 60);
105
 
106
            return $job->release($this->retryAfterMinutes * 60);
107
        }
108
    }
109
 
110
    /**
111
     * Specify a callback that should determine if rate limiting behavior should apply.
112
     *
113
     * @param  callable  $callback
114
     * @return $this
115
     */
116
    public function when(callable $callback)
117
    {
118
        $this->whenCallback = $callback;
119
 
120
        return $this;
121
    }
122
 
123
    /**
124
     * Set the prefix of the rate limiter key.
125
     *
126
     * @param  string  $prefix
127
     * @return $this
128
     */
129
    public function withPrefix(string $prefix)
130
    {
131
        $this->prefix = $prefix;
132
 
133
        return $this;
134
    }
135
 
136
    /**
137
     * Specify the number of minutes a job should be delayed when it is released (before it has reached its max exceptions).
138
     *
139
     * @param  int  $backoff
140
     * @return $this
141
     */
142
    public function backoff($backoff)
143
    {
144
        $this->retryAfterMinutes = $backoff;
145
 
146
        return $this;
147
    }
148
 
149
    /**
150
     * Get the cache key associated for the rate limiter.
151
     *
152
     * @param  mixed  $job
153
     * @return string
154
     */
155
    protected function getKey($job)
156
    {
157
        if ($this->key) {
158
            return $this->prefix.$this->key;
159
        } elseif ($this->byJob) {
160
            return $this->prefix.$job->job->uuid();
161
        }
162
 
163
        return $this->prefix.md5(get_class($job));
164
    }
165
 
166
    /**
167
     * Set the value that the rate limiter should be keyed by.
168
     *
169
     * @param  string  $key
170
     * @return $this
171
     */
172
    public function by($key)
173
    {
174
        $this->key = $key;
175
 
176
        return $this;
177
    }
178
 
179
    /**
180
     * Indicate that the throttle key should use the job's UUID.
181
     *
182
     * @return $this
183
     */
184
    public function byJob()
185
    {
186
        $this->byJob = true;
187
 
188
        return $this;
189
    }
190
 
191
    /**
192
     * Get the number of seconds that should elapse before the job is retried.
193
     *
194
     * @param  string  $key
195
     * @return int
196
     */
197
    protected function getTimeUntilNextRetry($key)
198
    {
199
        return $this->limiter->availableIn($key) + 3;
200
    }
201
}