Skip to content

Commit

Permalink
feat:add rate limiter code
Browse files Browse the repository at this point in the history
  • Loading branch information
itzealot committed Aug 14, 2022
0 parents commit 468a3fe
Show file tree
Hide file tree
Showing 16 changed files with 712 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# maven ignore
target/
*.jar
*.war
*.zip
*.tar
*.tar.gz

# eclipse ignore
.settings/
.project
.classpath

# idea ignore
.idea/
*.ipr
*.iml
*.iws

# temp ignore
*.log
*.cache
*.diff
*.patch
*.tmp
logs/

# system ignore
.DS_Store
Thumbs.db
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.wunong.smart</groupId>
<artifactId>smart-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>smart-middleware-tools</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>smart-rate-limiter</module>
</modules>

</project>
33 changes: 33 additions & 0 deletions smart-rate-limiter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.wunong.smart</groupId>
<artifactId>smart-middleware-tools</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>smart-rate-limiter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>smart-rate-limiter-core</module>
<module>smart-rate-limiter-starter</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.wunong.smart</groupId>
<artifactId>smart-rate-limiter-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
44 changes: 44 additions & 0 deletions smart-rate-limiter/smart-rate-limiter-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.wunong.smart</groupId>
<artifactId>smart-rate-limiter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>smart-rate-limiter-core</artifactId>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.wunong.smart</groupId>
<artifactId>smart-domain-platform-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.wunong.smart.rate.limiter.core.api;

import com.wunong.smart.domain.platform.utils.Numbers;

/**
* 限制器
*
* @author created by zealot.zt
*/
public interface IRateLimiter {

/**
* 尝试获取指定数量的许可
*
* @param permits
* @return
*/
boolean tryAcquire(int permits);

/**
* 尝试获取许可
*
* @return
*/
boolean tryAcquire();

/**
* 获取配置的qps
*
* @return
*/
int getQps();

/**
* 是否为降级
* 1. 即对应qps设置为0
*
* @return
*/
default boolean isDegraded() {
return Numbers.isNegative(getQps());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.wunong.smart.rate.limiter.core.config;

import lombok.Data;

/**
* 限流配置数据
*
* @author created by zealot.zt
*/
@Data
public class LimiterData {

/**
* 限流关键词
*/
private String key;

/**
* 限流提示
*/
private String tips;

public static LimiterData create(String key, String tips) {
LimiterData data = new LimiterData();
data.setKey(key);
data.setTips(tips);
return data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.wunong.smart.rate.limiter.core.enums;

/**
* 控制器枚举
*
* @author created by zealot.zt
*/
public enum LimiterEnum {

/**
* 被降级
*/
DEGRADE,

/**
* 被限流
*/
LIMITED,

/**
* 通过
*/
PASS,
;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.wunong.smart.rate.limiter.core.exception;

import com.wunong.smart.domain.platform.exception.IError;
import com.wunong.smart.domain.platform.exception.IErrorLevel;
import com.wunong.smart.domain.platform.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;

/**
* 降级异常
*
* @author created by zealot.zt
*/
public class DegradedException extends ServiceException {

/**
* 降级错误码
*/
public static final String DEGRADED_ERROR_CODE = "DEGRADED_ERROR";

/**
* 降级错误信息
*/
public static final IError DEGRADED_ERROR = IError.of(DEGRADED_ERROR_CODE, "亲,请求失败请稍后重试", IErrorLevel.IGNORE);

public DegradedException() {
super(DEGRADED_ERROR);
}

public DegradedException(String errorInfo) {
super(newDegradeError(errorInfo));
}

/**
* 构建降级错误
*
* @param errorInfo
* @return
*/
private static IError newDegradeError(String errorInfo) {
if (StringUtils.isBlank(errorInfo)) {
return DEGRADED_ERROR;
} else {
return IError.of(DEGRADED_ERROR_CODE, errorInfo, IErrorLevel.IGNORE);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.wunong.smart.rate.limiter.core.exception;

import com.wunong.smart.domain.platform.exception.IError;
import com.wunong.smart.domain.platform.exception.IErrorLevel;
import com.wunong.smart.domain.platform.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;

/**
* 限流异常
*
* @author created by zealot.zt
*/
public class LimitedException extends ServiceException {

/**
* 限流错误码
*/
public static final String LIMITED_ERROR_CODE = "LIMITED_ERROR";

/**
* 限流错误信息
*/
public static final IError LIMITED_ERROR = IError.of(LIMITED_ERROR_CODE, "亲,请求繁忙请稍后重试", IErrorLevel.IGNORE);

public LimitedException() {
super(LIMITED_ERROR);
}

public LimitedException(String errorInfo) {
super(IError.of(LIMITED_ERROR_CODE, errorInfo, IErrorLevel.IGNORE));
}

/**
* 构建降级错误
*
* @param errorInfo
* @return
*/
private static IError newLimitedError(String errorInfo) {
if (StringUtils.isBlank(errorInfo)) {
return LIMITED_ERROR;
} else {
return IError.of(LIMITED_ERROR_CODE, errorInfo, IErrorLevel.IGNORE);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.wunong.smart.rate.limiter.core.factory;

import com.wunong.smart.rate.limiter.core.api.IRateLimiter;
import com.wunong.smart.rate.limiter.core.config.LimiterData;
import com.wunong.smart.rate.limiter.core.exception.DegradedException;
import com.wunong.smart.rate.limiter.core.exception.LimitedException;

import java.lang.reflect.Method;

/**
* 限流工厂
*
* @author created by zealot.zt
*/
public interface RateLimiterFactory {

/**
* 尝试限流指定key
*
* @param key 被限流的key
* @return
* @throws LimitedException 限流成功抛出异常
*/
void tryLimit(String key) throws LimitedException, DegradedException;

/**
* 尝试限流指定方法
*
* @param method
* @return
* @throws LimitedException
*/
void tryLimit(Method method) throws LimitedException, DegradedException;

/**
* 尝试限流方法
*
* @param data
* @throws LimitedException
* @throws DegradedException
*/
void tryLimit(LimiterData data) throws LimitedException, DegradedException;

/**
* 根据key注册限制器
*
* @param key
* @param limiter
*/
void register(String key, IRateLimiter limiter);

}
Loading

0 comments on commit 468a3fe

Please sign in to comment.