io.github.panxiaochao.ratelimiter.annotation.RateLimiter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pxc-framework-ratelimiter Show documentation
Show all versions of pxc-framework-ratelimiter Show documentation
[2.0.6]pxc framework ratelimiter 限流模块
The newest version!
/*
* Copyright © 2022-2024 Lypxc ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.panxiaochao.ratelimiter.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
/**
*
* 防刷、限流注解, 应用于方法
*
*
* ElementType.TYPE:能修饰类、接口或枚举类型
* ElementType.FIELD:能修饰成员变量
* ElementType.METHOD:能修饰方法
*
*
* 限流的思路:
*
*
* 1、 通过[请求路径或者方法名:访问ip]的作为key,[访问次数]为value的方式对某一请求进行唯一标识
* 2、 每次访问的时候判断key是否存在,是否count超过了限制的访问次数
* 3、 若访问超出限制,则返回限制报错信息
*
*
* @author Lypxc
* @since 2023-06-28
*/
@Documented
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimiter {
/**
* 限流Key, 支持 Spring EL 表达式, 例如 #id, #user.id
*/
String key() default "";
/**
* 指定时间内, API最大请求次数
*/
int maxCount() default 10;
/**
* 限定时间范围, 默认毫秒
*/
long limitTime() default 60 * 1000;
/**
* 时间单位格式, 默认毫秒
*/
TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
/**
* 限流类型
*/
RateLimiterType rateLimiterType() default RateLimiterType.METHOD;
/**
* 自定义提示消息
*/
String message() default "";
/**
* 限流类型
*/
enum RateLimiterType {
/**
* 根据 IP 进行限流
*/
IP,
/**
* 根据 METHOD 进行限流
*/
METHOD,
/**
* 根据 IP+METHOD 进行限流
*/
IP_METHOD,
/**
* 单机/单实例限流
*/
SINGLE
}
}