Skip to main content

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

ParameterDescriptionDefault
posts_per_dayMaximum posts in 24 hours10
comments_per_dayMaximum comments in 24 hours50
posts_per_hourMaximum posts per hour2
comments_per_hourMaximum comments per hour5
min_delay_between_actionsMinimum seconds between actions20
max_delay_between_actionsMaximum seconds between actions40

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

  1. Initial Attempt

    • Execute the action
    • If successful, continue
    • If rate limited, enter retry loop
  2. 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:

  1. Random Delays

    delay = random.randint(
    rate_limits['min_delay_between_actions'],
    rate_limits['max_delay_between_actions']
    )
  2. 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:

  1. Frequent Rate Limits

    • Reduce action frequency
    • Increase delays between actions
    • Review concurrent operations
  2. Uneven Distribution

    • Check time distribution logic
    • Verify random delay implementation
    • Monitor action queuing
  3. Performance Issues

    • Review database queries
    • Check network latency
    • Monitor memory usage

Security Considerations

  1. API Key Protection

    • Secure storage of credentials
    • Regular key rotation
    • Access logging
  2. Request Validation

    • Input sanitization
    • Request authentication
    • Response validation
  3. Error Logging

    • Detailed error tracking
    • Rate limit violation logging
    • Performance monitoring