Class EmailRateLimiter

java.lang.Object
cloud.opencode.base.email.security.EmailRateLimiter

public class EmailRateLimiter extends Object
Email Rate Limiter 邮件发送频率限制器

Limits email sending rate to prevent abuse.

限制邮件发送频率以防止滥用。

Features | 主要功能:

  • Per-minute rate limiting - 每分钟频率限制
  • Per-hour rate limiting - 每小时频率限制
  • Per-day rate limiting - 每天频率限制
  • Per-recipient tracking - 按收件人跟踪

Usage Examples | 使用示例:

EmailRateLimiter limiter = new EmailRateLimiter(10, 100, 500);
if (!limiter.allowSend("recipient@example.com")) {
    throw new EmailException("Rate limit exceeded");
}

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-email V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Rate limit quota record 频率限制配额记录
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create rate limiter with default limits 使用默认限制创建频率限制器
    EmailRateLimiter(int maxPerMinute, int maxPerHour, int maxPerDay)
    Create rate limiter with custom limits 使用自定义限制创建频率限制器
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Check if sending is allowed (global, not per recipient) 检查是否允许发送(全局,非按收件人)
    boolean
    allowSend(String recipient)
    Check if sending is allowed for recipient 检查是否允许向收件人发送
    int
    Get max per day limit 获取每天最大限制
    int
    Get max per hour limit 获取每小时最大限制
    int
    Get max per minute limit 获取每分钟最大限制
    getQuota(String recipient)
    Get remaining quota for recipient 获取收件人剩余配额
    void
    reset(String recipient)
    Reset rate limit for recipient 重置收件人的频率限制
    void
    Reset all rate limits 重置所有频率限制

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EmailRateLimiter

      public EmailRateLimiter()
      Create rate limiter with default limits 使用默认限制创建频率限制器
    • EmailRateLimiter

      public EmailRateLimiter(int maxPerMinute, int maxPerHour, int maxPerDay)
      Create rate limiter with custom limits 使用自定义限制创建频率限制器
      Parameters:
      maxPerMinute - max emails per minute | 每分钟最大邮件数
      maxPerHour - max emails per hour | 每小时最大邮件数
      maxPerDay - max emails per day | 每天最大邮件数
  • Method Details

    • allowSend

      public boolean allowSend(String recipient)
      Check if sending is allowed for recipient 检查是否允许向收件人发送
      Parameters:
      recipient - the recipient email | 收件人邮箱
      Returns:
      true if allowed | 允许返回true
    • allowSend

      public boolean allowSend()
      Check if sending is allowed (global, not per recipient) 检查是否允许发送(全局,非按收件人)
      Returns:
      true if allowed | 允许返回true
    • getQuota

      public EmailRateLimiter.RateLimitQuota getQuota(String recipient)
      Get remaining quota for recipient 获取收件人剩余配额
      Parameters:
      recipient - the recipient email | 收件人邮箱
      Returns:
      the remaining quota | 剩余配额
    • reset

      public void reset(String recipient)
      Reset rate limit for recipient 重置收件人的频率限制
      Parameters:
      recipient - the recipient email | 收件人邮箱
    • resetAll

      public void resetAll()
      Reset all rate limits 重置所有频率限制
    • getMaxPerMinute

      public int getMaxPerMinute()
      Get max per minute limit 获取每分钟最大限制
      Returns:
      the limit | 限制值
    • getMaxPerHour

      public int getMaxPerHour()
      Get max per hour limit 获取每小时最大限制
      Returns:
      the limit | 限制值
    • getMaxPerDay

      public int getMaxPerDay()
      Get max per day limit 获取每天最大限制
      Returns:
      the limit | 限制值