Record Class RateLimitInfo
java.lang.Object
java.lang.Record
cloud.opencode.base.web.ratelimit.RateLimitInfo
Rate Limit Info - HTTP Rate Limiting Header Parser and Builder
限流信息 - HTTP 限流头部解析器和构建器
An immutable record representing rate limit information typically conveyed via HTTP response headers. Supports both parsing rate limit headers from responses and generating them for responses.
一个不可变记录,表示通常通过 HTTP 响应头部传递的限流信息。支持从响应中解析限流头部 以及为响应生成限流头部。
Standard Headers | 标准头部:
X-RateLimit-Limit— Maximum requests allowed in the window - 窗口内允许的最大请求数X-RateLimit-Remaining— Remaining requests in the window - 窗口内剩余请求数X-RateLimit-Reset— Unix epoch second when the window resets - 窗口重置的 Unix 纪元秒Retry-After— Seconds until the client should retry - 客户端应重试的秒数
Usage Examples | 使用示例:
// Create rate limit info
RateLimitInfo info = RateLimitInfo.of(100, 42, Instant.now().plusSeconds(3600).getEpochSecond());
// Apply to response headers
HttpHeaders headers = HttpHeaders.of();
info.applyTo(headers);
// Parse from response headers
RateLimitInfo parsed = RateLimitInfo.fromHeaders(headers);
if (parsed != null && parsed.isExhausted()) {
long waitSeconds = parsed.retryAfterSeconds();
}
Security | 安全性:
- Thread-safe: Yes (immutable record) - 是(不可变记录)
- Null-safe: Factory methods validate arguments - 工厂方法验证参数
- Since:
- JDK 25, opencode-base-web V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRateLimitInfo(long limit, long remaining, long resetEpochSecond) Compact constructor with validation. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyTo(HttpHeaders headers) Applies rate limit headers to the given HttpHeaders.final booleanIndicates whether some other object is "equal to" this one.static RateLimitInfofromHeaders(HttpHeaders headers) Parses rate limit information from HTTP response headers.final inthashCode()Returns a hash code value for this object.booleanChecks if the rate limit has been exhausted (no remaining requests).longlimit()Returns the value of thelimitrecord component.static RateLimitInfoof(long limit, long remaining, long resetEpochSecond) Creates a RateLimitInfo instance.longReturns the value of theremainingrecord component.longReturns the value of theresetEpochSecondrecord component.Returns the reset time as an Instant.longCalculates the number of seconds until the rate limit window resets.final StringtoString()Returns a string representation of this record class.
-
Field Details
-
HEADER_LIMIT
-
HEADER_REMAINING
-
HEADER_RESET
X-RateLimit-Reset header name (Unix epoch seconds).- See Also:
-
HEADER_RETRY_AFTER
-
-
Constructor Details
-
RateLimitInfo
public RateLimitInfo(long limit, long remaining, long resetEpochSecond) Compact constructor with validation. 带验证的紧凑构造函数。- Throws:
IllegalArgumentException- if limit is negative, remaining is negative, or remaining exceeds limit - 如果 limit 或 remaining 为负数,或 remaining 超过 limit
-
-
Method Details
-
of
Creates a RateLimitInfo instance. 创建 RateLimitInfo 实例。- Parameters:
limit- the rate limit ceiling - 限流上限remaining- the remaining requests in the current window - 当前窗口中剩余的请求数resetEpochSecond- the Unix epoch second when the window resets - 窗口重置的 Unix 纪元秒- Returns:
- a new RateLimitInfo - 新的 RateLimitInfo
-
fromHeaders
Parses rate limit information from HTTP response headers. 从 HTTP 响应头部解析限流信息。Returns
nullif the required headers (X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset) are not present or cannot be parsed as valid numbers.如果所需头部不存在或无法解析为有效数字,则返回
null。- Parameters:
headers- the HTTP headers to parse - 要解析的 HTTP 头部- Returns:
- the parsed RateLimitInfo or null - 解析的 RateLimitInfo 或 null
-
isExhausted
public boolean isExhausted()Checks if the rate limit has been exhausted (no remaining requests). 检查限流是否已耗尽(没有剩余请求)。- Returns:
- true if remaining is zero - 如果剩余为零返回 true
-
retryAfterSeconds
public long retryAfterSeconds()Calculates the number of seconds until the rate limit window resets. 计算距离限流窗口重置的秒数。Returns 0 if the reset time is in the past.
如果重置时间已过则返回 0。
- Returns:
- seconds until reset (non-negative) - 距离重置的秒数(非负)
-
resetInstant
Returns the reset time as an Instant. 返回重置时间的 Instant。- Returns:
- the reset Instant - 重置的 Instant
-
applyTo
Applies rate limit headers to the given HttpHeaders. 将限流头部应用到给定的 HttpHeaders。Sets the standard rate limit headers and, if the limit is exhausted, also sets the
Retry-Afterheader.设置标准限流头部,如果限流已耗尽,还会设置
Retry-After头部。- Parameters:
headers- the headers to apply to - 要应用的头部
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with thecomparemethod from their corresponding wrapper classes. -
limit
public long limit()Returns the value of thelimitrecord component.- Returns:
- the value of the
limitrecord component
-
remaining
public long remaining()Returns the value of theremainingrecord component.- Returns:
- the value of the
remainingrecord component
-
resetEpochSecond
public long resetEpochSecond()Returns the value of theresetEpochSecondrecord component.- Returns:
- the value of the
resetEpochSecondrecord component
-