• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / jobs / 24568210769

05 Apr 2026 08:48AM UTC coverage: 53.938% (-2.2%) from 56.164%
24568210769

push

github

daycry
Optimize

50 of 192 new or added lines in 14 files covered. (26.04%)

3 existing lines in 3 files now uncovered.

1219 of 2260 relevant lines covered (53.94%)

4.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Libraries/RateLimiter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Queues.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Jobs\Libraries;
15

16
use Daycry\Jobs\Exceptions\JobException;
17

18
/**
19
 * Rate limiter for queue processing using cache-based token bucket algorithm.
20
 * Prevents queue saturation by limiting jobs processed per minute.
21
 */
22
class RateLimiter
23
{
24
    /**
25
     * Check if a queue can process another job (rate limit check).
26
     *
27
     * @param string $queue        Queue name
28
     * @param int    $maxPerMinute Maximum jobs allowed per minute (0 = unlimited)
29
     *
30
     * @return bool True if allowed, false if throttled
31
     */
32
    public function allow(string $queue, int $maxPerMinute): bool
33
    {
34
        if ($maxPerMinute <= 0) {
×
35
            return true; // Unlimited
×
36
        }
37

NEW
38
        $key   = "queue_rate_{$queue}";
×
39
        $cache = service('cache');
×
40

41
        $count = (int) ($cache->get($key) ?? 0);
×
42

43
        if ($count >= $maxPerMinute) {
×
44
            return false; // Throttled
×
45
        }
46

47
        // Increment counter (expires after 60 seconds)
48
        $cache->save($key, $count + 1, 60);
×
49

50
        return true;
×
51
    }
52

53
    /**
54
     * Throw exception if rate limit exceeded.
55
     *
56
     * @throws JobException
57
     */
58
    public function throttle(string $queue, int $maxPerMinute): void
59
    {
60
        if (! $this->allow($queue, $maxPerMinute)) {
×
61
            throw JobException::forRateLimitExceeded($queue, $maxPerMinute);
×
62
        }
63
    }
64

65
    /**
66
     * Get current rate usage for a queue.
67
     *
68
     * @return int Number of jobs processed in current minute
69
     */
70
    public function getUsage(string $queue): int
71
    {
NEW
72
        $key   = "queue_rate_{$queue}";
×
73
        $cache = service('cache');
×
74

75
        return (int) ($cache->get($key) ?? 0);
×
76
    }
77

78
    /**
79
     * Reset rate limit counter for a queue.
80
     */
81
    public function reset(string $queue): void
82
    {
NEW
83
        $key   = "queue_rate_{$queue}";
×
84
        $cache = service('cache');
×
85
        $cache->delete($key);
×
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc