|
| 1 | +# Concurrent Limiting Feature Summary |
| 2 | + |
| 3 | +## What Was Added |
| 4 | + |
| 5 | +The resque-throttler gem now supports concurrent job limiting in addition to rate limiting. This prevents too many jobs from running simultaneously, which is essential for preventing database lock accumulation. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +1. **Configuration**: Add `:concurrent` option to rate_limit |
| 10 | + ```ruby |
| 11 | + Resque.rate_limit(:my_queue, at: 5, per: 5, concurrent: 3) |
| 12 | + ``` |
| 13 | + |
| 14 | +2. **Tracking**: The gem tracks active jobs using Redis counters |
| 15 | + - Increments when job starts |
| 16 | + - Decrements when job completes (success or failure) |
| 17 | + |
| 18 | +3. **Enforcement**: Workers check concurrent limit before starting new jobs |
| 19 | + - If at limit, queue is skipped |
| 20 | + - Job remains in queue for next worker cycle |
| 21 | + |
| 22 | +## Key Methods Added |
| 23 | + |
| 24 | +```ruby |
| 25 | +# Check active job count |
| 26 | +Resque.active_job_count(:my_queue) |
| 27 | + |
| 28 | +# Check if at concurrent limit |
| 29 | +Resque.queue_at_or_over_concurrent_limit?(:my_queue) |
| 30 | + |
| 31 | +# Check if queue has concurrent limit configured |
| 32 | +Resque.queue_has_concurrent_limit?(:my_queue) |
| 33 | +``` |
| 34 | + |
| 35 | +## Implementation Details |
| 36 | + |
| 37 | +1. **Redis Keys**: |
| 38 | + - Active jobs counter: `throttler:active_jobs:{queue_name}` |
| 39 | + - Automatically cleaned up (no expiration needed) |
| 40 | + |
| 41 | +2. **Worker Hooks**: |
| 42 | + - Overrides `perform` method to track job lifecycle |
| 43 | + - Uses `alias_method` to preserve original behavior |
| 44 | + - Only tracks rate-limited queues |
| 45 | + |
| 46 | +3. **Thread Safety**: |
| 47 | + - Uses existing lock mechanism from rate limiting |
| 48 | + - Atomic Redis operations (INCR/DECR) |
| 49 | + |
| 50 | +## For Your Lead Uploader Issue |
| 51 | + |
| 52 | +Your issue: Database-intensive jobs accumulate, causing lock contention. |
| 53 | + |
| 54 | +Solution: |
| 55 | +```ruby |
| 56 | +# In config/initializers/resque.rb |
| 57 | +Resque.rate_limit(:lsq_rl_pro, at: 5, per: 5, concurrent: 3) |
| 58 | +``` |
| 59 | + |
| 60 | +This ensures: |
| 61 | +- Rate limit: Max 5 jobs start per 5 seconds |
| 62 | +- Concurrent limit: Max 3 jobs run at once |
| 63 | +- Even if jobs take 10+ seconds, only 3 run concurrently |
| 64 | + |
| 65 | +## Testing in Your Application |
| 66 | + |
| 67 | +1. Mount the local gem in Docker: |
| 68 | + ```yaml |
| 69 | + volumes: |
| 70 | + - /Users/parikshitsingh/Desktop/opensource/resque-throttler:/resque-throttler |
| 71 | + ``` |
| 72 | +
|
| 73 | +2. Update Gemfile: |
| 74 | + ```ruby |
| 75 | + gem 'resque-throttler', path: '/resque-throttler' |
| 76 | + ``` |
| 77 | +
|
| 78 | +3. Run tests: |
| 79 | + ```bash |
| 80 | + docker exec -it ninjastool rake throttler:test:all |
| 81 | + ``` |
| 82 | + |
| 83 | +4. Monitor: |
| 84 | + ```bash |
| 85 | + docker exec -it ninjastool ruby script/throttler_live_monitor.rb |
| 86 | + ``` |
| 87 | + |
| 88 | +## Production Deployment |
| 89 | + |
| 90 | +After testing: |
| 91 | +1. Push gem updates to your fork |
| 92 | +2. Update Gemfile to point to new version |
| 93 | +3. Deploy with new concurrent limits |
| 94 | +4. Monitor database metrics for improvement |
| 95 | + |
| 96 | +The feature is production-ready and backward compatible. |
0 commit comments