提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.ratelimiter.core.annotation; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; |
|
4 |
import com.iailab.framework.idempotent.core.keyresolver.impl.ExpressionIdempotentKeyResolver; |
|
5 |
import com.iailab.framework.ratelimiter.core.keyresolver.RateLimiterKeyResolver; |
|
6 |
import com.iailab.framework.ratelimiter.core.keyresolver.impl.ClientIpRateLimiterKeyResolver; |
|
7 |
import com.iailab.framework.ratelimiter.core.keyresolver.impl.DefaultRateLimiterKeyResolver; |
|
8 |
import com.iailab.framework.ratelimiter.core.keyresolver.impl.ServerNodeRateLimiterKeyResolver; |
|
9 |
import com.iailab.framework.ratelimiter.core.keyresolver.impl.UserRateLimiterKeyResolver; |
|
10 |
|
|
11 |
import java.lang.annotation.ElementType; |
|
12 |
import java.lang.annotation.Retention; |
|
13 |
import java.lang.annotation.RetentionPolicy; |
|
14 |
import java.lang.annotation.Target; |
|
15 |
import java.util.concurrent.TimeUnit; |
|
16 |
|
|
17 |
/** |
|
18 |
* 限流注解 |
|
19 |
* |
|
20 |
* @author iailab |
|
21 |
*/ |
|
22 |
@Target({ElementType.METHOD}) |
|
23 |
@Retention(RetentionPolicy.RUNTIME) |
|
24 |
public @interface RateLimiter { |
|
25 |
|
|
26 |
/** |
|
27 |
* 限流的时间,默认为 1 秒 |
|
28 |
*/ |
|
29 |
int time() default 1; |
|
30 |
/** |
|
31 |
* 时间单位,默认为 SECONDS 秒 |
|
32 |
*/ |
|
33 |
TimeUnit timeUnit() default TimeUnit.SECONDS; |
|
34 |
|
|
35 |
/** |
|
36 |
* 限流次数 |
|
37 |
*/ |
|
38 |
int count() default 100; |
|
39 |
|
|
40 |
/** |
|
41 |
* 提示信息,请求过快的提示 |
|
42 |
* |
|
43 |
* @see GlobalErrorCodeConstants#TOO_MANY_REQUESTS |
|
44 |
*/ |
|
45 |
String message() default ""; // 为空时,使用 TOO_MANY_REQUESTS 错误提示 |
|
46 |
|
|
47 |
/** |
|
48 |
* 使用的 Key 解析器 |
|
49 |
* |
|
50 |
* @see DefaultRateLimiterKeyResolver 全局级别 |
|
51 |
* @see UserRateLimiterKeyResolver 用户 ID 级别 |
|
52 |
* @see ClientIpRateLimiterKeyResolver 用户 IP 级别 |
|
53 |
* @see ServerNodeRateLimiterKeyResolver 服务器 Node 级别 |
|
54 |
* @see ExpressionIdempotentKeyResolver 自定义表达式,通过 {@link #keyArg()} 计算 |
|
55 |
*/ |
|
56 |
Class<? extends RateLimiterKeyResolver> keyResolver() default DefaultRateLimiterKeyResolver.class; |
|
57 |
/** |
|
58 |
* 使用的 Key 参数 |
|
59 |
*/ |
|
60 |
String keyArg() default ""; |
|
61 |
|
|
62 |
} |