Skip to content

insynwyn/spam-handler

Repository files navigation

Spam Handler

A behavioral spam detection system with pluggable cache service and sliding window rate limiter using Redis zsets.

Features

  • Behavioral Spam Detection: Analyzes patterns in user behavior to detect spam
  • Pluggable Cache Service: Interface-based design allowing different cache implementations
  • Sliding Window Rate Limiter: Uses Redis zsets with timestamp scores and UUID members
  • Redis Integration: Optimized for Redis zset operations

Architecture

  • spam_detector.py - Core behavioral spam detection logic
  • cache/ - Cache service interfaces and implementations
    • base.py - Abstract cache interface
    • redis_cache.py - Redis implementation
    • memory_cache.py - In-memory implementation for testing
  • rate_limiter.py - Sliding window rate limiter using Redis zsets
  • models.py - Data models and types
  • config.py - Configuration management

Installation

pip install -r requirements.txt

Configuration

Set environment variables:

REDIS_URL=redis://localhost:6379/0
SPAM_DETECTION_THRESHOLD=0.7
RATE_LIMIT_WINDOW_SECONDS=3600
RATE_LIMIT_MAX_REQUESTS=100

Usage

from spam_handler import SpamDetector, RateLimiter
from spam_handler.cache import RedisCache

# Initialize components
cache = RedisCache()
detector = SpamDetector(cache)
rate_limiter = RateLimiter(cache)

# Check for spam
is_spam = detector.is_spam(user_id, content, metadata)

# Check rate limit
is_allowed = rate_limiter.is_allowed(user_id, action_type)

Testing

python -m pytest tests/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages