Skip to content

Commit 5ec361a

Browse files
Merge pull request #1 from CodingNinjasHQ/feature/concurrent-job-limiting
Add concurrent job limiting feature
2 parents b2eef87 + dab19fe commit 5ec361a

File tree

6 files changed

+500
-33
lines changed

6 files changed

+500
-33
lines changed

CLAUDE.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Commands
6+
7+
- **Install dependencies**: `bundle install`
8+
- **Run tests**: `rake test` or `rake`
9+
- **Build gem**: `rake build`
10+
- **Release gem**: `rake release`
11+
- **Generate documentation**: `rake rdoc`
12+
13+
## Architecture
14+
15+
This is a Ruby gem that adds rate limiting capabilities to Resque queues. The implementation works by:
16+
17+
1. **Core Module**: `Resque::Plugins::Throttler` in `/lib/resque/throttler.rb` contains all functionality
18+
2. **Rate Limit Configuration**: Users set limits via `Resque.rate_limit(:queue_name, at: count, per: seconds)`
19+
3. **Enforcement Mechanism**: Overrides `Resque::Worker#reserve` to check rate limits before allowing job reservation
20+
4. **Redis-based Tracking**: Uses Redis counters with expiration for tracking job counts and distributed locks for thread safety
21+
22+
Key implementation details:
23+
- Rate limit counters use Redis key: `throttler:rate_limit:{queue_name}`
24+
- Distributed locks use Redis key: `throttler:lock:{queue_name}` with 30-second expiration
25+
- Workers skip rate-limited queues rather than blocking
26+
- All logic is contained in a single file for simplicity
27+
28+
## Testing
29+
30+
The project uses Minitest with Mocha for mocking. Note that some test files (particularly `/test/resque_test.rb` and `/test/resque/job_test.rb`) appear to be from an older implementation and may not reflect the current code structure.
31+
32+
## Development Guidelines
33+
34+
- The gem follows standard Ruby gem conventions
35+
- Runtime dependency: resque > 1.25
36+
- All core functionality is in `/lib/resque/throttler.rb`
37+
- Keep the implementation simple - the entire gem is designed to do one thing well

CONCURRENT_LIMITING_SUMMARY.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)