Rate Limiting System
RedHarmony implements a sophisticated rate limiting system to ensure compliance with Reddit's API guidelines while maintaining natural interaction patterns.
Overview
The rate limiting system operates on multiple levels:
- Global rate limits for the entire application
- Per-action limits (posts vs. comments)
- Time-based limits (hourly and daily)
- Smart retry handling with exponential backoff
Configuration
Rate limits are configured in config.json
:
{
"rate_limits": {
"posts_per_day": 10,
"comments_per_day": 50,
"posts_per_hour": 2,
"comments_per_hour": 5,
"min_delay_between_actions": 20,
"max_delay_between_actions": 40
}
}
Configuration Parameters
Parameter | Description | Default |
---|---|---|
posts_per_day | Maximum posts in 24 hours | 10 |
comments_per_day | Maximum comments in 24 hours | 50 |
posts_per_hour | Maximum posts per hour | 2 |
comments_per_hour | Maximum comments per hour | 5 |
min_delay_between_actions | Minimum seconds between actions | 20 |
max_delay_between_actions | Maximum seconds between actions | 40 |
Smart Retry Strategy
RedHarmony implements an exponential backoff strategy for handling rate limits:
def handle_rate_limit(func):
"""Decorator to handle Reddit API rate limits"""
def wrapper(*args, **kwargs):
max_retries = 3
retry_delay = 60 # Start with 1 minute delay
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except praw.exceptions.RedditAPIException as e:
if "RATELIMIT" in str(e):
wait_time = retry_delay * (attempt + 1)
logger.warning(f"Rate limited. Waiting {wait_time} seconds...")
time.sleep(wait_time)
else:
raise
return wrapper
Retry Behavior
-
Initial Attempt
- Execute the action
- If successful, continue
- If rate limited, enter retry loop
-
Retry Loop
- First retry: 60-second delay
- Second retry: 120-second delay
- Third retry: 180-second delay
- After three failures: abort operation
Natural Interaction Patterns
RedHarmony maintains natural interaction patterns through:
-
Random Delays
delay = random.randint(
rate_limits['min_delay_between_actions'],
rate_limits['max_delay_between_actions']
) -
Time Distribution
- Actions are spread throughout the hour
- Automatic hourly resets
- Natural pause periods
Best Practices
1. Configuration Guidelines
- Set conservative daily limits initially
- Adjust based on subreddit activity
- Keep hourly limits below 25% of daily limits
- Maintain reasonable delays between actions
2. Monitoring and Adjustment
- Track rate limit hits
- Monitor success rates
- Adjust limits based on:
- Subreddit response
- API response times
- Error rates
3. Error Handling
try:
# Attempt action
perform_reddit_action()
except praw.exceptions.RedditAPIException as e:
if "RATELIMIT" in str(e):
# Handle rate limit
handle_rate_limit()
else:
# Handle other API errors
handle_other_error()
4. Performance Optimization
- Cache frequently accessed data
- Batch similar operations
- Implement request queuing
- Use asynchronous operations where possible
Integration with Other Systems
Database Integration
Rate limit tracking is stored in the database. See Database System for details on:
- Activity logging
- Rate limit tracking
- Performance monitoring
Personality System Integration
Rate limits are shared across all personalities. See Personality System for:
- Per-personality tracking
- Activity distribution
- Interaction patterns
Troubleshooting
Common rate limiting issues and solutions:
-
Frequent Rate Limits
- Reduce action frequency
- Increase delays between actions
- Review concurrent operations
-
Uneven Distribution
- Check time distribution logic
- Verify random delay implementation
- Monitor action queuing
-
Performance Issues
- Review database queries
- Check network latency
- Monitor memory usage
Security Considerations
-
API Key Protection
- Secure storage of credentials
- Regular key rotation
- Access logging
-
Request Validation
- Input sanitization
- Request authentication
- Response validation
-
Error Logging
- Detailed error tracking
- Rate limit violation logging
- Performance monitoring