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

daycry / jobs / 21145580946

19 Jan 2026 04:55PM UTC coverage: 58.025% (-4.5%) from 62.567%
21145580946

push

github

daycry
Add security, performance, and health features; simplify architecture

Introduces shell command whitelisting, smart token detection, per-queue rate limiting, dead letter queue, job timeout protection, config caching, and a health monitoring command. Refactors architecture by consolidating traits, removing the CompletionStrategy pattern, and unifying retry policies under RetryPolicyFixed. Updates documentation and tests to reflect new features and architectural changes, ensuring backward compatibility and improved reliability.

143 of 340 new or added lines in 20 files covered. (42.06%)

1 existing line in 1 file now uncovered.

1193 of 2056 relevant lines covered (58.03%)

4.44 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
    {
NEW
34
        if ($maxPerMinute <= 0) {
×
NEW
35
            return true; // Unlimited
×
36
        }
37

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

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

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

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

NEW
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
    {
NEW
60
        if (! $this->allow($queue, $maxPerMinute)) {
×
NEW
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}";
×
NEW
73
        $cache = service('cache');
×
74

NEW
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}";
×
NEW
84
        $cache = service('cache');
×
NEW
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